blob: b4f153d06be1a3cdec5ebf2c0b056e001c547404 [file] [log] [blame]
Gunnar Millsb0ce9962018-09-07 13:39:10 -05001#include "config.h"
2
3#include "sync_manager.hpp"
4
Adriana Kobylaka9074342018-05-08 11:52:44 -05005#include <sys/inotify.h>
6#include <sys/wait.h>
7#include <unistd.h>
Gunnar Millsb0ce9962018-09-07 13:39:10 -05008
Patrick Williamsc9bb6422021-08-27 06:18:35 -05009#include <phosphor-logging/lg2.hpp>
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050010
Adriana Kobylak58aa7502020-06-08 11:12:11 -050011#include <filesystem>
George Liu44b9fef2023-02-07 14:31:32 +080012#include <system_error>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050013
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050014namespace phosphor
15{
16namespace software
17{
18namespace manager
19{
20
Patrick Williamsc9bb6422021-08-27 06:18:35 -050021PHOSPHOR_LOG2_USING;
Adriana Kobylakc98d9122020-05-05 10:36:01 -050022namespace fs = std::filesystem;
Adriana Kobylaka9074342018-05-08 11:52:44 -050023
24int Sync::processEntry(int mask, const fs::path& entryPath)
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050025{
Adriana Kobylaka9074342018-05-08 11:52:44 -050026 int status{};
27 pid_t pid = fork();
28
29 if (pid == 0)
30 {
Adriana Kobylakc98d9122020-05-05 10:36:01 -050031 fs::path dst(ALT_RWFS);
32 dst /= entryPath.relative_path();
Adriana Kobylaka9074342018-05-08 11:52:44 -050033
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 Liu44b9fef2023-02-07 14:31:32 +080038 std::error_code ec;
39 if (!(fs::exists(dst, ec)))
Adriana Kobylaka9074342018-05-08 11:52:44 -050040 {
George Liu44b9fef2023-02-07 14:31:32 +080041 if (fs::is_directory(entryPath, ec))
Adriana Kobylaka9074342018-05-08 11:52:44 -050042 {
43 // Source is a directory, create it at the destination.
George Liu44b9fef2023-02-07 14:31:32 +080044 fs::create_directories(dst, ec);
Adriana Kobylaka9074342018-05-08 11:52:44 -050045 }
46 else
47 {
48 // Source is a file, create the directory where this file
49 // resides at the destination.
George Liu44b9fef2023-02-07 14:31:32 +080050 fs::create_directories(dst.parent_path(), ec);
Adriana Kobylaka9074342018-05-08 11:52:44 -050051 }
52 }
53
Jian Zhangcb4f0ca2023-03-23 18:18:10 +080054 execl("/usr/bin/rsync", "rsync", "-aI", entryPath.c_str(),
Adriana Kobylaka9074342018-05-08 11:52:44 -050055 dst.c_str(), nullptr);
Jian Zhangcb4f0ca2023-03-23 18:18:10 +080056
Adriana Kobylaka9074342018-05-08 11:52:44 -050057 // execl only returns on fail
Patrick Williamsc9bb6422021-08-27 06:18:35 -050058 error("Error ({ERRNO}) occurred during the rsync call on {PATH}",
59 "ERRNO", errno, "PATH", entryPath);
Adriana Kobylaka9074342018-05-08 11:52:44 -050060 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 Williamsc9bb6422021-08-27 06:18:35 -050067 error(
68 "Error ({ERRNO}) occurred during the rsync delete call on {PATH}",
69 "ERRNO", errno, "PATH", entryPath);
Adriana Kobylaka9074342018-05-08 11:52:44 -050070 return -1;
71 }
72 }
73 else if (pid > 0)
74 {
75 waitpid(pid, &status, 0);
76 }
77 else
78 {
Patrick Williamsc9bb6422021-08-27 06:18:35 -050079 error("Error ({ERRNO}) occurred during fork", "ERRNO", errno);
Adriana Kobylaka9074342018-05-08 11:52:44 -050080 return -1;
81 }
82
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050083 return 0;
84}
85
86} // namespace manager
87} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -050088} // namespace phosphor