blob: caafae0ff8bd130c8842e3a9eeffa07f953544e9 [file] [log] [blame]
Gunnar Millsb0ce9962018-09-07 13:39:10 -05001#include "config.h"
2
3#include "sync_manager.hpp"
4
Adriana Kobylaka9074342018-05-08 11:52:44 -05005#include <sys/inotify.h>
6#include <sys/wait.h>
7#include <unistd.h>
Gunnar Millsb0ce9962018-09-07 13:39:10 -05008
Patrick Williamsc9bb6422021-08-27 06:18:35 -05009#include <phosphor-logging/lg2.hpp>
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050010
Adriana Kobylak58aa7502020-06-08 11:12:11 -050011#include <filesystem>
George Liu44b9fef2023-02-07 14:31:32 +080012#include <system_error>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050013
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050014namespace phosphor
15{
16namespace software
17{
18namespace manager
19{
20
Patrick Williamsc9bb6422021-08-27 06:18:35 -050021PHOSPHOR_LOG2_USING;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050022namespace fs = std::filesystem;
Adriana Kobylaka9074342018-05-08 11:52:44 -050023
24int Sync::processEntry(int mask, const fs::path& entryPath)
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050025{
Adriana Kobylaka9074342018-05-08 11:52:44 -050026 int status{};
27 pid_t pid = fork();
28
29 if (pid == 0)
30 {
Adriana Kobylakc98d9122020-05-05 10:36:01 -050031 fs::path dst(ALT_RWFS);
32 dst /= entryPath.relative_path();
Adriana Kobylaka9074342018-05-08 11:52:44 -050033
34 // rsync needs an additional --delete argument to handle file deletions
35 // so need to differentiate between the different file events.
36 if (mask & IN_CLOSE_WRITE)
37 {
George Liu44b9fef2023-02-07 14:31:32 +080038 std::error_code ec;
39 if (!(fs::exists(dst, ec)))
Adriana Kobylaka9074342018-05-08 11:52:44 -050040 {
George Liu44b9fef2023-02-07 14:31:32 +080041 if (fs::is_directory(entryPath, ec))
Adriana Kobylaka9074342018-05-08 11:52:44 -050042 {
43 // Source is a directory, create it at the destination.
George Liu44b9fef2023-02-07 14:31:32 +080044 fs::create_directories(dst, ec);
Adriana Kobylaka9074342018-05-08 11:52:44 -050045 }
46 else
47 {
48 // Source is a file, create the directory where this file
49 // resides at the destination.
George Liu44b9fef2023-02-07 14:31:32 +080050 fs::create_directories(dst.parent_path(), ec);
Adriana Kobylaka9074342018-05-08 11:52:44 -050051 }
52 }
53
54 execl("/usr/bin/rsync", "rsync", "-a", entryPath.c_str(),
55 dst.c_str(), nullptr);
56 // execl only returns on fail
Patrick Williamsc9bb6422021-08-27 06:18:35 -050057 error("Error ({ERRNO}) occurred during the rsync call on {PATH}",
58 "ERRNO", errno, "PATH", entryPath);
Adriana Kobylaka9074342018-05-08 11:52:44 -050059 return -1;
60 }
61 else if (mask & IN_DELETE)
62 {
63 execl("/usr/bin/rsync", "rsync", "-a", "--delete",
64 entryPath.c_str(), dst.c_str(), nullptr);
65 // execl only returns on fail
Patrick Williamsc9bb6422021-08-27 06:18:35 -050066 error(
67 "Error ({ERRNO}) occurred during the rsync delete call on {PATH}",
68 "ERRNO", errno, "PATH", entryPath);
Adriana Kobylaka9074342018-05-08 11:52:44 -050069 return -1;
70 }
71 }
72 else if (pid > 0)
73 {
74 waitpid(pid, &status, 0);
75 }
76 else
77 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050078 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno);
Adriana Kobylaka9074342018-05-08 11:52:44 -050079 return -1;
80 }
81
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050082 return 0;
83}
84
85} // namespace manager
86} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -050087} // namespace phosphor