Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
| 17 | #include "os.h" |
| 18 | |
| 19 | #include <cstddef> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | |
| 24 | #include "file_linux.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 28 | File* OS::OpenFile(const char* name, bool writable, bool create) { |
| 29 | int flags = 0; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 30 | if (writable) { |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 31 | flags |= O_RDWR; |
| 32 | if (create) { |
| 33 | flags |= (O_CREAT | O_TRUNC); |
| 34 | } |
| 35 | } else { |
| 36 | flags |= O_RDONLY; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 37 | } |
| 38 | int fd = open(name, flags, 0666); |
| 39 | if (fd < 0) { |
| 40 | return NULL; |
| 41 | } |
| 42 | return new LinuxFile(name, fd, true); |
| 43 | } |
| 44 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 45 | File* OS::FileFromFd(const char* name, int fd) { |
| 46 | return new LinuxFile(name, fd, false); |
| 47 | } |
| 48 | |
| 49 | bool OS::FileExists(const char* name) { |
| 50 | struct stat st; |
| 51 | if (stat(name, &st) == 0) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 52 | return S_ISREG(st.st_mode); // TODO: Deal with symlinks? |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 53 | } else { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 58 | bool OS::DirectoryExists(const char* name) { |
| 59 | struct stat st; |
| 60 | if (stat(name, &st) == 0) { |
| 61 | return S_ISDIR(st.st_mode); // TODO: Deal with symlinks? |
| 62 | } else { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 67 | } // namespace art |