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 | |
Brian Carlstrom | b45ccbf | 2013-03-13 15:14:24 -0700 | [diff] [blame] | 24 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 25 | #include "base/unix_file/fd_file.h" |
| 26 | #include "UniquePtr.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 30 | File* OS::OpenFileForReading(const char* name) { |
| 31 | return OpenFileWithFlags(name, O_RDONLY); |
| 32 | } |
| 33 | |
| 34 | File* OS::OpenFileReadWrite(const char* name) { |
| 35 | return OpenFileWithFlags(name, O_RDWR); |
| 36 | } |
| 37 | |
| 38 | File* OS::CreateEmptyFile(const char* name) { |
| 39 | return OpenFileWithFlags(name, O_RDWR | O_CREAT | O_TRUNC); |
| 40 | } |
| 41 | |
| 42 | File* OS::OpenFileWithFlags(const char* name, int flags) { |
Brian Carlstrom | b45ccbf | 2013-03-13 15:14:24 -0700 | [diff] [blame] | 43 | CHECK(name != NULL); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 44 | UniquePtr<File> file(new File); |
| 45 | if (!file->Open(name, flags, 0666)) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 46 | return NULL; |
| 47 | } |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 48 | return file.release(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool OS::FileExists(const char* name) { |
| 52 | struct stat st; |
| 53 | if (stat(name, &st) == 0) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 54 | return S_ISREG(st.st_mode); // TODO: Deal with symlinks? |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 55 | } else { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 60 | bool OS::DirectoryExists(const char* name) { |
| 61 | struct stat st; |
| 62 | if (stat(name, &st) == 0) { |
| 63 | return S_ISDIR(st.st_mode); // TODO: Deal with symlinks? |
| 64 | } else { |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 69 | } // namespace art |