blob: da8ea38ccec0e8681fb2ba48b563685152c3cc06 [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2010 Google Inc. All Rights Reserved.
2
3#include "os.h"
4
5#include <cstddef>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9
10#include "file_linux.h"
11
12namespace art {
13
14File* OS::OpenBinaryFile(const char* name, bool writable) {
15 int flags = O_RDONLY;
16 if (writable) {
17 flags = (O_RDWR | O_CREAT | O_TRUNC);
18 }
19 int fd = open(name, flags, 0666);
20 if (fd < 0) {
21 return NULL;
22 }
23 return new LinuxFile(name, fd, true);
24}
25
26File* OS::OpenTextFile(const char* name, bool writable) {
27 return OpenBinaryFile(name, writable);
28}
29
30File* OS::FileFromFd(const char* name, int fd) {
31 return new LinuxFile(name, fd, false);
32}
33
34bool OS::FileExists(const char* name) {
35 struct stat st;
36 if (stat(name, &st) == 0) {
37 return S_ISREG(st.st_mode); // TODO Deal with symlinks?
38 } else {
39 return false;
40 }
41}
42
43} // namespace art