usb: Copy image file to /tmp/images via USB
Traverse the USB mount directory, and copy the first file with
a `.tar` extension under the directory to the `/tmp/images`
directory.
Tested: Manually call the fs::copy_file app locally, test passed.
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I8e03b9c3305f5e9d9a1535dd176ecd40dbea8903
diff --git a/usb/usb_manager.cpp b/usb/usb_manager.cpp
new file mode 100644
index 0000000..b002dbd
--- /dev/null
+++ b/usb/usb_manager.cpp
@@ -0,0 +1,50 @@
+#include "config.h"
+
+#include "usb_manager.hpp"
+
+namespace phosphor
+{
+namespace usb
+{
+
+bool USBManager::run()
+{
+ fs::path dir(usbPath);
+ if (!fs::exists(dir))
+ {
+ return false;
+ }
+
+ for (const auto& p : std::filesystem::directory_iterator(dir))
+ {
+ if (p.path().extension() == ".tar")
+ {
+ fs::path dstPath{IMG_UPLOAD_DIR / p.path().filename()};
+ if (fs::exists(dstPath))
+ {
+ lg2::info(
+ "{DSTPATH} already exists in the /tmp/images directory, exit the upgrade",
+ "DSTPATH", p.path().filename());
+
+ break;
+ }
+
+ try
+ {
+ return fs::copy_file(fs::absolute(p.path()), dstPath);
+ }
+ catch (const std::exception& e)
+ {
+ lg2::error("Error when copying {SRC} to /tmp/images: {ERROR}",
+ "SRC", p.path(), "ERROR", e.what());
+ }
+
+ break;
+ }
+ }
+
+ return false;
+}
+
+} // namespace usb
+} // namespace phosphor
\ No newline at end of file