ipc/chromium/src/base/platform_file_posix.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/platform_file_posix.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,68 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/platform_file.h"
     1.9 +
    1.10 +#include <sys/stat.h>
    1.11 +#include <fcntl.h>
    1.12 +#include <errno.h>
    1.13 +#ifdef ANDROID
    1.14 +#include <linux/stat.h>
    1.15 +#endif
    1.16 +
    1.17 +#include "base/logging.h"
    1.18 +#include "base/string_util.h"
    1.19 +
    1.20 +namespace base {
    1.21 +
    1.22 +// TODO(erikkay): does it make sense to support PLATFORM_FILE_EXCLUSIVE_* here?
    1.23 +PlatformFile CreatePlatformFile(const std::wstring& name,
    1.24 +                                int flags,
    1.25 +                                bool* created) {
    1.26 +  int open_flags = 0;
    1.27 +  if (flags & PLATFORM_FILE_CREATE)
    1.28 +    open_flags = O_CREAT | O_EXCL;
    1.29 +
    1.30 +  if (flags & PLATFORM_FILE_CREATE_ALWAYS) {
    1.31 +    DCHECK(!open_flags);
    1.32 +    open_flags = O_CREAT | O_TRUNC;
    1.33 +  }
    1.34 +
    1.35 +  if (!open_flags && !(flags & PLATFORM_FILE_OPEN) &&
    1.36 +      !(flags & PLATFORM_FILE_OPEN_ALWAYS)) {
    1.37 +    NOTREACHED();
    1.38 +    errno = ENOTSUP;
    1.39 +    return kInvalidPlatformFileValue;
    1.40 +  }
    1.41 +
    1.42 +  if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) {
    1.43 +    open_flags |= O_RDWR;
    1.44 +  } else if (flags & PLATFORM_FILE_WRITE) {
    1.45 +    open_flags |= O_WRONLY;
    1.46 +  } else if (!(flags & PLATFORM_FILE_READ)) {
    1.47 +    NOTREACHED();
    1.48 +  }
    1.49 +
    1.50 +  DCHECK(O_RDONLY == 0);
    1.51 +
    1.52 +  int descriptor = open(WideToUTF8(name).c_str(), open_flags,
    1.53 +                        S_IRUSR | S_IWUSR);
    1.54 +
    1.55 +  if (flags & PLATFORM_FILE_OPEN_ALWAYS) {
    1.56 +    if (descriptor > 0) {
    1.57 +      if (created)
    1.58 +        *created = false;
    1.59 +    } else {
    1.60 +      open_flags |= O_CREAT;
    1.61 +      descriptor = open(WideToUTF8(name).c_str(), open_flags,
    1.62 +                        S_IRUSR | S_IWUSR);
    1.63 +      if (created && descriptor > 0)
    1.64 +        *created = true;
    1.65 +    }
    1.66 +  }
    1.67 +
    1.68 +  return descriptor;
    1.69 +}
    1.70 +
    1.71 +}  // namespace base

mercurial