15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_PLATFORM_FILE_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_PLATFORM_FILE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// exactly one of the five (possibly combining with other flags) when opening
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or creating a file.
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// PLATFORM_FILE_(WRITE|APPEND) are mutually exclusive. This is so that APPEND
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// behavior will be consistent with O_APPEND on POSIX.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum PlatformFileFlags {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_OPEN = 1 << 0,             // Opens a file, only if it exists.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_CREATE = 1 << 1,           // Creates a new file, only if it
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           // does not already exist.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_OPEN_ALWAYS = 1 << 2,      // May create a new file.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_CREATE_ALWAYS = 1 << 3,    // May overwrite an old file.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_OPEN_TRUNCATED = 1 << 4,   // Opens a file and truncates it,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           // only if it exists.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_READ = 1 << 5,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_WRITE = 1 << 6,
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_APPEND = 1 << 7,
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_EXCLUSIVE_READ = 1 << 8,   // EXCLUSIVE is opposite of Windows
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           // SHARE
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_EXCLUSIVE_WRITE = 1 << 9,
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_ASYNC = 1 << 10,
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_TEMPORARY = 1 << 11,       // Used on Windows only
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_HIDDEN = 1 << 12,          // Used on Windows only
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_DELETE_ON_CLOSE = 1 << 13,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_WRITE_ATTRIBUTES = 1 << 14,  // Used on Windows only
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_ENUMERATE = 1 << 15,         // May enumerate directory
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_SHARE_DELETE = 1 << 16,      // Used on Windows only
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_TERMINAL_DEVICE = 1 << 17,   // Serial port flags
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_BACKUP_SEMANTICS = 1 << 18,  // Used on Windows only
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  PLATFORM_FILE_EXECUTE = 1 << 19,           // Used on Windows only
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// browser policy doesn't allow the operation to be executed.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum PlatformFileError {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_OK = 0,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_FAILED = -1,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_IN_USE = -2,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_EXISTS = -3,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_NOT_FOUND = -4,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_ACCESS_DENIED = -5,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_NO_MEMORY = -7,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_NO_SPACE = -8,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_INVALID_OPERATION = -10,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_SECURITY = -11,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_ABORT = -12,
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_NOT_A_FILE = -13,
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_NOT_EMPTY = -14,
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_ERROR_INVALID_URL = -15,
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PLATFORM_FILE_ERROR_IO = -16,
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Put new entries here and increment PLATFORM_FILE_ERROR_MAX.
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PLATFORM_FILE_ERROR_MAX = -17
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum PlatformFileWhence {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_FROM_BEGIN   = 0,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_FROM_CURRENT = 1,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PLATFORM_FILE_FROM_END     = 2
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Used to hold information about a given file.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If you add more fields to this structure (platform-specific fields are OK),
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// make sure to update all functions that use it in file_util_{win|posix}.cc
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// too, and the ParamTraits<base::PlatformFileInfo> implementation in
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// chrome/common/common_param_traits.cc.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct BASE_EXPORT PlatformFileInfo {
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformFileInfo();
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~PlatformFileInfo();
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The size of the file in bytes.  Undefined when is_directory is true.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 size;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if the file corresponds to a directory.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_directory;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if the file corresponds to a symbolic link.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_symbolic_link;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The last modified time of a file.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time last_modified;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The last accessed time of a file.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time last_accessed;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The creation time of a file.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time creation_time;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
117b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#if defined(OS_WIN)
118b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)typedef HANDLE PlatformFile;
119b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
120b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)PlatformFileError LastErrorToPlatformFileError(DWORD saved_errno);
121b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#elif defined(OS_POSIX)
122b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)typedef int PlatformFile;
123b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)const PlatformFile kInvalidPlatformFileValue = -1;
124b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)PlatformFileError ErrnoToPlatformFileError(int saved_errno);
125b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#endif
126b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Creates or opens the given file. If |created| is provided, it will be set to
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// true if a new file was created [or an old one truncated to zero length to
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// false otherwise.  |error| can be NULL.
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This function fails with 'access denied' if the |name| contains path
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// traversal ('..') components.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name,
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            int flags,
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            bool* created,
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                            PlatformFileError* error);
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Same as CreatePlatformFile but allows paths with traversal (like \..\)
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// components. Use only with extreme care.
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT PlatformFile CreatePlatformFileUnsafe(const FilePath& name,
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                  int flags,
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                  bool* created,
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                  PlatformFileError* error);
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)BASE_EXPORT FILE* FdopenPlatformFile(PlatformFile file, const char* mode);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Closes a file handle. Returns |true| on success and |false| otherwise.
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool ClosePlatformFile(PlatformFile file);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Changes current position in the file to an |offset| relative to an origin
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// defined by |whence|. Returns the resultant current position in the file
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (relative to the start) or -1 in case of error.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int64 SeekPlatformFile(PlatformFile file,
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   PlatformFileWhence whence,
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int64 offset);
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Reads the given number of bytes (or until EOF is reached) starting with the
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// given offset. Returns the number of bytes read, or -1 on error. Note that
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this function makes a best effort to read all data on all platforms, so it is
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not intended for stream oriented files but instead for cases when the normal
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// expectation is that actually |size| bytes are read unless there is an error.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset,
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 char* data, int size);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Same as above but without seek.
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file,
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             char* data, int size);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Reads the given number of bytes (or until EOF is reached) starting with the
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// given offset, but does not make any effort to read all data on all platforms.
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the number of bytes read, or -1 on error.
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset,
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             char* data, int size);
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Same as above but without seek.
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int ReadPlatformFileCurPosNoBestEffort(PlatformFile file,
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                   char* data, int size);
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Writes the given buffer into the file at the given offset, overwritting any
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// data that was previously there. Returns the number of bytes written, or -1
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// on error. Note that this function makes a best effort to write all data on
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// all platforms.
184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Ignores the offset and writes to the end of the file if the file was opened
185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// with PLATFORM_FILE_APPEND.
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset,
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const char* data, int size);
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Save as above but without seek.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file,
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                              const char* data, int size);
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Save as above but does not make any effort to write all data on all
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platforms. Returns the number of bytes written, or -1 on error.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT int WritePlatformFileCurPosNoBestEffort(PlatformFile file,
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                    const char* data, int size);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Truncates the given file to the given length. If |length| is greater than
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the current size of the file, the file is extended with zeros. If the file
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// doesn't exist, |false| is returned.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Flushes the buffers of the given file.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool FlushPlatformFile(PlatformFile file);
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Touches the given file.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool TouchPlatformFile(PlatformFile file,
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const Time& last_access_time,
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const Time& last_modified_time);
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns some information for the given file.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info);
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use this class to pass ownership of a PlatformFile to a receiver that may or
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// may not want to accept it.  This class does not own the storage for the
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PlatformFile.
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// EXAMPLE:
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  void MaybeProcessFile(PassPlatformFile pass_file) {
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    if (...) {
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      PlatformFile file = pass_file.ReleaseValue();
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      // Now, we are responsible for closing |file|.
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    }
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  }
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  void OpenAndMaybeProcessFile(const FilePath& path) {
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    PlatformFile file = CreatePlatformFile(path, ...);
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    MaybeProcessFile(PassPlatformFile(&file));
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    if (file != kInvalidPlatformFileValue)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      ClosePlatformFile(file);
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  }
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT PassPlatformFile {
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit PassPlatformFile(PlatformFile* value) : value_(value) {
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called to retrieve the PlatformFile stored in this object.  The caller
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // gains ownership of the PlatformFile and is now responsible for closing it.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Any subsequent calls to this method will return an invalid PlatformFile.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformFile ReleaseValue() {
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PlatformFile temp = *value_;
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    *value_ = kInvalidPlatformFileValue;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return temp;
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PlatformFile* value_;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace base
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_PLATFORM_FILE_H_
255