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