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 "file.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
Shinichiro Hamaji | 706c27f | 2016-04-11 18:35:06 +0900 | [diff] [blame] | 24 | #include "fileutil.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 25 | #include "log.h" |
| 26 | #include "parser.h" |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 27 | #include "stmt.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 28 | |
| 29 | Makefile::Makefile(const string& filename) |
Shinichiro Hamaji | 0755047 | 2016-02-29 19:56:50 +0900 | [diff] [blame] | 30 | : mtime_(0), filename_(filename), exists_(false) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 31 | int fd = open(filename.c_str(), O_RDONLY); |
| 32 | if (fd < 0) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | struct stat st; |
| 37 | if (fstat(fd, &st) < 0) { |
| 38 | PERROR("fstat failed for %s", filename.c_str()); |
| 39 | } |
| 40 | |
Shinichiro Hamaji | 0755047 | 2016-02-29 19:56:50 +0900 | [diff] [blame] | 41 | size_t len = st.st_size; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 42 | mtime_ = st.st_mtime; |
Shinichiro Hamaji | 0755047 | 2016-02-29 19:56:50 +0900 | [diff] [blame] | 43 | buf_.resize(len); |
| 44 | exists_ = true; |
Shinichiro Hamaji | 706c27f | 2016-04-11 18:35:06 +0900 | [diff] [blame] | 45 | ssize_t r = HANDLE_EINTR(read(fd, &buf_[0], len)); |
Shinichiro Hamaji | 0755047 | 2016-02-29 19:56:50 +0900 | [diff] [blame] | 46 | if (r != static_cast<ssize_t>(len)) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 47 | if (r < 0) |
| 48 | PERROR("read failed for %s", filename.c_str()); |
Shinichiro Hamaji | 0755047 | 2016-02-29 19:56:50 +0900 | [diff] [blame] | 49 | ERROR("Unexpected read length=%zd expected=%zu", r, len); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | if (close(fd) < 0) { |
| 53 | PERROR("close failed for %s", filename.c_str()); |
| 54 | } |
| 55 | |
| 56 | Parse(this); |
| 57 | } |
| 58 | |
| 59 | Makefile::~Makefile() { |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 60 | for (Stmt* stmt : stmts_) |
| 61 | delete stmt; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 62 | } |