blob: c972f8f95ccdcb004e12c16742ae4028afaa4832 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstromdb4d5402011-08-09 12:18:28 -070016
David Sehr8f4b0562018-03-02 12:01:51 -080017#include "base/os.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070018
Brian Carlstromdb4d5402011-08-09 12:18:28 -070019#include <fcntl.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include <sys/stat.h>
21#include <sys/types.h>
22
Ian Rogers700a4022014-05-19 16:49:03 -070023#include <cstddef>
24#include <memory>
Brian Carlstromdb4d5402011-08-09 12:18:28 -070025
Andreas Gampe57943812017-12-06 21:39:13 -080026#include <android-base/logging.h>
27
Elliott Hughes76160052012-12-12 16:31:20 -080028#include "base/unix_file/fd_file.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070029
30namespace art {
31
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070032File* OS::OpenFileForReading(const char* name) {
33 return OpenFileWithFlags(name, O_RDONLY);
34}
35
36File* OS::OpenFileReadWrite(const char* name) {
37 return OpenFileWithFlags(name, O_RDWR);
38}
39
Calin Juravlef83e7332015-11-04 16:16:47 +000040static File* CreateEmptyFile(const char* name, int extra_flags) {
Andreas Gampeb73f1f52015-06-30 10:52:46 -070041 // 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 Juravlef83e7332015-11-04 16:16:47 +000045 return OS::OpenFileWithFlags(name, O_CREAT | extra_flags);
46}
47
48File* OS::CreateEmptyFile(const char* name) {
49 return art::CreateEmptyFile(name, O_RDWR | O_TRUNC);
50}
51
52File* OS::CreateEmptyFileWriteOnly(const char* name) {
53 return art::CreateEmptyFile(name, O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC);
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070054}
55
Calin Juravledf674c42017-04-27 19:30:16 -070056File* OS::OpenFileWithFlags(const char* name, int flags, bool auto_flush) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070057 CHECK(name != nullptr);
David Brazdilb64decd2016-08-09 12:10:56 +010058 bool read_only = ((flags & O_ACCMODE) == O_RDONLY);
Calin Juravledf674c42017-04-27 19:30:16 -070059 bool check_usage = !read_only && auto_flush;
Calin Juravle2a3fa672019-05-23 13:14:35 -070060 std::unique_ptr<File> file(
61 new File(name, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, check_usage));
Andreas Gampedf878922015-08-13 16:44:54 -070062 if (!file->IsOpened()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070063 return nullptr;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070064 }
Elliott Hughes76160052012-12-12 16:31:20 -080065 return file.release();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070066}
67
David Sehr8f4b0562018-03-02 12:01:51 -080068bool OS::FileExists(const char* name, bool check_file_type) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069 struct stat st;
70 if (stat(name, &st) == 0) {
David Sehr8f4b0562018-03-02 12:01:51 -080071 if (check_file_type) {
72 return S_ISREG(st.st_mode); // TODO: Deal with symlinks?
73 } else {
74 return true;
75 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070076 } else {
77 return false;
78 }
79}
80
Brian Carlstrom16192862011-09-12 17:50:06 -070081bool OS::DirectoryExists(const char* name) {
82 struct stat st;
83 if (stat(name, &st) == 0) {
84 return S_ISDIR(st.st_mode); // TODO: Deal with symlinks?
85 } else {
86 return false;
87 }
88}
89
David Sehr8f4b0562018-03-02 12:01:51 -080090int64_t OS::GetFileSizeBytes(const char* name) {
91 struct stat st;
92 if (stat(name, &st) == 0) {
Vladimir Markob08ad192018-03-08 14:25:47 +000093 return st.st_size; // TODO: Deal with symlinks? According to the documentation,
94 // the st_size for a symlink is "the length of the pathname
95 // it contains, without a terminating null byte."
David Sehr8f4b0562018-03-02 12:01:51 -080096 } else {
Vladimir Markob08ad192018-03-08 14:25:47 +000097 return -1;
David Sehr8f4b0562018-03-02 12:01:51 -080098 }
99}
100
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700101} // namespace art