blob: f83aebd755f6cda6400d337455783aa593aecf04 [file] [log] [blame]
Adriana Kobylaka9074342018-05-08 11:52:44 -05001#include <experimental/filesystem>
2#include <phosphor-logging/log.hpp>
3#include <sys/inotify.h>
4#include <sys/wait.h>
5#include <unistd.h>
6#include "config.h"
Adriana Kobylakb072d1b2018-04-24 11:37:21 -05007#include "sync_manager.hpp"
8
9namespace phosphor
10{
11namespace software
12{
13namespace manager
14{
15
Adriana Kobylaka9074342018-05-08 11:52:44 -050016using namespace phosphor::logging;
17namespace fs = std::experimental::filesystem;
18
19int Sync::processEntry(int mask, const fs::path& entryPath)
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050020{
Adriana Kobylaka9074342018-05-08 11:52:44 -050021 int status{};
22 pid_t pid = fork();
23
24 if (pid == 0)
25 {
26 fs::path dst(ALT_RWFS / entryPath);
27
28 // rsync needs an additional --delete argument to handle file deletions
29 // so need to differentiate between the different file events.
30 if (mask & IN_CLOSE_WRITE)
31 {
32 if (!(fs::exists(dst)))
33 {
34 if (fs::is_directory(entryPath))
35 {
36 // Source is a directory, create it at the destination.
37 fs::create_directories(dst);
38 }
39 else
40 {
41 // Source is a file, create the directory where this file
42 // resides at the destination.
43 fs::create_directories(dst.parent_path());
44 }
45 }
46
47 execl("/usr/bin/rsync", "rsync", "-a", entryPath.c_str(),
48 dst.c_str(), nullptr);
49 // execl only returns on fail
50 log<level::ERR>("Error occurred during the rsync call",
51 entry("ERRNO=%d", errno),
52 entry("PATH=%s", entryPath.c_str()));
53 return -1;
54 }
55 else if (mask & IN_DELETE)
56 {
57 execl("/usr/bin/rsync", "rsync", "-a", "--delete",
58 entryPath.c_str(), dst.c_str(), nullptr);
59 // execl only returns on fail
60 log<level::ERR>("Error occurred during the rsync delete call",
61 entry("ERRNO=%d", errno),
62 entry("PATH=%s", entryPath.c_str()));
63 return -1;
64 }
65 }
66 else if (pid > 0)
67 {
68 waitpid(pid, &status, 0);
69 }
70 else
71 {
72 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", errno));
73 return -1;
74 }
75
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050076 return 0;
77}
78
79} // namespace manager
80} // namespace software
81} // namepsace phosphor