• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/os.h"
18 
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <cstddef>
24 #include <memory>
25 
26 #include <android-base/logging.h>
27 
28 #include "base/unix_file/fd_file.h"
29 
30 namespace art {
31 
OpenFileForReading(const char * name)32 File* OS::OpenFileForReading(const char* name) {
33   return OpenFileWithFlags(name, O_RDONLY);
34 }
35 
OpenFileReadWrite(const char * name)36 File* OS::OpenFileReadWrite(const char* name) {
37   return OpenFileWithFlags(name, O_RDWR);
38 }
39 
CreateEmptyFile(const char * name,int extra_flags)40 static File* CreateEmptyFile(const char* name, int extra_flags) {
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 
45   return OS::OpenFileWithFlags(name, O_CREAT | extra_flags);
46 }
47 
CreateEmptyFile(const char * name)48 File* OS::CreateEmptyFile(const char* name) {
49   return art::CreateEmptyFile(name, O_RDWR | O_TRUNC);
50 }
51 
CreateEmptyFileWriteOnly(const char * name)52 File* OS::CreateEmptyFileWriteOnly(const char* name) {
53   return art::CreateEmptyFile(name, O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC);
54 }
55 
OpenFileWithFlags(const char * name,int flags,bool auto_flush)56 File* OS::OpenFileWithFlags(const char* name, int flags, bool auto_flush) {
57   CHECK(name != nullptr);
58   bool read_only = ((flags & O_ACCMODE) == O_RDONLY);
59   bool check_usage = !read_only && auto_flush;
60   std::unique_ptr<File> file(
61       new File(name, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, check_usage));
62   if (!file->IsOpened()) {
63     return nullptr;
64   }
65   return file.release();
66 }
67 
FileExists(const char * name,bool check_file_type)68 bool OS::FileExists(const char* name, bool check_file_type) {
69   struct stat st;
70   if (stat(name, &st) == 0) {
71     if (check_file_type) {
72       return S_ISREG(st.st_mode);  // TODO: Deal with symlinks?
73     } else {
74       return true;
75     }
76   } else {
77     return false;
78   }
79 }
80 
DirectoryExists(const char * name)81 bool 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 
GetFileSizeBytes(const char * name)90 int64_t OS::GetFileSizeBytes(const char* name) {
91   struct stat st;
92   if (stat(name, &st) == 0) {
93     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."
96   } else {
97     return -1;
98   }
99 }
100 
101 }  // namespace art
102