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 | |
Jian Zhang | cb4f0ca | 2023-03-23 18:18:10 +0800 | [diff] [blame] | 54 | execl("/usr/bin/rsync", "rsync", "-aI", entryPath.c_str(), |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 55 | dst.c_str(), nullptr); |
Jian Zhang | cb4f0ca | 2023-03-23 18:18:10 +0800 | [diff] [blame] | 56 | |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 57 | // execl only returns on fail |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 58 | error("Error ({ERRNO}) occurred during the rsync call on {PATH}", |
| 59 | "ERRNO", errno, "PATH", entryPath); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 60 | return -1; |
| 61 | } |
| 62 | else if (mask & IN_DELETE) |
| 63 | { |
| 64 | execl("/usr/bin/rsync", "rsync", "-a", "--delete", |
| 65 | entryPath.c_str(), dst.c_str(), nullptr); |
| 66 | // execl only returns on fail |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 67 | error( |
| 68 | "Error ({ERRNO}) occurred during the rsync delete call on {PATH}", |
| 69 | "ERRNO", errno, "PATH", entryPath); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 70 | return -1; |
| 71 | } |
| 72 | } |
| 73 | else if (pid > 0) |
| 74 | { |
| 75 | waitpid(pid, &status, 0); |
| 76 | } |
| 77 | else |
| 78 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 79 | error("Error ({ERRNO}) occurred during fork", "ERRNO", errno); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 80 | return -1; |
| 81 | } |
| 82 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | } // namespace manager |
| 87 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 88 | } // namespace phosphor |