Ejemplo n.º 1
0
PlatformFile::PlatformFile(const std::string& path, int mode, int perms) {
  int oflag = 0;
  bool may_create = false;
  bool check_existence = false;

  if ((mode & PF_READ) == PF_READ && (mode & PF_WRITE) == PF_WRITE) {
    oflag = O_RDWR;
  } else if ((mode & PF_READ) == PF_READ) {
    oflag = O_RDONLY;
  } else if ((mode & PF_WRITE) == PF_WRITE) {
    oflag = O_WRONLY;
  }

  switch (PF_GET_OPTIONS(mode)) {
    case PF_GET_OPTIONS(PF_CREATE_ALWAYS):
      oflag |= O_CREAT;
      may_create = true;
      break;
    case PF_GET_OPTIONS(PF_CREATE_NEW):
      oflag |= O_CREAT | O_EXCL;
      may_create = true;
      break;
    case PF_GET_OPTIONS(PF_OPEN_EXISTING):
      check_existence = true;
      break;
    case PF_GET_OPTIONS(PF_TRUNCATE):
      if (mode & PF_WRITE) {
        oflag |= O_TRUNC;
      }

      break;
    default:
      break;
  }

  if ((mode & PF_NONBLOCK) == PF_NONBLOCK) {
    oflag |= O_NONBLOCK;
    is_nonblock_ = true;
  }

  if ((mode & PF_APPEND) == PF_APPEND) {
    oflag |= O_APPEND;
  }

  if (perms == -1 && may_create) {
    perms = 0666;
  }

  if (check_existence && !fs::exists(path.c_str())) {
    handle_ = kInvalidHandle;
  } else {
    handle_ = ::open(path.c_str(), oflag, perms);
  }
}
Ejemplo n.º 2
0
PlatformFile::PlatformFile(const fs::path& path, int mode, int perms)
    : fname_(path) {
  int oflag = 0;
  bool may_create = false;
  bool check_existence = false;

  if ((mode & PF_READ) == PF_READ && (mode & PF_WRITE) == PF_WRITE) {
    oflag = O_RDWR;
  } else if ((mode & PF_READ) == PF_READ) {
    oflag = O_RDONLY;
  } else if ((mode & PF_WRITE) == PF_WRITE) {
    oflag = O_WRONLY;
  }

  switch (PF_GET_OPTIONS(mode)) {
  case PF_GET_OPTIONS(PF_CREATE_ALWAYS):
    oflag |= O_CREAT | O_TRUNC;
    may_create = true;
    break;
  case PF_GET_OPTIONS(PF_CREATE_NEW):
    oflag |= O_CREAT | O_EXCL;
    may_create = true;
    break;
  case PF_GET_OPTIONS(PF_OPEN_EXISTING):
    check_existence = true;
    break;
  case PF_GET_OPTIONS(PF_OPEN_ALWAYS):
    oflag |= O_CREAT;
    may_create = true;
    break;
  case PF_GET_OPTIONS(PF_TRUNCATE):
    if (mode & PF_WRITE) {
      oflag |= O_TRUNC;
    }

    break;
  default:
    break;
  }

  if ((mode & PF_NONBLOCK) == PF_NONBLOCK) {
    oflag |= O_NONBLOCK;
    is_nonblock_ = true;
  }

  if ((mode & PF_APPEND) == PF_APPEND) {
    oflag |= O_APPEND;
  }

  if (perms == -1 && may_create) {
    perms = 0666;
  }

  boost::system::error_code ec;
  if (check_existence &&
      (!fs::exists(fname_, ec) || ec.value() != errc::success)) {
    handle_ = kInvalidHandle;
  } else {
    handle_ = ::open(fname_.c_str(), oflag, perms);
  }
}