Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | #include "fileutil.h" |
| 18 | |
| 19 | #include <errno.h> |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 20 | #include <fcntl.h> |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 21 | #include <glob.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 22 | #include <limits.h> |
Shinichiro Hamaji | 7cf1935 | 2016-01-26 15:32:44 +0900 | [diff] [blame^] | 23 | #include <signal.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 24 | #include <sys/stat.h> |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 27 | #include <unistd.h> |
Shinichiro Hamaji | b58bb4b | 2015-07-30 18:02:51 +0900 | [diff] [blame] | 28 | #if defined(__APPLE__) |
| 29 | #include <mach-o/dyld.h> |
| 30 | #endif |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 31 | |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 32 | #include <unordered_map> |
| 33 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 34 | #include "log.h" |
| 35 | |
| 36 | bool Exists(StringPiece filename) { |
| 37 | CHECK(filename.size() < PATH_MAX); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 38 | struct stat st; |
Shinichiro Hamaji | fda7943 | 2015-07-05 03:17:34 +0900 | [diff] [blame] | 39 | if (stat(filename.as_string().c_str(), &st) < 0) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 40 | return false; |
| 41 | } |
| 42 | return true; |
| 43 | } |
Shinichiro Hamaji | fda7943 | 2015-07-05 03:17:34 +0900 | [diff] [blame] | 44 | |
Shinichiro Hamaji | 6ce977d | 2015-10-15 16:01:16 +0900 | [diff] [blame] | 45 | double GetTimestampFromStat(const struct stat& st) { |
| 46 | #if defined(__linux__) |
| 47 | return st.st_mtime + st.st_mtim.tv_nsec * 0.001 * 0.001 * 0.001; |
| 48 | #else |
| 49 | return st.st_mtime; |
| 50 | #endif |
| 51 | } |
| 52 | |
Shinichiro Hamaji | fda7943 | 2015-07-05 03:17:34 +0900 | [diff] [blame] | 53 | double GetTimestamp(StringPiece filename) { |
| 54 | CHECK(filename.size() < PATH_MAX); |
| 55 | struct stat st; |
| 56 | if (stat(filename.as_string().c_str(), &st) < 0) { |
| 57 | return -2.0; |
| 58 | } |
Shinichiro Hamaji | 6ce977d | 2015-10-15 16:01:16 +0900 | [diff] [blame] | 59 | return GetTimestampFromStat(st); |
Shinichiro Hamaji | fda7943 | 2015-07-05 03:17:34 +0900 | [diff] [blame] | 60 | } |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 61 | |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 62 | int RunCommand(const string& shell, const string& cmd, |
| 63 | RedirectStderr redirect_stderr, |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 64 | string* s) { |
| 65 | int pipefd[2]; |
| 66 | if (pipe(pipefd) != 0) |
| 67 | PERROR("pipe failed"); |
| 68 | int pid; |
| 69 | if ((pid = vfork())) { |
| 70 | int status; |
| 71 | close(pipefd[1]); |
| 72 | while (true) { |
| 73 | int result = waitpid(pid, &status, WNOHANG); |
| 74 | if (result < 0) |
| 75 | PERROR("waitpid failed"); |
| 76 | |
| 77 | while (true) { |
| 78 | char buf[4096]; |
| 79 | ssize_t r = read(pipefd[0], buf, 4096); |
| 80 | if (r < 0) |
| 81 | PERROR("read failed"); |
| 82 | if (r == 0) |
| 83 | break; |
| 84 | s->append(buf, buf+r); |
| 85 | } |
| 86 | |
| 87 | if (result != 0) { |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | close(pipefd[0]); |
| 92 | |
| 93 | return status; |
| 94 | } else { |
| 95 | close(pipefd[0]); |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 96 | if (redirect_stderr == RedirectStderr::STDOUT) { |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 97 | if (dup2(pipefd[1], 2) < 0) |
| 98 | PERROR("dup2 failed"); |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 99 | } else if (redirect_stderr == RedirectStderr::DEV_NULL) { |
| 100 | int fd = open("/dev/null", O_WRONLY); |
| 101 | if (dup2(fd, 2) < 0) |
| 102 | PERROR("dup2 failed"); |
| 103 | close(fd); |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 104 | } |
| 105 | if (dup2(pipefd[1], 1) < 0) |
| 106 | PERROR("dup2 failed"); |
| 107 | close(pipefd[1]); |
| 108 | |
| 109 | const char* argv[] = { |
| 110 | shell.c_str(), "-c", cmd.c_str(), NULL |
| 111 | }; |
| 112 | execvp(argv[0], const_cast<char**>(argv)); |
Shinichiro Hamaji | 7cf1935 | 2016-01-26 15:32:44 +0900 | [diff] [blame^] | 113 | PLOG("execvp for %s failed", argv[0]); |
| 114 | kill(getppid(), SIGTERM); |
| 115 | _exit(1); |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 116 | } |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 117 | } |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 118 | |
Shinichiro Hamaji | b58bb4b | 2015-07-30 18:02:51 +0900 | [diff] [blame] | 119 | void GetExecutablePath(string* path) { |
| 120 | #if defined(__linux__) |
| 121 | char mypath[PATH_MAX + 1]; |
| 122 | ssize_t l = readlink("/proc/self/exe", mypath, PATH_MAX); |
| 123 | if (l < 0) { |
| 124 | PERROR("readlink for /proc/self/exe"); |
| 125 | } |
| 126 | mypath[l] = '\0'; |
| 127 | *path = mypath; |
| 128 | #elif defined(__APPLE__) |
| 129 | char mypath[PATH_MAX + 1]; |
Shinichiro Hamaji | e6ec118 | 2015-07-31 15:02:35 +0900 | [diff] [blame] | 130 | uint32_t size = PATH_MAX; |
| 131 | if (_NSGetExecutablePath(mypath, &size) != 0) { |
Shinichiro Hamaji | b58bb4b | 2015-07-30 18:02:51 +0900 | [diff] [blame] | 132 | ERROR("_NSGetExecutablePath failed"); |
| 133 | } |
Shinichiro Hamaji | e6ec118 | 2015-07-31 15:02:35 +0900 | [diff] [blame] | 134 | mypath[size] = 0; |
Shinichiro Hamaji | b58bb4b | 2015-07-30 18:02:51 +0900 | [diff] [blame] | 135 | *path = mypath; |
| 136 | #else |
| 137 | #error "Unsupported OS" |
| 138 | #endif |
| 139 | } |
| 140 | |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 141 | namespace { |
| 142 | |
| 143 | class GlobCache { |
| 144 | public: |
| 145 | ~GlobCache() { |
Shinichiro Hamaji | a09ed28 | 2015-07-31 12:21:54 +0900 | [diff] [blame] | 146 | Clear(); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void Get(const char* pat, vector<string>** files) { |
| 150 | auto p = cache_.emplace(pat, nullptr); |
| 151 | if (p.second) { |
| 152 | vector<string>* files = p.first->second = new vector<string>; |
| 153 | if (strcspn(pat, "?*[\\") != strlen(pat)) { |
| 154 | glob_t gl; |
| 155 | glob(pat, GLOB_NOSORT, NULL, &gl); |
| 156 | for (size_t i = 0; i < gl.gl_pathc; i++) { |
| 157 | files->push_back(gl.gl_pathv[i]); |
| 158 | } |
| 159 | globfree(&gl); |
| 160 | } else { |
| 161 | if (Exists(pat)) |
| 162 | files->push_back(pat); |
| 163 | } |
| 164 | } |
| 165 | *files = p.first->second; |
| 166 | } |
| 167 | |
Shinichiro Hamaji | a09ed28 | 2015-07-31 12:21:54 +0900 | [diff] [blame] | 168 | const unordered_map<string, vector<string>*>& GetAll() const { |
| 169 | return cache_; |
| 170 | } |
| 171 | |
| 172 | void Clear() { |
| 173 | for (auto& p : cache_) { |
| 174 | delete p.second; |
| 175 | } |
| 176 | cache_.clear(); |
| 177 | } |
| 178 | |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 179 | private: |
| 180 | unordered_map<string, vector<string>*> cache_; |
| 181 | }; |
| 182 | |
Shinichiro Hamaji | a09ed28 | 2015-07-31 12:21:54 +0900 | [diff] [blame] | 183 | static GlobCache g_gc; |
| 184 | |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 185 | } // namespace |
| 186 | |
| 187 | void Glob(const char* pat, vector<string>** files) { |
Shinichiro Hamaji | a09ed28 | 2015-07-31 12:21:54 +0900 | [diff] [blame] | 188 | g_gc.Get(pat, files); |
| 189 | } |
| 190 | |
| 191 | const unordered_map<string, vector<string>*>& GetAllGlobCache() { |
| 192 | return g_gc.GetAll(); |
| 193 | } |
| 194 | |
| 195 | void ClearGlobCache() { |
| 196 | g_gc.Clear(); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 197 | } |