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 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <fcntl.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 22 | #include <cstddef> |
| 23 | #include <memory> |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 24 | |
Brian Carlstrom | b45ccbf | 2013-03-13 15:14:24 -0700 | [diff] [blame] | 25 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | #include "base/unix_file/fd_file.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 | |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 38 | static File* CreateEmptyFile(const char* name, int extra_flags) { |
Andreas Gampe | b73f1f5 | 2015-06-30 10:52:46 -0700 | [diff] [blame] | 39 | // In case the file exists, unlink it so we get a new file. This is necessary as the previous |
| 40 | // file may be in use and must not be changed. |
| 41 | unlink(name); |
| 42 | |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 43 | return OS::OpenFileWithFlags(name, O_CREAT | extra_flags); |
| 44 | } |
| 45 | |
| 46 | File* OS::CreateEmptyFile(const char* name) { |
| 47 | return art::CreateEmptyFile(name, O_RDWR | O_TRUNC); |
| 48 | } |
| 49 | |
| 50 | File* OS::CreateEmptyFileWriteOnly(const char* name) { |
| 51 | return art::CreateEmptyFile(name, O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | File* OS::OpenFileWithFlags(const char* name, int flags) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 55 | CHECK(name != nullptr); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 56 | std::unique_ptr<File> file(new File); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 57 | if (!file->Open(name, flags, 0666)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 58 | return nullptr; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 59 | } |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 60 | return file.release(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | bool OS::FileExists(const char* name) { |
| 64 | struct stat st; |
| 65 | if (stat(name, &st) == 0) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 66 | return S_ISREG(st.st_mode); // TODO: Deal with symlinks? |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 67 | } else { |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 72 | bool OS::DirectoryExists(const char* name) { |
| 73 | struct stat st; |
| 74 | if (stat(name, &st) == 0) { |
| 75 | return S_ISDIR(st.st_mode); // TODO: Deal with symlinks? |
| 76 | } else { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 81 | } // namespace art |