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> |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 12 | #include <system_error> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 13 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 14 | namespace phosphor |
| 15 | { |
| 16 | namespace software |
| 17 | { |
| 18 | namespace manager |
| 19 | { |
| 20 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 21 | PHOSPHOR_LOG2_USING; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 22 | namespace fs = std::filesystem; |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 23 | |
| 24 | int Sync::processEntry(int mask, const fs::path& entryPath) |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 25 | { |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 26 | int status{}; |
| 27 | pid_t pid = fork(); |
| 28 | |
| 29 | if (pid == 0) |
| 30 | { |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 31 | fs::path dst(ALT_RWFS); |
| 32 | dst /= entryPath.relative_path(); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 33 | |
| 34 | // rsync needs an additional --delete argument to handle file deletions |
| 35 | // so need to differentiate between the different file events. |
| 36 | if (mask & IN_CLOSE_WRITE) |
| 37 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 38 | std::error_code ec; |
| 39 | if (!(fs::exists(dst, ec))) |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 40 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 41 | if (fs::is_directory(entryPath, ec)) |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 42 | { |
| 43 | // Source is a directory, create it at the destination. |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 44 | fs::create_directories(dst, ec); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 45 | } |
| 46 | else |
| 47 | { |
| 48 | // Source is a file, create the directory where this file |
| 49 | // resides at the destination. |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 50 | fs::create_directories(dst.parent_path(), ec); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | execl("/usr/bin/rsync", "rsync", "-a", entryPath.c_str(), |
| 55 | dst.c_str(), nullptr); |
| 56 | // execl only returns on fail |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 57 | error("Error ({ERRNO}) occurred during the rsync call on {PATH}", |
| 58 | "ERRNO", errno, "PATH", entryPath); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | else if (mask & IN_DELETE) |
| 62 | { |
| 63 | execl("/usr/bin/rsync", "rsync", "-a", "--delete", |
| 64 | entryPath.c_str(), dst.c_str(), nullptr); |
| 65 | // execl only returns on fail |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 66 | error( |
| 67 | "Error ({ERRNO}) occurred during the rsync delete call on {PATH}", |
| 68 | "ERRNO", errno, "PATH", entryPath); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | } |
| 72 | else if (pid > 0) |
| 73 | { |
| 74 | waitpid(pid, &status, 0); |
| 75 | } |
| 76 | else |
| 77 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 78 | error("Error ({ERRNO}) occurred during fork", "ERRNO", errno); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 79 | return -1; |
| 80 | } |
| 81 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | } // namespace manager |
| 86 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 87 | } // namespace phosphor |