blob: 1a3e0dd4afedc291186ecf4a1f3f5da1de3425a3 [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>
12
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050013namespace phosphor
14{
15namespace software
16{
17namespace manager
18{
19
Patrick Williamsc9bb6422021-08-27 06:18:35 -050020PHOSPHOR_LOG2_USING;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050021namespace fs = std::filesystem;
Adriana Kobylaka9074342018-05-08 11:52:44 -050022
23int Sync::processEntry(int mask, const fs::path& entryPath)
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050024{
Adriana Kobylaka9074342018-05-08 11:52:44 -050025 int status{};
26 pid_t pid = fork();
27
28 if (pid == 0)
29 {
Adriana Kobylakc98d9122020-05-05 10:36:01 -050030 fs::path dst(ALT_RWFS);
31 dst /= entryPath.relative_path();
Adriana Kobylaka9074342018-05-08 11:52:44 -050032
33 // rsync needs an additional --delete argument to handle file deletions
34 // so need to differentiate between the different file events.
35 if (mask & IN_CLOSE_WRITE)
36 {
37 if (!(fs::exists(dst)))
38 {
39 if (fs::is_directory(entryPath))
40 {
41 // Source is a directory, create it at the destination.
42 fs::create_directories(dst);
43 }
44 else
45 {
46 // Source is a file, create the directory where this file
47 // resides at the destination.
48 fs::create_directories(dst.parent_path());
49 }
50 }
51
52 execl("/usr/bin/rsync", "rsync", "-a", entryPath.c_str(),
53 dst.c_str(), nullptr);
54 // execl only returns on fail
Patrick Williamsc9bb6422021-08-27 06:18:35 -050055 error("Error ({ERRNO}) occurred during the rsync call on {PATH}",
56 "ERRNO", errno, "PATH", entryPath);
Adriana Kobylaka9074342018-05-08 11:52:44 -050057 return -1;
58 }
59 else if (mask & IN_DELETE)
60 {
61 execl("/usr/bin/rsync", "rsync", "-a", "--delete",
62 entryPath.c_str(), dst.c_str(), nullptr);
63 // execl only returns on fail
Patrick Williamsc9bb6422021-08-27 06:18:35 -050064 error(
65 "Error ({ERRNO}) occurred during the rsync delete call on {PATH}",
66 "ERRNO", errno, "PATH", entryPath);
Adriana Kobylaka9074342018-05-08 11:52:44 -050067 return -1;
68 }
69 }
70 else if (pid > 0)
71 {
72 waitpid(pid, &status, 0);
73 }
74 else
75 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050076 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno);
Adriana Kobylaka9074342018-05-08 11:52:44 -050077 return -1;
78 }
79
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050080 return 0;
81}
82
83} // namespace manager
84} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -050085} // namespace phosphor