George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "usb_manager.hpp" |
| 4 | |
| 5 | namespace phosphor |
| 6 | { |
| 7 | namespace usb |
| 8 | { |
| 9 | |
| 10 | bool 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 |