security/sandbox/chromium/base/platform_file.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/platform_file.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,254 @@
     1.4 +// Copyright (c) 2012 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 +#ifndef BASE_PLATFORM_FILE_H_
     1.9 +#define BASE_PLATFORM_FILE_H_
    1.10 +
    1.11 +#include "build/build_config.h"
    1.12 +#if defined(OS_WIN)
    1.13 +#include <windows.h>
    1.14 +#endif
    1.15 +
    1.16 +#include <string>
    1.17 +
    1.18 +#include "base/base_export.h"
    1.19 +#include "base/basictypes.h"
    1.20 +#include "base/files/file_path.h"
    1.21 +#include "base/time/time.h"
    1.22 +
    1.23 +namespace base {
    1.24 +
    1.25 +// PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify
    1.26 +// exactly one of the five (possibly combining with other flags) when opening
    1.27 +// or creating a file.
    1.28 +// PLATFORM_FILE_(WRITE|APPEND) are mutually exclusive. This is so that APPEND
    1.29 +// behavior will be consistent with O_APPEND on POSIX.
    1.30 +enum PlatformFileFlags {
    1.31 +  PLATFORM_FILE_OPEN = 1 << 0,             // Opens a file, only if it exists.
    1.32 +  PLATFORM_FILE_CREATE = 1 << 1,           // Creates a new file, only if it
    1.33 +                                           // does not already exist.
    1.34 +  PLATFORM_FILE_OPEN_ALWAYS = 1 << 2,      // May create a new file.
    1.35 +  PLATFORM_FILE_CREATE_ALWAYS = 1 << 3,    // May overwrite an old file.
    1.36 +  PLATFORM_FILE_OPEN_TRUNCATED = 1 << 4,   // Opens a file and truncates it,
    1.37 +                                           // only if it exists.
    1.38 +  PLATFORM_FILE_READ = 1 << 5,
    1.39 +  PLATFORM_FILE_WRITE = 1 << 6,
    1.40 +  PLATFORM_FILE_APPEND = 1 << 7,
    1.41 +  PLATFORM_FILE_EXCLUSIVE_READ = 1 << 8,   // EXCLUSIVE is opposite of Windows
    1.42 +                                           // SHARE
    1.43 +  PLATFORM_FILE_EXCLUSIVE_WRITE = 1 << 9,
    1.44 +  PLATFORM_FILE_ASYNC = 1 << 10,
    1.45 +  PLATFORM_FILE_TEMPORARY = 1 << 11,       // Used on Windows only
    1.46 +  PLATFORM_FILE_HIDDEN = 1 << 12,          // Used on Windows only
    1.47 +  PLATFORM_FILE_DELETE_ON_CLOSE = 1 << 13,
    1.48 +
    1.49 +  PLATFORM_FILE_WRITE_ATTRIBUTES = 1 << 14,  // Used on Windows only
    1.50 +  PLATFORM_FILE_ENUMERATE = 1 << 15,         // May enumerate directory
    1.51 +
    1.52 +  PLATFORM_FILE_SHARE_DELETE = 1 << 16,      // Used on Windows only
    1.53 +
    1.54 +  PLATFORM_FILE_TERMINAL_DEVICE = 1 << 17,   // Serial port flags
    1.55 +  PLATFORM_FILE_BACKUP_SEMANTICS = 1 << 18,  // Used on Windows only
    1.56 +
    1.57 +  PLATFORM_FILE_EXECUTE = 1 << 19,           // Used on Windows only
    1.58 +};
    1.59 +
    1.60 +// PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
    1.61 +// a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
    1.62 +// browser policy doesn't allow the operation to be executed.
    1.63 +enum PlatformFileError {
    1.64 +  PLATFORM_FILE_OK = 0,
    1.65 +  PLATFORM_FILE_ERROR_FAILED = -1,
    1.66 +  PLATFORM_FILE_ERROR_IN_USE = -2,
    1.67 +  PLATFORM_FILE_ERROR_EXISTS = -3,
    1.68 +  PLATFORM_FILE_ERROR_NOT_FOUND = -4,
    1.69 +  PLATFORM_FILE_ERROR_ACCESS_DENIED = -5,
    1.70 +  PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6,
    1.71 +  PLATFORM_FILE_ERROR_NO_MEMORY = -7,
    1.72 +  PLATFORM_FILE_ERROR_NO_SPACE = -8,
    1.73 +  PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9,
    1.74 +  PLATFORM_FILE_ERROR_INVALID_OPERATION = -10,
    1.75 +  PLATFORM_FILE_ERROR_SECURITY = -11,
    1.76 +  PLATFORM_FILE_ERROR_ABORT = -12,
    1.77 +  PLATFORM_FILE_ERROR_NOT_A_FILE = -13,
    1.78 +  PLATFORM_FILE_ERROR_NOT_EMPTY = -14,
    1.79 +  PLATFORM_FILE_ERROR_INVALID_URL = -15,
    1.80 +  PLATFORM_FILE_ERROR_IO = -16,
    1.81 +  // Put new entries here and increment PLATFORM_FILE_ERROR_MAX.
    1.82 +  PLATFORM_FILE_ERROR_MAX = -17
    1.83 +};
    1.84 +
    1.85 +// This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux.
    1.86 +enum PlatformFileWhence {
    1.87 +  PLATFORM_FILE_FROM_BEGIN   = 0,
    1.88 +  PLATFORM_FILE_FROM_CURRENT = 1,
    1.89 +  PLATFORM_FILE_FROM_END     = 2
    1.90 +};
    1.91 +
    1.92 +// Used to hold information about a given file.
    1.93 +// If you add more fields to this structure (platform-specific fields are OK),
    1.94 +// make sure to update all functions that use it in file_util_{win|posix}.cc
    1.95 +// too, and the ParamTraits<base::PlatformFileInfo> implementation in
    1.96 +// chrome/common/common_param_traits.cc.
    1.97 +struct BASE_EXPORT PlatformFileInfo {
    1.98 +  PlatformFileInfo();
    1.99 +  ~PlatformFileInfo();
   1.100 +
   1.101 +  // The size of the file in bytes.  Undefined when is_directory is true.
   1.102 +  int64 size;
   1.103 +
   1.104 +  // True if the file corresponds to a directory.
   1.105 +  bool is_directory;
   1.106 +
   1.107 +  // True if the file corresponds to a symbolic link.
   1.108 +  bool is_symbolic_link;
   1.109 +
   1.110 +  // The last modified time of a file.
   1.111 +  base::Time last_modified;
   1.112 +
   1.113 +  // The last accessed time of a file.
   1.114 +  base::Time last_accessed;
   1.115 +
   1.116 +  // The creation time of a file.
   1.117 +  base::Time creation_time;
   1.118 +};
   1.119 +
   1.120 +#if defined(OS_WIN)
   1.121 +typedef HANDLE PlatformFile;
   1.122 +const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
   1.123 +PlatformFileError LastErrorToPlatformFileError(DWORD saved_errno);
   1.124 +#elif defined(OS_POSIX)
   1.125 +typedef int PlatformFile;
   1.126 +const PlatformFile kInvalidPlatformFileValue = -1;
   1.127 +PlatformFileError ErrnoToPlatformFileError(int saved_errno);
   1.128 +#endif
   1.129 +
   1.130 +// Creates or opens the given file. If |created| is provided, it will be set to
   1.131 +// true if a new file was created [or an old one truncated to zero length to
   1.132 +// simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and
   1.133 +// false otherwise.  |error| can be NULL.
   1.134 +//
   1.135 +// This function fails with 'access denied' if the |name| contains path
   1.136 +// traversal ('..') components.
   1.137 +BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name,
   1.138 +                                            int flags,
   1.139 +                                            bool* created,
   1.140 +                                            PlatformFileError* error);
   1.141 +
   1.142 +// Same as CreatePlatformFile but allows paths with traversal (like \..\)
   1.143 +// components. Use only with extreme care.
   1.144 +BASE_EXPORT PlatformFile CreatePlatformFileUnsafe(const FilePath& name,
   1.145 +                                                  int flags,
   1.146 +                                                  bool* created,
   1.147 +                                                  PlatformFileError* error);
   1.148 +
   1.149 +BASE_EXPORT FILE* FdopenPlatformFile(PlatformFile file, const char* mode);
   1.150 +
   1.151 +// Closes a file handle. Returns |true| on success and |false| otherwise.
   1.152 +BASE_EXPORT bool ClosePlatformFile(PlatformFile file);
   1.153 +
   1.154 +// Changes current position in the file to an |offset| relative to an origin
   1.155 +// defined by |whence|. Returns the resultant current position in the file
   1.156 +// (relative to the start) or -1 in case of error.
   1.157 +BASE_EXPORT int64 SeekPlatformFile(PlatformFile file,
   1.158 +                                   PlatformFileWhence whence,
   1.159 +                                   int64 offset);
   1.160 +
   1.161 +// Reads the given number of bytes (or until EOF is reached) starting with the
   1.162 +// given offset. Returns the number of bytes read, or -1 on error. Note that
   1.163 +// this function makes a best effort to read all data on all platforms, so it is
   1.164 +// not intended for stream oriented files but instead for cases when the normal
   1.165 +// expectation is that actually |size| bytes are read unless there is an error.
   1.166 +BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset,
   1.167 +                                 char* data, int size);
   1.168 +
   1.169 +// Same as above but without seek.
   1.170 +BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file,
   1.171 +                                             char* data, int size);
   1.172 +
   1.173 +// Reads the given number of bytes (or until EOF is reached) starting with the
   1.174 +// given offset, but does not make any effort to read all data on all platforms.
   1.175 +// Returns the number of bytes read, or -1 on error.
   1.176 +BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset,
   1.177 +                                             char* data, int size);
   1.178 +
   1.179 +// Same as above but without seek.
   1.180 +BASE_EXPORT int ReadPlatformFileCurPosNoBestEffort(PlatformFile file,
   1.181 +                                                   char* data, int size);
   1.182 +
   1.183 +// Writes the given buffer into the file at the given offset, overwritting any
   1.184 +// data that was previously there. Returns the number of bytes written, or -1
   1.185 +// on error. Note that this function makes a best effort to write all data on
   1.186 +// all platforms.
   1.187 +// Ignores the offset and writes to the end of the file if the file was opened
   1.188 +// with PLATFORM_FILE_APPEND.
   1.189 +BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset,
   1.190 +                                  const char* data, int size);
   1.191 +
   1.192 +// Save as above but without seek.
   1.193 +BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file,
   1.194 +                                              const char* data, int size);
   1.195 +
   1.196 +// Save as above but does not make any effort to write all data on all
   1.197 +// platforms. Returns the number of bytes written, or -1 on error.
   1.198 +BASE_EXPORT int WritePlatformFileCurPosNoBestEffort(PlatformFile file,
   1.199 +                                                    const char* data, int size);
   1.200 +
   1.201 +// Truncates the given file to the given length. If |length| is greater than
   1.202 +// the current size of the file, the file is extended with zeros. If the file
   1.203 +// doesn't exist, |false| is returned.
   1.204 +BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length);
   1.205 +
   1.206 +// Flushes the buffers of the given file.
   1.207 +BASE_EXPORT bool FlushPlatformFile(PlatformFile file);
   1.208 +
   1.209 +// Touches the given file.
   1.210 +BASE_EXPORT bool TouchPlatformFile(PlatformFile file,
   1.211 +                                   const Time& last_access_time,
   1.212 +                                   const Time& last_modified_time);
   1.213 +
   1.214 +// Returns some information for the given file.
   1.215 +BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info);
   1.216 +
   1.217 +// Use this class to pass ownership of a PlatformFile to a receiver that may or
   1.218 +// may not want to accept it.  This class does not own the storage for the
   1.219 +// PlatformFile.
   1.220 +//
   1.221 +// EXAMPLE:
   1.222 +//
   1.223 +//  void MaybeProcessFile(PassPlatformFile pass_file) {
   1.224 +//    if (...) {
   1.225 +//      PlatformFile file = pass_file.ReleaseValue();
   1.226 +//      // Now, we are responsible for closing |file|.
   1.227 +//    }
   1.228 +//  }
   1.229 +//
   1.230 +//  void OpenAndMaybeProcessFile(const FilePath& path) {
   1.231 +//    PlatformFile file = CreatePlatformFile(path, ...);
   1.232 +//    MaybeProcessFile(PassPlatformFile(&file));
   1.233 +//    if (file != kInvalidPlatformFileValue)
   1.234 +//      ClosePlatformFile(file);
   1.235 +//  }
   1.236 +//
   1.237 +class BASE_EXPORT PassPlatformFile {
   1.238 + public:
   1.239 +  explicit PassPlatformFile(PlatformFile* value) : value_(value) {
   1.240 +  }
   1.241 +
   1.242 +  // Called to retrieve the PlatformFile stored in this object.  The caller
   1.243 +  // gains ownership of the PlatformFile and is now responsible for closing it.
   1.244 +  // Any subsequent calls to this method will return an invalid PlatformFile.
   1.245 +  PlatformFile ReleaseValue() {
   1.246 +    PlatformFile temp = *value_;
   1.247 +    *value_ = kInvalidPlatformFileValue;
   1.248 +    return temp;
   1.249 +  }
   1.250 +
   1.251 + private:
   1.252 +  PlatformFile* value_;
   1.253 +};
   1.254 +
   1.255 +}  // namespace base
   1.256 +
   1.257 +#endif  // BASE_PLATFORM_FILE_H_

mercurial