blob: d3a1ccbcb84dfd03f424519109417fa6ce6383b7 [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
19#include <cstddef>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23
Brian Carlstromb45ccbf2013-03-13 15:14:24 -070024#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/unix_file/fd_file.h"
26#include "UniquePtr.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027
28namespace art {
29
Brian Carlstromf5822582012-03-19 22:34:31 -070030File* OS::OpenFile(const char* name, bool writable, bool create) {
Brian Carlstromb45ccbf2013-03-13 15:14:24 -070031 CHECK(name != NULL);
Brian Carlstromf5822582012-03-19 22:34:31 -070032 int flags = 0;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070033 if (writable) {
Brian Carlstromf5822582012-03-19 22:34:31 -070034 flags |= O_RDWR;
35 if (create) {
36 flags |= (O_CREAT | O_TRUNC);
37 }
38 } else {
39 flags |= O_RDONLY;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040 }
Elliott Hughes76160052012-12-12 16:31:20 -080041 UniquePtr<File> file(new File);
42 if (!file->Open(name, flags, 0666)) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 return NULL;
44 }
Elliott Hughes76160052012-12-12 16:31:20 -080045 return file.release();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046}
47
48bool OS::FileExists(const char* name) {
49 struct stat st;
50 if (stat(name, &st) == 0) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070051 return S_ISREG(st.st_mode); // TODO: Deal with symlinks?
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052 } else {
53 return false;
54 }
55}
56
Brian Carlstrom16192862011-09-12 17:50:06 -070057bool OS::DirectoryExists(const char* name) {
58 struct stat st;
59 if (stat(name, &st) == 0) {
60 return S_ISDIR(st.st_mode); // TODO: Deal with symlinks?
61 } else {
62 return false;
63 }
64}
65
Brian Carlstromdb4d5402011-08-09 12:18:28 -070066} // namespace art