Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame^] | 1 | // Copyright 2010 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "os.h" |
| 4 | |
| 5 | #include <cstddef> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | |
| 10 | #include "file_linux.h" |
| 11 | |
| 12 | namespace art { |
| 13 | |
| 14 | File* OS::OpenBinaryFile(const char* name, bool writable) { |
| 15 | int flags = O_RDONLY; |
| 16 | if (writable) { |
| 17 | flags = (O_RDWR | O_CREAT | O_TRUNC); |
| 18 | } |
| 19 | int fd = open(name, flags, 0666); |
| 20 | if (fd < 0) { |
| 21 | return NULL; |
| 22 | } |
| 23 | return new LinuxFile(name, fd, true); |
| 24 | } |
| 25 | |
| 26 | File* OS::OpenTextFile(const char* name, bool writable) { |
| 27 | return OpenBinaryFile(name, writable); |
| 28 | } |
| 29 | |
| 30 | File* OS::FileFromFd(const char* name, int fd) { |
| 31 | return new LinuxFile(name, fd, false); |
| 32 | } |
| 33 | |
| 34 | bool OS::FileExists(const char* name) { |
| 35 | struct stat st; |
| 36 | if (stat(name, &st) == 0) { |
| 37 | return S_ISREG(st.st_mode); // TODO Deal with symlinks? |
| 38 | } else { |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | } // namespace art |