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