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// This file contains utility functions for dealing with the local
6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// filesystem.
7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef BASE_FILE_UTIL_H_
9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define BASE_FILE_UTIL_H_
103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "build/build_config.h"
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <windows.h>
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#if defined(UNIT_TEST)
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <aclapi.h>
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX)
20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <sys/stat.h>
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <stdio.h>
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <stack>
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string>
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <vector>
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
29ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/base_api.h"
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/basictypes.h"
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/file_path.h"
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h"
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/platform_file.h"
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/string16.h"
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX)
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/eintr_wrapper.h"
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/file_descriptor_posix.h"
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/logging.h"
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace base {
43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass Time;
44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}
45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottnamespace file_util {
47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//-----------------------------------------------------------------------------
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Functions that operate purely on a path string w/o touching the filesystem:
50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if the given path ends with a path separator character.
52ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool EndsWithSeparator(const FilePath& path);
53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Makes sure that |path| ends with a separator IFF path is a directory that
55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// exists. Returns true if |path| is an existing directory, false otherwise.
56ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool EnsureEndsWithSeparator(FilePath* path);
57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Convert provided relative path into an absolute path.  Returns false on
59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// error. On POSIX, this function fails if the path does not exist.
60ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool AbsolutePath(FilePath* path);
61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if |parent| contains |child|. Both paths are converted to
63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// absolute paths before doing the comparison.
64ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool ContainsPath(const FilePath& parent, const FilePath& child);
65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//-----------------------------------------------------------------------------
67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Functions that involve filesystem access or modification:
68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns the number of files matching the current path that were
70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// created on or after the given |file_time|.  Doesn't count ".." or ".".
71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Note for POSIX environments: a file created before |file_time|
73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// can be mis-detected as a newer file due to low precision of
74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// timestmap of file creation time. If you need to avoid such
75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// mis-detection perfectly, you should wait one second before
76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// obtaining |file_time|.
77ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int CountFilesCreatedAfter(const FilePath& path,
78ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                    const base::Time& file_time);
79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns the total number of bytes used by all the files under |root_path|.
81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// If the path does not exist the function returns 0.
82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// This function is implemented using the FileEnumerator class so it is not
84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// particularly speedy in any platform.
85ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int64 ComputeDirectorySize(const FilePath& root_path);
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns the total number of bytes used by all files matching the provided
88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// |pattern|, on this |directory| (without recursion). If the path does not
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// exist the function returns 0.
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// This function is implemented using the FileEnumerator class so it is not
92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// particularly speedy in any platform.
93ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int64 ComputeFilesSize(const FilePath& directory,
94ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                const FilePath::StringType& pattern);
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Deletes the given path, whether it's a file or a directory.
97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If it's a directory, it's perfectly happy to delete all of the
98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// directory's contents.  Passing true to recursive deletes
99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// subdirectories and their contents as well.
100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if successful, false otherwise.
101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// WARNING: USING THIS WITH recursive==true IS EQUIVALENT
103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//          TO "rm -rf", SO USE WITH CAUTION.
104ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool Delete(const FilePath& path, bool recursive);
105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Schedules to delete the given path, whether it's a file or a directory, until
108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the operating system is restarted.
109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Note:
110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 1) The file/directory to be deleted should exist in a temp folder.
111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 2) The directory to be deleted must be empty.
112ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool DeleteAfterReboot(const FilePath& path);
113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
115c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Moves the given path, whether it's a file or a directory.
116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If a simple rename is not possible, such as in the case where the paths are
117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// on different volumes, this will attempt to copy and delete. Returns
118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// true for success.
119ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool Move(const FilePath& from_path, const FilePath& to_path);
120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Renames file |from_path| to |to_path|. Both paths must be on the same
122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// volume, or the function will fail. Destination file will be created
123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// if it doesn't exist. Prefer this function over Move when dealing with
124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// temporary files. On Windows it preserves attributes of the target file.
125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true on success.
126ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool ReplaceFile(const FilePath& from_path, const FilePath& to_path);
127c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
128c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copies a single file. Use CopyDirectory to copy directories.
129ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CopyFile(const FilePath& from_path, const FilePath& to_path);
130c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
131c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copies the given path, and optionally all subdirectories and their contents
132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// as well.
133c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If there are files existing under to_path, always overwrite.
134c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if successful, false otherwise.
135c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Don't use wildcards on the names, it may stop working without notice.
136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
137c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// If you only need to copy a file use CopyFile, it's faster.
138ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CopyDirectory(const FilePath& from_path, const FilePath& to_path,
139ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                            bool recursive);
140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if the given path exists on the local filesystem,
142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// false otherwise.
143ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool PathExists(const FilePath& path);
144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if the given path is writable by the user, false otherwise.
146ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool PathIsWritable(const FilePath& path);
147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
148c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if the given path exists and is a directory, false otherwise.
149ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool DirectoryExists(const FilePath& path);
150c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
151c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
152c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Gets the creation time of the given file (expressed in the local timezone),
153c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// and returns it via the creation_time parameter.  Returns true if successful,
154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// false otherwise.
155ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetFileCreationLocalTime(const std::wstring& filename,
156ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                       LPSYSTEMTIME creation_time);
157c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
158c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Same as above, but takes a previously-opened file handle instead of a name.
159ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle,
160ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                                 LPSYSTEMTIME creation_time);
161c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // defined(OS_WIN)
162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
163c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if the contents of the two files given are equal, false
164c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// otherwise.  If either file can't be read, returns false.
165ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool ContentsEqual(const FilePath& filename1,
166ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                            const FilePath& filename2);
167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if the contents of the two text files given are equal, false
169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// otherwise.  This routine treats "\r\n" and "\n" as equivalent.
170ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool TextContentsEqual(const FilePath& filename1,
171ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                const FilePath& filename2);
172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Read the file at |path| into |contents|, returning true on success.
174c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// |contents| may be NULL, in which case this function is useful for its
175c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// side effect of priming the disk cache.
176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Useful for unit tests.
177ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool ReadFileToString(const FilePath& path, std::string* contents);
178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX)
180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Read exactly |bytes| bytes from file descriptor |fd|, storing the result
181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// in |buffer|. This function is protected against EINTR and partial reads.
182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true iff |bytes| bytes have been successfuly read from |fd|.
183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool ReadFromFD(int fd, char* buffer, size_t bytes);
184201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
185201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// Creates a symbolic link at |symlink| pointing to |target|.  Returns
186201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// false on failure.
187201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdochbool CreateSymbolicLink(const FilePath& target, const FilePath& symlink);
188201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
189201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// Reads the given |symlink| and returns where it points to in |target|.
190201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// Returns false upon failure.
191201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdochbool ReadSymbolicLink(const FilePath& symlink, FilePath* target);
192c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // defined(OS_POSIX)
193c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
194c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
195c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Resolve Windows shortcut (.LNK file)
196c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This methods tries to resolve a shortcut .LNK file. If the |path| is valid
197c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// returns true and puts the target into the |path|, otherwise returns
198c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// false leaving the path as it is.
199ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool ResolveShortcut(FilePath* path);
200c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
201c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Create a Windows shortcut (.LNK file)
202c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This method creates a shortcut link using the information given. Ensure
203c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// you have initialized COM before calling into this function. 'source'
204c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// and 'destination' parameters are required, everything else can be NULL.
205c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// 'source' is the existing file, 'destination' is the new link file to be
206c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// created; for best results pass the filename with the .lnk extension.
207c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// The 'icon' can specify a dll or exe in which case the icon index is the
208c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// resource id. 'app_id' is the app model id for the shortcut on Win7.
209c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Note that if the shortcut exists it will overwrite it.
210ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CreateShortcutLink(const wchar_t *source,
211ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *destination,
212ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *working_dir,
213ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *arguments,
214ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *description,
215ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *icon,
216ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 int icon_index,
217ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t* app_id);
218c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
219c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Update a Windows shortcut (.LNK file). This method assumes the shortcut
220c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// link already exists (otherwise false is returned). Ensure you have
221c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// initialized COM before calling into this function. Only 'destination'
222c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// parameter is required, everything else can be NULL (but if everything else
223c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// is NULL no changes are made to the shortcut). 'destination' is the link
224c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// file to be updated. 'app_id' is the app model id for the shortcut on Win7.
225c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// For best results pass the filename with the .lnk extension.
226ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool UpdateShortcutLink(const wchar_t *source,
227ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *destination,
228ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *working_dir,
229ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *arguments,
230ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *description,
231ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t *icon,
232ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 int icon_index,
233ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                 const wchar_t* app_id);
234c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
235c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Pins a shortcut to the Windows 7 taskbar. The shortcut file must already
236c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// exist and be a shortcut that points to an executable.
237ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool TaskbarPinShortcutLink(const wchar_t* shortcut);
238c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
239c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Unpins a shortcut from the Windows 7 taskbar. The shortcut must exist and
240c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// already be pinned to the taskbar.
241ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool TaskbarUnpinShortcutLink(const wchar_t* shortcut);
242c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
243c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copy from_path to to_path recursively and then delete from_path recursively.
244c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns true if all operations succeed.
245c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This function simulates Move(), but unlike Move() it works across volumes.
246c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This fuction is not transactional.
247ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CopyAndDeleteDirectory(const FilePath& from_path,
248ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                     const FilePath& to_path);
249c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // defined(OS_WIN)
250c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
251c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Return true if the given directory is empty
252ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool IsDirectoryEmpty(const FilePath& dir_path);
253c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
254c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Get the temporary directory provided by the system.
2553345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// WARNING: DON'T USE THIS. If you want to create a temporary file, use one of
2563345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// the functions below.
257ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetTempDir(FilePath* path);
258c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Get a temporary directory for shared memory files.
259c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Only useful on POSIX; redirects to GetTempDir() on Windows.
260ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetShmemTempDir(FilePath* path);
261c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
262c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Get the home directory.  This is more complicated than just getenv("HOME")
263c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// as it knows to fall back on getpwent() etc.
264ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API FilePath GetHomeDir();
265c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
266c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Creates a temporary file. The full path is placed in |path|, and the
267c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// function returns true if was successful in creating the file. The file will
268c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// be empty and all handles closed after this function returns.
269ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CreateTemporaryFile(FilePath* path);
270c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
2713345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Same as CreateTemporaryFile but the file is created in |dir|.
272ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CreateTemporaryFileInDir(const FilePath& dir,
273ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                       FilePath* temp_file);
2743345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
275c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Create and open a temporary file.  File is opened for read/write.
276c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The full path is placed in |path|.
277c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns a handle to the opened file or NULL if an error occured.
278ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API FILE* CreateAndOpenTemporaryFile(FilePath* path);
279c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Like above but for shmem files.  Only useful for POSIX.
280ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API FILE* CreateAndOpenTemporaryShmemFile(FilePath* path);
281c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
282ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir,
283ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                               FilePath* path);
284c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
2853345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Create a new directory. If prefix is provided, the new directory name is in
2863345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// the format of prefixyyyy.
2873345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// NOTE: prefix is ignored in the POSIX implementation.
2883345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// If success, return true and output the full path of the directory created.
289ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CreateNewTempDirectory(const FilePath::StringType& prefix,
290ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                     FilePath* new_temp_path);
291c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
292c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Create a directory within another directory.
2933345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Extra characters will be appended to |prefix| to ensure that the
294c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// new directory does not have the same name as an existing directory.
295ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CreateTemporaryDirInDir(const FilePath& base_dir,
296ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                      const FilePath::StringType& prefix,
297ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                      FilePath* new_dir);
298c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
299c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Creates a directory, as well as creating any parent directories, if they
300c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// don't exist. Returns 'true' on successful creation, or if the directory
301c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// already exists.  The directory is only readable by the current user.
302ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CreateDirectory(const FilePath& full_path);
303c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
304c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns the file size. Returns true on success.
305ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetFileSize(const FilePath& file_path, int64* file_size);
306c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
307c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns true if the given path's base name is ".".
308ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool IsDot(const FilePath& path);
309c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
310c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Returns true if the given path's base name is "..".
311ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool IsDotDot(const FilePath& path);
312c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
313c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Sets |real_path| to |path| with symbolic links and junctions expanded.
314c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// On windows, make sure the path starts with a lettered drive.
315c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// |path| must reference a file.  Function will fail if |path| points to
316c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// a directory or to a nonexistent path.  On windows, this function will
317c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// fail if |path| is a junction or symlink that points to an empty file,
318c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// or if |real_path| would be longer than MAX_PATH characters.
319ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool NormalizeFilePath(const FilePath& path, FilePath* real_path);
320c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
3213345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#if defined(OS_WIN)
3223345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Given an existing file in |path|, it returns in |real_path| the path
3233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// in the native NT format, of the form "\Device\HarddiskVolumeXX\..".
3243345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Returns false it it fails. Empty files cannot be resolved with this
3253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// function.
326ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool NormalizeToNativeFilePath(const FilePath& path,
327ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                        FilePath* nt_path);
3283345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#endif
329c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
330c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns information about the given file path.
331ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetFileInfo(const FilePath& file_path,
332ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                          base::PlatformFileInfo* info);
3333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
3343345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Sets the time of the last access and the time of the last modification.
335ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool TouchFile(const FilePath& path,
336ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                        const base::Time& last_accessed,
337ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                        const base::Time& last_modified);
338c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
339c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Set the time of the last modification. Useful for unit tests.
340ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool SetLastModifiedTime(const FilePath& path,
341ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                  const base::Time& last_modified);
342c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
343c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX)
344c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Store inode number of |path| in |inode|. Return true on success.
345c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottbool GetInode(const FilePath& path, ino_t* inode);
346c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
347c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
348c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
349ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API FILE* OpenFile(const FilePath& filename, const char* mode);
350c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
351c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Closes file opened by OpenFile. Returns true on success.
352ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool CloseFile(FILE* file);
353c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
354c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Truncates an open file to end at the location of the current file pointer.
355c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This is a cross-platform analog to Windows' SetEndOfFile() function.
356ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool TruncateFile(FILE* file);
357c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
358c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Reads the given number of bytes from the file into the buffer.  Returns
359c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the number of read bytes, or -1 on error.
360ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int ReadFile(const FilePath& filename, char* data, int size);
361c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
362c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Writes the given buffer into the file, overwriting any data that was
363c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// previously there.  Returns the number of bytes written, or -1 on error.
364ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API int WriteFile(const FilePath& filename, const char* data, int size);
365c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX)
366c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Append the data to |fd|. Does not close |fd| when done.
367c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottint WriteFileDescriptor(const int fd, const char* data, int size);
368c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
369c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
370c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Gets the current working directory for the process.
371ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool GetCurrentDirectory(FilePath* path);
372c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
373c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Sets the current working directory for the process.
374ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool SetCurrentDirectory(const FilePath& path);
375c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
376c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// A class to handle auto-closing of FILE*'s.
377c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass ScopedFILEClose {
378c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
379c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  inline void operator()(FILE* x) const {
380c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    if (x) {
381c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott      fclose(x);
382c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    }
383c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
384c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
385c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
386c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttypedef scoped_ptr_malloc<FILE, ScopedFILEClose> ScopedFILE;
387c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
388c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX)
389c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// A class to handle auto-closing of FDs.
390c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass ScopedFDClose {
391c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
392c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  inline void operator()(int* x) const {
393c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    if (x && *x >= 0) {
394c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      if (HANDLE_EINTR(close(*x)) < 0)
395c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch        PLOG(ERROR) << "close";
396c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    }
397c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
398c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
399c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
400c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scotttypedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD;
401c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // OS_POSIX
402c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
403c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// A class for enumerating the files in a provided path. The order of the
404c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// results is not guaranteed.
405c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
406c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// DO NOT USE FROM THE MAIN THREAD of your application unless it is a test
407c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// program where latency does not matter. This class is blocking.
408ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API FileEnumerator {
409c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
410c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
411c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  typedef WIN32_FIND_DATA FindInfo;
412c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX)
413c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  typedef struct {
414c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    struct stat stat;
415c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    std::string filename;
416c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  } FindInfo;
417c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
418c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
419c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  enum FILE_TYPE {
420c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    FILES                 = 1 << 0,
421c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    DIRECTORIES           = 1 << 1,
422c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    INCLUDE_DOT_DOT       = 1 << 2,
423c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_POSIX)
424c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    SHOW_SYM_LINKS        = 1 << 4,
425c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
426c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  };
427c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
428c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // |root_path| is the starting directory to search for. It may or may not end
429c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // in a slash.
430c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  //
431c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If |recursive| is true, this will enumerate all matches in any
432c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // subdirectories matched as well. It does a breadth-first search, so all
433c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // files in one directory will be returned before any files in a
434c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // subdirectory.
435c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  //
436c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // |file_type| specifies whether the enumerator should match files,
437c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // directories, or both.
438c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  //
439c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // |pattern| is an optional pattern for which files to match. This
440c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // works like shell globbing. For example, "*.txt" or "Foo???.doc".
441c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // However, be careful in specifying patterns that aren't cross platform
442c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // since the underlying code uses OS-specific matching routines.  In general,
443c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Windows matching is less featureful than others, so test there first.
444c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // If unspecified, this will match all files.
445c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // NOTE: the pattern only matches the contents of root_path, not files in
446c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // recursive subdirectories.
447c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // TODO(erikkay): Fix the pattern matching to work at all levels.
448c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  FileEnumerator(const FilePath& root_path,
449c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                 bool recursive,
450c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                 FileEnumerator::FILE_TYPE file_type);
451c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  FileEnumerator(const FilePath& root_path,
452c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                 bool recursive,
453c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                 FileEnumerator::FILE_TYPE file_type,
454c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                 const FilePath::StringType& pattern);
455c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ~FileEnumerator();
456c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
457c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns an empty string if there are no more results.
458c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  FilePath Next();
459c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
460c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Write the file info into |info|.
461c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void GetFindInfo(FindInfo* info);
462c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
463c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Looks inside a FindInfo and determines if it's a directory.
464c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static bool IsDirectory(const FindInfo& info);
465c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
466c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static FilePath GetFilename(const FindInfo& find_info);
467c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
468c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
469c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Returns true if the given path should be skipped in enumeration.
470c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool ShouldSkip(const FilePath& path);
471c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
472c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
473c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
474731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  // True when find_data_ is valid.
475731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  bool has_find_data_;
476c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  WIN32_FIND_DATA find_data_;
477c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  HANDLE find_handle_;
478c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#elif defined(OS_POSIX)
4793f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  struct DirectoryEntryInfo {
480c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    FilePath filename;
481c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    struct stat stat;
4823f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen  };
483c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
484c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Read the filenames in source into the vector of DirectoryEntryInfo's
485c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries,
486c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                            const FilePath& source, bool show_links);
487c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
488c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // The files in the current directory
489c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  std::vector<DirectoryEntryInfo> directory_entries_;
490c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
491c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // The next entry to use from the directory_entries_ vector
492c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  size_t current_directory_entry_;
493c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
494c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
495c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  FilePath root_path_;
496c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool recursive_;
497c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  FILE_TYPE file_type_;
498c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  FilePath::StringType pattern_;  // Empty when we want to find everything.
499c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
500c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // A stack that keeps track of which subdirectories we still need to
501c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // enumerate in the breadth-first search.
502c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  std::stack<FilePath> pending_paths_;
503c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
504c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(FileEnumerator);
505c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
506c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
507ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass BASE_API MemoryMappedFile {
508c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
509c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // The default constructor sets all members to invalid/null values.
510c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  MemoryMappedFile();
511c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ~MemoryMappedFile();
512c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
513c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Opens an existing file and maps it into memory. Access is restricted to
514c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // read only. If this object already points to a valid memory mapped file
515c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // then this method will fail and return false. If it cannot open the file,
516c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // the file does not exist, or the memory mapping fails, it will return false.
517c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Later we may want to allow the user to specify access.
518c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool Initialize(const FilePath& file_name);
519c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // As above, but works with an already-opened file. MemoryMappedFile will take
520c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // ownership of |file| and close it when done.
521c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool Initialize(base::PlatformFile file);
522c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
523dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#if defined(OS_WIN)
524dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Opens an existing file and maps it as an image section. Please refer to
525dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // the Initialize function above for additional information.
526dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  bool InitializeAsImageSection(const FilePath& file_name);
527dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#endif  // OS_WIN
528dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
529c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  const uint8* data() const { return data_; }
530c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  size_t length() const { return length_; }
531c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
532c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Is file_ a valid file handle that points to an open, memory mapped file?
533dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  bool IsValid() const;
534c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
535c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
536c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Open the given file and pass it to MapFileToMemoryInternal().
537c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool MapFileToMemory(const FilePath& file_name);
538c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
539c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Map the file to memory, set data_ to that memory address. Return true on
540c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // success, false on any kind of failure. This is a helper for Initialize().
541c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  bool MapFileToMemoryInternal();
542c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
543c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Closes all open handles. Later we may want to make this public.
544c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void CloseHandles();
545c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
546c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#if defined(OS_WIN)
547dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // MapFileToMemoryInternal calls this function. It provides the ability to
548dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // pass in flags which control the mapped section.
549dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  bool MapFileToMemoryInternalEx(int flags);
550dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
551c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  HANDLE file_mapping_;
552c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif
553dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  base::PlatformFile file_;
554c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  uint8* data_;
555c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  size_t length_;
556c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
557c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  DISALLOW_COPY_AND_ASSIGN(MemoryMappedFile);
558c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
559c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
560c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Renames a file using the SHFileOperation API to ensure that the target file
561c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// gets the correct default security descriptor in the new path.
562ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool RenameFileAndResetSecurityDescriptor(
563c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const FilePath& source_file_path,
564c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    const FilePath& target_file_path);
565c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
566c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Returns whether the file has been modified since a particular date.
567ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool HasFileBeenModifiedSince(
568ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    const FileEnumerator::FindInfo& find_info,
569ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    const base::Time& cutoff_time);
570c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
571c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifdef UNIT_TEST
572c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
573c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochinline bool MakeFileUnreadable(const FilePath& path) {
574c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#if defined(OS_POSIX)
575c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  struct stat stat_buf;
576c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  if (stat(path.value().c_str(), &stat_buf) != 0)
577c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return false;
578c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  stat_buf.st_mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
579c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
580c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return chmod(path.value().c_str(), stat_buf.st_mode) == 0;
581c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
582c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#elif defined(OS_WIN)
583c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  PACL old_dacl;
584c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  PSECURITY_DESCRIPTOR security_descriptor;
585c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
586c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           SE_FILE_OBJECT,
587c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl,
588c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           NULL, &security_descriptor) != ERROR_SUCCESS)
589c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return false;
590c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
591c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Deny Read access for the current user.
592c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  EXPLICIT_ACCESS change;
593c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  change.grfAccessPermissions = GENERIC_READ;
594c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  change.grfAccessMode = DENY_ACCESS;
595c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  change.grfInheritance = 0;
596c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  change.Trustee.pMultipleTrustee = NULL;
597c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
598c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  change.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
599c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  change.Trustee.TrusteeType = TRUSTEE_IS_USER;
600c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  change.Trustee.ptstrName = L"CURRENT_USER";
601c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
602c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  PACL new_dacl;
603c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) {
604c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    LocalFree(security_descriptor);
605c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return false;
606c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
607c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
608c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
609c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                  SE_FILE_OBJECT, DACL_SECURITY_INFORMATION,
610c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                  NULL, NULL, new_dacl, NULL);
611c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  LocalFree(security_descriptor);
612c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  LocalFree(new_dacl);
613c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
614c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return rc == ERROR_SUCCESS;
615c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#else
616c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  NOTIMPLEMENTED();
617c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return false;
618c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif
619c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
620c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
621c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // UNIT_TEST
622c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
623c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#if defined(OS_WIN)
624c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Loads the file passed in as an image section and touches pages to avoid
625c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // subsequent hard page faults during LoadLibrary. The size to be pre read
626c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // is passed in. If it is 0 then the whole file is paged in. The step size
627c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // which indicates the number of bytes to skip after every page touched is
628c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // also passed in.
629ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool BASE_API PreReadImage(const wchar_t* file_path, size_t size_to_read,
630ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                           size_t step_size);
631c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // OS_WIN
6323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
6333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#if defined(OS_LINUX)
6343345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Broad categories of file systems as returned by statfs() on Linux.
6353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickenum FileSystemType {
6363345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_UNKNOWN,  // statfs failed.
6373345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_0,        // statfs.f_type == 0 means unknown, may indicate AFS.
6383345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_ORDINARY,       // on-disk filesystem like ext2
6393345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_NFS,
6403345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_SMB,
6413345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_CODA,
6423345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_MEMORY,         // in-memory file system
643ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  FILE_SYSTEM_CGROUP,         // cgroup control.
6443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_OTHER,          // any other value.
6453345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  FILE_SYSTEM_TYPE_COUNT
6463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick};
6473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
6483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Attempts determine the FileSystemType for |path|.
6493345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Returns false if |path| doesn't exist.
6503345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickbool GetFileSystemType(const FilePath& path, FileSystemType* type);
6513345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#endif
6523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
653c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott}  // namespace file_util
654c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
655c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Deprecated functions have been moved to this separate header file,
656c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// which must be included last after all the above definitions.
657c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/file_util_deprecated.h"
658c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
659c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // BASE_FILE_UTIL_H_
660