Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 1 | #include <stdexcept> |
| 2 | #include <cstddef> |
| 3 | #include <cstring> |
| 4 | #include <string> |
| 5 | #include <sys/inotify.h> |
| 6 | #include <unistd.h> |
Gunnar Mills | 4e48fd5 | 2017-04-28 09:53:02 -0500 | [diff] [blame] | 7 | #include <experimental/filesystem> |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | #include "config.h" |
| 10 | #include "watch.hpp" |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 11 | #include "image_manager.hpp" |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace software |
| 16 | { |
| 17 | namespace manager |
| 18 | { |
| 19 | |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 20 | using namespace phosphor::logging; |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 21 | using namespace std::string_literals; |
Gunnar Mills | 4e48fd5 | 2017-04-28 09:53:02 -0500 | [diff] [blame] | 22 | namespace fs = std::experimental::filesystem; |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 23 | |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 24 | Watch::Watch(sd_event* loop, |
| 25 | std::function<int(std::string&)> imageCallback) : imageCallback( |
| 26 | imageCallback) |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 27 | { |
Gunnar Mills | 4e48fd5 | 2017-04-28 09:53:02 -0500 | [diff] [blame] | 28 | // Check if IMAGE DIR exists. |
| 29 | fs::path imgDirPath(IMG_UPLOAD_DIR); |
| 30 | if (!fs::is_directory(imgDirPath)) |
| 31 | { |
Gunnar Mills | 7e1abfc | 2017-11-09 15:43:50 -0600 | [diff] [blame] | 32 | fs::create_directories(imgDirPath); |
Gunnar Mills | 4e48fd5 | 2017-04-28 09:53:02 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 35 | fd = inotify_init1(IN_NONBLOCK); |
| 36 | if (-1 == fd) |
| 37 | { |
| 38 | // Store a copy of errno, because the string creation below will |
| 39 | // invalidate errno due to one more system calls. |
| 40 | auto error = errno; |
| 41 | throw std::runtime_error( |
| 42 | "inotify_init1 failed, errno="s + std::strerror(error)); |
| 43 | } |
| 44 | |
Gunnar Mills | 368cfcb | 2017-04-27 16:13:17 -0500 | [diff] [blame] | 45 | wd = inotify_add_watch(fd, IMG_UPLOAD_DIR, IN_CLOSE_WRITE); |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 46 | if (-1 == wd) |
| 47 | { |
| 48 | auto error = errno; |
| 49 | close(fd); |
| 50 | throw std::runtime_error( |
| 51 | "inotify_add_watch failed, errno="s + std::strerror(error)); |
| 52 | } |
| 53 | |
| 54 | auto rc = sd_event_add_io(loop, |
| 55 | nullptr, |
| 56 | fd, |
| 57 | EPOLLIN, |
| 58 | callback, |
| 59 | this); |
| 60 | if (0 > rc) |
| 61 | { |
| 62 | throw std::runtime_error( |
| 63 | "failed to add to event loop, rc="s + std::strerror(-rc)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | Watch::~Watch() |
| 68 | { |
| 69 | if ((-1 != fd) && (-1 != wd)) |
| 70 | { |
| 71 | inotify_rm_watch(fd, wd); |
| 72 | close(fd); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | int Watch::callback(sd_event_source* s, |
| 77 | int fd, |
| 78 | uint32_t revents, |
| 79 | void* userdata) |
| 80 | { |
| 81 | if (!(revents & EPOLLIN)) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | constexpr auto maxBytes = 1024; |
| 87 | uint8_t buffer[maxBytes]; |
| 88 | auto bytes = read(fd, buffer, maxBytes); |
| 89 | if (0 > bytes) |
| 90 | { |
| 91 | auto error = errno; |
| 92 | throw std::runtime_error( |
| 93 | "failed to read inotify event, errno="s + std::strerror(error)); |
| 94 | } |
| 95 | |
| 96 | auto offset = 0; |
| 97 | while (offset < bytes) |
| 98 | { |
| 99 | auto event = reinterpret_cast<inotify_event*>(&buffer[offset]); |
Gunnar Mills | 368cfcb | 2017-04-27 16:13:17 -0500 | [diff] [blame] | 100 | if ((event->mask & IN_CLOSE_WRITE) && !(event->mask & IN_ISDIR)) |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 101 | { |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 102 | auto tarballPath = std::string{IMG_UPLOAD_DIR} + '/' + event->name; |
| 103 | auto rc = static_cast<Watch*>(userdata)->imageCallback(tarballPath); |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 104 | if (rc < 0) |
| 105 | { |
| 106 | log<level::ERR>("Error processing image", |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 107 | entry("IMAGE=%s", tarballPath)); |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | offset += offsetof(inotify_event, name) + event->len; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | } // namespace manager |
| 119 | } // namespace software |
| 120 | } // namespace phosphor |