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 | |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 14 | File* OS::OpenFile(const char* name, bool writable) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 26 | File* OS::FileFromFd(const char* name, int fd) { |
| 27 | return new LinuxFile(name, fd, false); |
| 28 | } |
| 29 | |
| 30 | bool OS::FileExists(const char* name) { |
| 31 | struct stat st; |
| 32 | if (stat(name, &st) == 0) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 33 | return S_ISREG(st.st_mode); // TODO: Deal with symlinks? |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 34 | } else { |
| 35 | return false; |
| 36 | } |
| 37 | } |
| 38 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 39 | bool OS::DirectoryExists(const char* name) { |
| 40 | struct stat st; |
| 41 | if (stat(name, &st) == 0) { |
| 42 | return S_ISDIR(st.st_mode); // TODO: Deal with symlinks? |
| 43 | } else { |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 48 | } // namespace art |