summaryrefslogtreecommitdiffstats
path: root/src/os_linux.cc
blob: 3fae386e39497897a0d69e272a2582734f76f9de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2010 Google Inc. All Rights Reserved.

#include "os.h"

#include <cstddef>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "file_linux.h"

namespace art {

File* OS::OpenFile(const char* name, bool writable) {
  int flags = O_RDONLY;
  if (writable) {
    flags = (O_RDWR | O_CREAT | O_TRUNC);
  }
  int fd = open(name, flags, 0666);
  if (fd < 0) {
    return NULL;
  }
  return new LinuxFile(name, fd, true);
}

File* OS::FileFromFd(const char* name, int fd) {
  return new LinuxFile(name, fd, false);
}

bool OS::FileExists(const char* name) {
  struct stat st;
  if (stat(name, &st) == 0) {
    return S_ISREG(st.st_mode);  // TODO: Deal with symlinks?
  } else {
    return false;
  }
}

bool OS::DirectoryExists(const char* name) {
  struct stat st;
  if (stat(name, &st) == 0) {
    return S_ISDIR(st.st_mode);  // TODO: Deal with symlinks?
  } else {
    return false;
  }
}

}  // namespace art