blob: 5974c003d3b5d1dbe0b3205dee4ed81053878d50 [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
24#include "file_linux.h"
25
26namespace art {
27
Brian Carlstrom4e777d42011-08-15 13:53:52 -070028File* OS::OpenFile(const char* name, bool writable) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070029 int flags = O_RDONLY;
30 if (writable) {
31 flags = (O_RDWR | O_CREAT | O_TRUNC);
32 }
33 int fd = open(name, flags, 0666);
34 if (fd < 0) {
35 return NULL;
36 }
37 return new LinuxFile(name, fd, true);
38}
39
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040File* OS::FileFromFd(const char* name, int fd) {
41 return new LinuxFile(name, fd, false);
42}
43
44bool OS::FileExists(const char* name) {
45 struct stat st;
46 if (stat(name, &st) == 0) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070047 return S_ISREG(st.st_mode); // TODO: Deal with symlinks?
Brian Carlstromdb4d5402011-08-09 12:18:28 -070048 } else {
49 return false;
50 }
51}
52
Brian Carlstrom16192862011-09-12 17:50:06 -070053bool OS::DirectoryExists(const char* name) {
54 struct stat st;
55 if (stat(name, &st) == 0) {
56 return S_ISDIR(st.st_mode); // TODO: Deal with symlinks?
57 } else {
58 return false;
59 }
60}
61
Brian Carlstromdb4d5402011-08-09 12:18:28 -070062} // namespace art