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 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 17 | #include "os.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <cstddef> |
| 24 | #include <memory> |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 25 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 26 | #include <android-base/logging.h> |
| 27 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 28 | #include "unix_file/fd_file.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 32 | File* OS::OpenFileForReading(const char* name) { |
| 33 | return OpenFileWithFlags(name, O_RDONLY); |
| 34 | } |
| 35 | |
| 36 | File* OS::OpenFileReadWrite(const char* name) { |
| 37 | return OpenFileWithFlags(name, O_RDWR); |
| 38 | } |
| 39 | |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 40 | static File* CreateEmptyFile(const char* name, int extra_flags) { |
Andreas Gampe | b73f1f5 | 2015-06-30 10:52:46 -0700 | [diff] [blame] | 41 | // In case the file exists, unlink it so we get a new file. This is necessary as the previous |
| 42 | // file may be in use and must not be changed. |
| 43 | unlink(name); |
| 44 | |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 45 | return OS::OpenFileWithFlags(name, O_CREAT | extra_flags); |
| 46 | } |
| 47 | |
| 48 | File* OS::CreateEmptyFile(const char* name) { |
| 49 | return art::CreateEmptyFile(name, O_RDWR | O_TRUNC); |
| 50 | } |
| 51 | |
| 52 | File* OS::CreateEmptyFileWriteOnly(const char* name) { |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 53 | #ifdef _WIN32 |
| 54 | int flags = O_WRONLY | O_TRUNC; |
| 55 | #else |
| 56 | int flags = O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC; |
| 57 | #endif |
| 58 | return art::CreateEmptyFile(name, flags); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Calin Juravle | df674c4 | 2017-04-27 19:30:16 -0700 | [diff] [blame] | 61 | File* OS::OpenFileWithFlags(const char* name, int flags, bool auto_flush) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 62 | CHECK(name != nullptr); |
David Brazdil | b64decd | 2016-08-09 12:10:56 +0100 | [diff] [blame] | 63 | bool read_only = ((flags & O_ACCMODE) == O_RDONLY); |
Calin Juravle | df674c4 | 2017-04-27 19:30:16 -0700 | [diff] [blame] | 64 | bool check_usage = !read_only && auto_flush; |
Calin Juravle | 5bf2dbd | 2019-05-23 13:14:35 -0700 | [diff] [blame] | 65 | std::unique_ptr<File> file( |
| 66 | new File(name, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, check_usage)); |
Andreas Gampe | df87892 | 2015-08-13 16:44:54 -0700 | [diff] [blame] | 67 | if (!file->IsOpened()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 68 | return nullptr; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 69 | } |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 70 | return file.release(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 73 | bool OS::FileExists(const char* name, bool check_file_type) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 74 | struct stat st; |
| 75 | if (stat(name, &st) == 0) { |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 76 | if (check_file_type) { |
| 77 | return S_ISREG(st.st_mode); // TODO: Deal with symlinks? |
| 78 | } else { |
| 79 | return true; |
| 80 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 81 | } else { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 86 | bool OS::DirectoryExists(const char* name) { |
| 87 | struct stat st; |
| 88 | if (stat(name, &st) == 0) { |
| 89 | return S_ISDIR(st.st_mode); // TODO: Deal with symlinks? |
| 90 | } else { |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 95 | int64_t OS::GetFileSizeBytes(const char* name) { |
| 96 | struct stat st; |
| 97 | if (stat(name, &st) == 0) { |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 98 | return st.st_size; // TODO: Deal with symlinks? According to the documentation, |
| 99 | // the st_size for a symlink is "the length of the pathname |
| 100 | // it contains, without a terminating null byte." |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 101 | } else { |
Vladimir Marko | a5785a2 | 2018-03-08 14:25:47 +0000 | [diff] [blame] | 102 | return -1; |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 106 | } // namespace art |