blob: e4403d79326aace9b67f7d86404eba85a35070ef [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
17#include "os.h"
18
Brian Carlstromdb4d5402011-08-09 12:18:28 -070019#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <cstddef>
23#include <memory>
Brian Carlstromdb4d5402011-08-09 12:18:28 -070024
Brian Carlstromb45ccbf2013-03-13 15:14:24 -070025#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/fd_file.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027
28namespace art {
29
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070030File* OS::OpenFileForReading(const char* name) {
31 return OpenFileWithFlags(name, O_RDONLY);
32}
33
34File* OS::OpenFileReadWrite(const char* name) {
35 return OpenFileWithFlags(name, O_RDWR);
36}
37
38File* OS::CreateEmptyFile(const char* name) {
39 return OpenFileWithFlags(name, O_RDWR | O_CREAT | O_TRUNC);
40}
41
42File* OS::OpenFileWithFlags(const char* name, int flags) {
Brian Carlstromb45ccbf2013-03-13 15:14:24 -070043 CHECK(name != NULL);
Ian Rogers700a4022014-05-19 16:49:03 -070044 std::unique_ptr<File> file(new File);
Elliott Hughes76160052012-12-12 16:31:20 -080045 if (!file->Open(name, flags, 0666)) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046 return NULL;
47 }
Elliott Hughes76160052012-12-12 16:31:20 -080048 return file.release();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070049}
50
51bool OS::FileExists(const char* name) {
52 struct stat st;
53 if (stat(name, &st) == 0) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070054 return S_ISREG(st.st_mode); // TODO: Deal with symlinks?
Brian Carlstromdb4d5402011-08-09 12:18:28 -070055 } else {
56 return false;
57 }
58}
59
Brian Carlstrom16192862011-09-12 17:50:06 -070060bool 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
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069} // namespace art