Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 1 | #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 Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 7 | #include "sync_manager.hpp" |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace software |
| 12 | { |
| 13 | namespace manager |
| 14 | { |
| 15 | |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 16 | using namespace phosphor::logging; |
| 17 | namespace fs = std::experimental::filesystem; |
| 18 | |
| 19 | int Sync::processEntry(int mask, const fs::path& entryPath) |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 20 | { |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 21 | 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 Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | } // namespace manager |
| 80 | } // namespace software |
| 81 | } // namepsace phosphor |