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 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 10 | |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 11 | #include <filesystem> |
| 12 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 13 | namespace phosphor |
| 14 | { |
| 15 | namespace software |
| 16 | { |
| 17 | namespace manager |
| 18 | { |
| 19 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 20 | PHOSPHOR_LOG2_USING; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 21 | namespace fs = std::filesystem; |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 22 | |
| 23 | int Sync::processEntry(int mask, const fs::path& entryPath) |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 24 | { |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 25 | int status{}; |
| 26 | pid_t pid = fork(); |
| 27 | |
| 28 | if (pid == 0) |
| 29 | { |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 30 | fs::path dst(ALT_RWFS); |
| 31 | dst /= entryPath.relative_path(); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 32 | |
| 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 Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 55 | error("Error ({ERRNO}) occurred during the rsync call on {PATH}", |
| 56 | "ERRNO", errno, "PATH", entryPath); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 57 | 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 Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 64 | error( |
| 65 | "Error ({ERRNO}) occurred during the rsync delete call on {PATH}", |
| 66 | "ERRNO", errno, "PATH", entryPath); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | } |
| 70 | else if (pid > 0) |
| 71 | { |
| 72 | waitpid(pid, &status, 0); |
| 73 | } |
| 74 | else |
| 75 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 76 | error("Error ({ERRNO}) occurred during fork", "ERRNO", errno); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 77 | return -1; |
| 78 | } |
| 79 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | } // namespace manager |
| 84 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 85 | } // namespace phosphor |