blob: b002dbd63726894158947657269feb0829a0779e [file] [log] [blame]
George Liu073a6532021-10-25 14:40:03 +08001#include "config.h"
2
3#include "usb_manager.hpp"
4
5namespace phosphor
6{
7namespace usb
8{
9
10bool USBManager::run()
11{
12 fs::path dir(usbPath);
13 if (!fs::exists(dir))
14 {
15 return false;
16 }
17
18 for (const auto& p : std::filesystem::directory_iterator(dir))
19 {
20 if (p.path().extension() == ".tar")
21 {
22 fs::path dstPath{IMG_UPLOAD_DIR / p.path().filename()};
23 if (fs::exists(dstPath))
24 {
25 lg2::info(
26 "{DSTPATH} already exists in the /tmp/images directory, exit the upgrade",
27 "DSTPATH", p.path().filename());
28
29 break;
30 }
31
32 try
33 {
34 return fs::copy_file(fs::absolute(p.path()), dstPath);
35 }
36 catch (const std::exception& e)
37 {
38 lg2::error("Error when copying {SRC} to /tmp/images: {ERROR}",
39 "SRC", p.path(), "ERROR", e.what());
40 }
41
42 break;
43 }
44 }
45
46 return false;
47}
48
49} // namespace usb
50} // namespace phosphor