blob: 3fae386e39497897a0d69e272a2582734f76f9de [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
Brian Carlstrom4e777d42011-08-15 13:53:52 -070014File* OS::OpenFile(const char* name, bool writable) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -070015 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
Brian Carlstromdb4d5402011-08-09 12:18:28 -070026File* OS::FileFromFd(const char* name, int fd) {
27 return new LinuxFile(name, fd, false);
28}
29
30bool OS::FileExists(const char* name) {
31 struct stat st;
32 if (stat(name, &st) == 0) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070033 return S_ISREG(st.st_mode); // TODO: Deal with symlinks?
Brian Carlstromdb4d5402011-08-09 12:18:28 -070034 } else {
35 return false;
36 }
37}
38
Brian Carlstrom16192862011-09-12 17:50:06 -070039bool OS::DirectoryExists(const char* name) {
40 struct stat st;
41 if (stat(name, &st) == 0) {
42 return S_ISDIR(st.st_mode); // TODO: Deal with symlinks?
43 } else {
44 return false;
45 }
46}
47
Brian Carlstromdb4d5402011-08-09 12:18:28 -070048} // namespace art