13f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef BASE_PLATFORM_FILE_H_
6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define BASE_PLATFORM_FILE_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "build/build_config.h"
10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <windows.h>
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string>
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
16ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/base_api.h"
17ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/basictypes.h"
18ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/file_path.h"
19ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/time.h"
20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace base {
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttypedef HANDLE PlatformFile;
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottconst PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX)
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttypedef int PlatformFile;
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottconst PlatformFile kInvalidPlatformFileValue = -1;
29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottenum PlatformFileFlags {
32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_OPEN = 1,
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_CREATE = 2,
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_OPEN_ALWAYS = 4,    // May create a new file.
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_CREATE_ALWAYS = 8,  // May overwrite an old file.
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_READ = 16,
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_WRITE = 32,
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_EXCLUSIVE_READ = 64,  // EXCLUSIVE is opposite of Windows SHARE
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_EXCLUSIVE_WRITE = 128,
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_ASYNC = 256,
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_TEMPORARY = 512,        // Used on Windows only
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  PLATFORM_FILE_HIDDEN = 1024,          // Used on Windows only
433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_DELETE_ON_CLOSE = 2048,
443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_TRUNCATE = 4096,
45ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  PLATFORM_FILE_WRITE_ATTRIBUTES = 8192,  // Used on Windows only
46ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  PLATFORM_FILE_ENUMERATE = 16384,  // May enumerate directory
473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick};
483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
493345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of
503345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a
513345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// browser policy doesn't allow the operation to be executed.
523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickenum PlatformFileError {
533345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_OK = 0,
543345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_FAILED = -1,
553345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_IN_USE = -2,
563345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_EXISTS = -3,
573345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_NOT_FOUND = -4,
583345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_ACCESS_DENIED = -5,
593345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6,
603345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_NO_MEMORY = -7,
613345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_NO_SPACE = -8,
623345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9,
633345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_INVALID_OPERATION = -10,
643345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PLATFORM_FILE_ERROR_SECURITY = -11,
65731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  PLATFORM_FILE_ERROR_ABORT = -12,
66731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  PLATFORM_FILE_ERROR_NOT_A_FILE = -13,
67731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  PLATFORM_FILE_ERROR_NOT_EMPTY = -14,
68ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  PLATFORM_FILE_ERROR_INVALID_URL = -15,
693345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick};
703345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
713345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Used to hold information about a given file.
723345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// If you add more fields to this structure (platform-specific fields are OK),
733345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// make sure to update all functions that use it in file_util_{win|posix}.cc
743345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// too, and the ParamTraits<base::PlatformFileInfo> implementation in
753345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// chrome/common/common_param_traits.cc.
76ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstruct BASE_API PlatformFileInfo {
773f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  PlatformFileInfo();
783f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  ~PlatformFileInfo();
793f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen
803345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // The size of the file in bytes.  Undefined when is_directory is true.
813345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  int64 size;
823345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
833345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // True if the file corresponds to a directory.
843345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool is_directory;
853345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
86201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  // True if the file corresponds to a symbolic link.
87201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  bool is_symbolic_link;
88201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
893345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // The last modified time of a file.
903345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  base::Time last_modified;
913345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
923345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // The last accessed time of a file.
933345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  base::Time last_accessed;
943345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // The creation time of a file.
963345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  base::Time creation_time;
97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Creates or opens the given file. If PLATFORM_FILE_OPEN_ALWAYS is used, and
100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// |created| is provided, |created| will be set to true if the file was created
1013345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// or to false in case the file was just opened. |error_code| can be NULL.
102ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API PlatformFile CreatePlatformFile(const FilePath& name,
103ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                         int flags,
104ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                         bool* created,
105ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                         PlatformFileError* error_code);
106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
1073345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Closes a file handle. Returns |true| on success and |false| otherwise.
108ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool ClosePlatformFile(PlatformFile file);
109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
1103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Reads the given number of bytes (or until EOF is reached) starting with the
1113345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// given offset. Returns the number of bytes read, or -1 on error.
112ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int ReadPlatformFile(PlatformFile file, int64 offset,
113ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                              char* data, int size);
1143345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1153345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Writes the given buffer into the file at the given offset, overwritting any
1163345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// data that was previously there. Returns the number of bytes written, or -1
1173345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// on error.
118ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int WritePlatformFile(PlatformFile file, int64 offset,
119ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                               const char* data, int size);
1203345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1213345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Truncates the given file to the given length. If |length| is greater than
1223345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// the current size of the file, the file is extended with zeros. If the file
1233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// doesn't exist, |false| is returned.
124ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool TruncatePlatformFile(PlatformFile file, int64 length);
1253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1263345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Flushes the buffers of the given file.
127ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool FlushPlatformFile(PlatformFile file);
1283345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1293345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Touches the given file.
130ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool TouchPlatformFile(PlatformFile file, const Time& last_access_time,
131ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                const Time& last_modified_time);
1323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Returns some information for the given file.
134ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info);
1353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1363345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Use this class to pass ownership of a PlatformFile to a receiver that may or
1373345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// may not want to accept it.  This class does not own the storage for the
1383345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// PlatformFile.
1393345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//
1403345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// EXAMPLE:
1413345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//
1423345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//  void MaybeProcessFile(PassPlatformFile pass_file) {
1433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//    if (...) {
1443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//      PlatformFile file = pass_file.ReleaseValue();
1453345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//      // Now, we are responsible for closing |file|.
1463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//    }
1473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//  }
1483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//
1493345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//  void OpenAndMaybeProcessFile(const FilePath& path) {
1503345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//    PlatformFile file = CreatePlatformFile(path, ...);
1513345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//    MaybeProcessFile(PassPlatformFile(&file));
1523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//    if (file != kInvalidPlatformFileValue)
1533345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//      ClosePlatformFile(file);
1543345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//  }
1553345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//
156ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API PassPlatformFile {
1573345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick public:
1583345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  explicit PassPlatformFile(PlatformFile* value) : value_(value) {
1593345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  }
1603345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1613345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Called to retrieve the PlatformFile stored in this object.  The caller
1623345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // gains ownership of the PlatformFile and is now responsible for closing it.
1633345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Any subsequent calls to this method will return an invalid PlatformFile.
1643345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PlatformFile ReleaseValue() {
1653345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    PlatformFile temp = *value_;
1663345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    *value_ = kInvalidPlatformFileValue;
1673345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    return temp;
1683345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  }
1693345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1703345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick private:
1713345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  PlatformFile* value_;
1723345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick};
1733345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}  // namespace base
175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // BASE_PLATFORM_FILE_H_
177