2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
25 #include "base/logging.h"
26 #include "base/unix_file/fd_file.h"
30 File* OS::OpenFileForReading(const char* name) {
31 return OpenFileWithFlags(name, O_RDONLY);
34 File* OS::OpenFileReadWrite(const char* name) {
35 return OpenFileWithFlags(name, O_RDWR);
38 static File* CreateEmptyFile(const char* name, int extra_flags) {
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.
43 return OS::OpenFileWithFlags(name, O_CREAT | extra_flags);
46 File* OS::CreateEmptyFile(const char* name) {
47 return art::CreateEmptyFile(name, O_RDWR | O_TRUNC);
50 File* OS::CreateEmptyFileWriteOnly(const char* name) {
51 return art::CreateEmptyFile(name, O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC);
54 File* OS::OpenFileWithFlags(const char* name, int flags) {
55 CHECK(name != nullptr);
56 std::unique_ptr<File> file(new File);
57 if (!file->Open(name, flags, 0666)) {
60 return file.release();
63 bool OS::FileExists(const char* name) {
65 if (stat(name, &st) == 0) {
66 return S_ISREG(st.st_mode); // TODO: Deal with symlinks?
72 bool OS::DirectoryExists(const char* name) {
74 if (stat(name, &st) == 0) {
75 return S_ISDIR(st.st_mode); // TODO: Deal with symlinks?