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 */
16
17 #include "os.h"
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <cstddef>
23 #include <memory>
24
25 #include "base/logging.h"
26 #include "base/unix_file/fd_file.h"
27
28 namespace art {
29
OpenFileForReading(const char * name)30 File* OS::OpenFileForReading(const char* name) {
31 return OpenFileWithFlags(name, O_RDONLY);
32 }
33
OpenFileReadWrite(const char * name)34 File* OS::OpenFileReadWrite(const char* name) {
35 return OpenFileWithFlags(name, O_RDWR);
36 }
37
CreateEmptyFile(const char * name)38 File* OS::CreateEmptyFile(const char* name) {
39 return OpenFileWithFlags(name, O_RDWR | O_CREAT | O_TRUNC);
40 }
41
OpenFileWithFlags(const char * name,int flags)42 File* OS::OpenFileWithFlags(const char* name, int flags) {
43 CHECK(name != NULL);
44 std::unique_ptr<File> file(new File);
45 if (!file->Open(name, flags, 0666)) {
46 return NULL;
47 }
48 return file.release();
49 }
50
FileExists(const char * name)51 bool OS::FileExists(const char* name) {
52 struct stat st;
53 if (stat(name, &st) == 0) {
54 return S_ISREG(st.st_mode); // TODO: Deal with symlinks?
55 } else {
56 return false;
57 }
58 }
59
DirectoryExists(const char * name)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
69 } // namespace art
70