Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "sync_manager.hpp" |
| 4 | |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 5 | #include <sys/inotify.h> |
| 6 | #include <sys/wait.h> |
| 7 | #include <unistd.h> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 8 | |
| 9 | #include <experimental/filesystem> |
| 10 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace software |
| 15 | { |
| 16 | namespace manager |
| 17 | { |
| 18 | |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 19 | using namespace phosphor::logging; |
| 20 | namespace fs = std::experimental::filesystem; |
| 21 | |
| 22 | int Sync::processEntry(int mask, const fs::path& entryPath) |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 23 | { |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 24 | 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 Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | } // namespace manager |
| 83 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 84 | } // namespace phosphor |