blob: 48d773bbb073219e803f3df9b5e6d2c52ffd7201 [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
9#include <experimental/filesystem>
10#include <phosphor-logging/log.hpp>
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050011
12namespace phosphor
13{
14namespace software
15{
16namespace manager
17{
18
Adriana Kobylaka9074342018-05-08 11:52:44 -050019using namespace phosphor::logging;
20namespace fs = std::experimental::filesystem;
21
22int Sync::processEntry(int mask, const fs::path& entryPath)
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050023{
Adriana Kobylaka9074342018-05-08 11:52:44 -050024 int status{};
25 pid_t pid = fork();
26
27 if (pid == 0)
28 {
29 fs::path dst(ALT_RWFS / entryPath);
30
31 // rsync needs an additional --delete argument to handle file deletions
32 // so need to differentiate between the different file events.
33 if (mask & IN_CLOSE_WRITE)
34 {
35 if (!(fs::exists(dst)))
36 {
37 if (fs::is_directory(entryPath))
38 {
39 // Source is a directory, create it at the destination.
40 fs::create_directories(dst);
41 }
42 else
43 {
44 // Source is a file, create the directory where this file
45 // resides at the destination.
46 fs::create_directories(dst.parent_path());
47 }
48 }
49
50 execl("/usr/bin/rsync", "rsync", "-a", entryPath.c_str(),
51 dst.c_str(), nullptr);
52 // execl only returns on fail
53 log<level::ERR>("Error occurred during the rsync call",
54 entry("ERRNO=%d", errno),
55 entry("PATH=%s", entryPath.c_str()));
56 return -1;
57 }
58 else if (mask & IN_DELETE)
59 {
60 execl("/usr/bin/rsync", "rsync", "-a", "--delete",
61 entryPath.c_str(), dst.c_str(), nullptr);
62 // execl only returns on fail
63 log<level::ERR>("Error occurred during the rsync delete call",
64 entry("ERRNO=%d", errno),
65 entry("PATH=%s", entryPath.c_str()));
66 return -1;
67 }
68 }
69 else if (pid > 0)
70 {
71 waitpid(pid, &status, 0);
72 }
73 else
74 {
75 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", errno));
76 return -1;
77 }
78
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050079 return 0;
80}
81
82} // namespace manager
83} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -050084} // namespace phosphor