• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 #ifndef ART_LIBARTBASE_BASE_OS_H_
18 #define ART_LIBARTBASE_BASE_OS_H_
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <cstddef>
24 #include <memory>
25 #include <string>
26 
27 #include "unix_file/fd_file.h"
28 
29 namespace art {
30 
31 using File = ::unix_file::FdFile;
32 
33 struct FileWithRange {
34   std::unique_ptr<File> file;
35   off_t start;
36   size_t length;
37 
38   static FileWithRange Invalid();
39 };
40 
41 // Interface to the underlying OS platform.
42 class OS {
43  public:
44   // Open an existing file with read only access.
45   static File* OpenFileForReading(const char* name);
46 
47   // Open an existing file with read/write access.
48   static File* OpenFileReadWrite(const char* name);
49 
50   // Create an empty file with read/write access. This is a *new* file, that is, if the file
51   // already exists, it is *not* overwritten, but unlinked, and a new inode will be used.
52   static File* CreateEmptyFile(const char* name);
53 
54   // Create an empty file with write access. This is a *new* file, that is, if the file
55   // already exists, it is *not* overwritten, but unlinked, and a new inode will be used.
56   static File* CreateEmptyFileWriteOnly(const char* name);
57 
58   // Open a file with the specified open(2) flags.
59   static File* OpenFileWithFlags(const char* name, int flags, bool auto_flush = true);
60 
61   // Check if a file exists.
62   static bool FileExists(const char* name, bool check_file_type = true);
63 
64   // Check if a directory exists.
65   static bool DirectoryExists(const char* name);
66 
67   // Get the size of a file (or -1 if it does not exist).
68   static int64_t GetFileSizeBytes(const char* name);
69 
70   // Open an existing file or an entry in a zip file with read only access.
71   // `name_and_zip_entry` should be either a path to an existing file, or a path to a zip file and
72   // the name of the zip entry, separated by `zip_separator`.
73   // `alignment` is the expected alignment of the specified zip entry, in bytes. Only applicable if
74   // `name_and_zip_entry` points to a zip entry.
75   // Returns `file` being the file at the specified path and the range being the entire range of the
76   // file, if `name_and_zip_entry` points to a file. Returns `file` being the zip file and the range
77   // being the range of the zip entry, if `name_and_zip_entry` points to a zip entry. Returns `file`
78   // being nullptr on failure.
79   static FileWithRange OpenFileDirectlyOrFromZip(const std::string& name_and_zip_entry,
80                                                  const char* zip_separator,
81                                                  size_t alignment,
82                                                  std::string* error_msg);
83 };
84 
85 }  // namespace art
86 
87 #endif  // ART_LIBARTBASE_BASE_OS_H_
88