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> |
| 20 | #include <limits.h> |
| 21 | #include <sys/stat.h> |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame^] | 22 | #include <sys/types.h> |
| 23 | #include <sys/wait.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "log.h" |
| 27 | |
| 28 | bool Exists(StringPiece filename) { |
| 29 | CHECK(filename.size() < PATH_MAX); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 30 | struct stat st; |
Shinichiro Hamaji | fda7943 | 2015-07-05 03:17:34 +0900 | [diff] [blame] | 31 | if (stat(filename.as_string().c_str(), &st) < 0) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } |
Shinichiro Hamaji | fda7943 | 2015-07-05 03:17:34 +0900 | [diff] [blame] | 36 | |
| 37 | double GetTimestamp(StringPiece filename) { |
| 38 | CHECK(filename.size() < PATH_MAX); |
| 39 | struct stat st; |
| 40 | if (stat(filename.as_string().c_str(), &st) < 0) { |
| 41 | return -2.0; |
| 42 | } |
| 43 | return st.st_mtime; |
| 44 | } |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame^] | 45 | |
| 46 | int RunCommand(const string& shell, const string& cmd, bool redirect_stderr, |
| 47 | string* s) { |
| 48 | int pipefd[2]; |
| 49 | if (pipe(pipefd) != 0) |
| 50 | PERROR("pipe failed"); |
| 51 | int pid; |
| 52 | if ((pid = vfork())) { |
| 53 | int status; |
| 54 | close(pipefd[1]); |
| 55 | while (true) { |
| 56 | int result = waitpid(pid, &status, WNOHANG); |
| 57 | if (result < 0) |
| 58 | PERROR("waitpid failed"); |
| 59 | |
| 60 | while (true) { |
| 61 | char buf[4096]; |
| 62 | ssize_t r = read(pipefd[0], buf, 4096); |
| 63 | if (r < 0) |
| 64 | PERROR("read failed"); |
| 65 | if (r == 0) |
| 66 | break; |
| 67 | s->append(buf, buf+r); |
| 68 | } |
| 69 | |
| 70 | if (result != 0) { |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | close(pipefd[0]); |
| 75 | |
| 76 | return status; |
| 77 | } else { |
| 78 | close(pipefd[0]); |
| 79 | if (redirect_stderr) { |
| 80 | if (dup2(pipefd[1], 2) < 0) |
| 81 | PERROR("dup2 failed"); |
| 82 | } |
| 83 | if (dup2(pipefd[1], 1) < 0) |
| 84 | PERROR("dup2 failed"); |
| 85 | close(pipefd[1]); |
| 86 | |
| 87 | const char* argv[] = { |
| 88 | shell.c_str(), "-c", cmd.c_str(), NULL |
| 89 | }; |
| 90 | execvp(argv[0], const_cast<char**>(argv)); |
| 91 | } |
| 92 | abort(); |
| 93 | } |