1 // Copyright 2007, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GEARS_BASE_COMMON_FILE_H__
27 #define GEARS_BASE_COMMON_FILE_H__
30 #include "gears/base/common/common.h"
31 #include "gears/base/common/int_types.h"
32 #include "gears/base/common/string16.h"
34 class SecurityOrigin
; // base/common/security_model.h
38 // Hard Limit on path component length, used for sanity checking.
39 static const size_t kMaxPathComponentChars
;
41 // Creates a new file. If the file already exists, returns false.
42 // Returns true if a new file has been created.
43 static bool CreateNewFile(const char16
*full_filepath
);
45 // Deletes a file. If the file does not exist, returns false.
46 // Returns true if the file was deleted.
47 static bool Delete(const char16
*full_filepath
);
49 // Returns true if the file exists.
50 static bool Exists(const char16
*full_filepath
);
52 // Returns true if the directory exists.
53 static bool DirectoryExists(const char16
*full_dirpath
);
55 // Returns the size of the file. If the file does not exist, or is otherwise
56 // unreadable, returns 0.
57 static int64
GetFileSize(const char16
*full_filepath
);
59 // Reads part of the contents of the file into memory. Returns the number of
60 // bytes read (or zero on failure).
61 static int ReadFileSegmentToBuffer(const char16
*full_filepath
,
66 // Reads the contents of the file into memory. If the file does not exist,
67 // returns false. Returns true on success
68 static bool ReadFileToVector(const char16
*full_filepath
,
69 std::vector
<uint8
> *data
);
71 // Writes the contents of the file. If the file does not exist, returns false.
72 // Existing contents are overwritten. Returns true on success
73 static bool WriteVectorToFile(const char16
*full_filepath
,
74 const std::vector
<uint8
> *data
);
76 // Returns the number of files and directories contained in the given
77 // directory. If the directory does not exist, returns 0.
78 static int GetDirectoryFileCount(const char16
*full_dirpath
);
80 // Returns a pointer to the last '.' within 'filename' if an extension is
81 // found, or a pointer to the trailing NULL otherwise.
82 static const char16
*GetFileExtension(const char16
*filename
);
84 // The basename (last path component) for the given path is returned in the
85 // basename parameter.
87 // Example usage is splitting the filename off the end of a path for the
88 // purpose of sanity checking.
89 // Edge case: if a Windows \\share path is used, 'share' will be returned
90 // and not '\\share' as may be expected.
92 // Returns true if function succeeds.
93 static bool GetBaseName(const std::string16
&path
, std::string16
*basename
);
95 // Gets the parent directory for a path.
97 // * 'path' parameter may not be empty.
98 // * Fails if path doesn't specify a parent directory e.g. 'filename'.
99 static bool GetParentDirectory(const std::string16
&path
,
100 std::string16
*parent
);
102 // Creates a unique file in the system temporary directory. Returns the
103 // full path of the new file in 'path'.
104 // Returns true if the function succeeds. 'path' is unmodified on failure.
105 static bool CreateNewTempFile(std::string16
*full_filepath
);
107 // Creates a unique directory under the system temporary directory. Returns
108 // the full path of the new directory in 'path'.
109 // Returns true if the function succeeds. 'path' is unmodified on failure.
110 static bool CreateNewTempDirectory(std::string16
*full_filepath
);
112 // Ensures all directories along the specified path exist. Any directories
113 // that do not exist will be created. Returns true if the function succeeds.
114 static bool RecursivelyCreateDir(const char16
*full_dirpath
);
116 // Removes the directory and all of its children. If the directory does
117 // not exist, returns false. Returns true if the function succeeds.
118 static bool DeleteRecursively(const char16
*full_dirpath
);
120 // Writes raw data to a file.
121 // If file doesn't exist or an error occurs, false is returned.
122 static bool WriteBytesToFile(const char16
*full_filepath
, const uint8
*data
,
125 // Clears the last file error for the current thread
126 static void ClearLastFileError();
128 // Gets the last file error that occurred on the current thread. If
129 // there was no error, returns false and error_out is cleared.
130 // Note: CreateNewFile is the only method that sets an error message
132 static bool GetLastFileError(std::string16
*error_out
);
135 static void SetLastFileError(const char16
*message
,
136 const char16
*filepath
,
139 static const char16
*kCreateFileFailedMessage
;
142 typedef std::vector
<std::string16
> PathComponents
;
144 // Splits a path into it's individual Components, on windows if a drive
145 // letter is part of the path, then this is the first item in the list e.g.
146 // 'c:\a.txt' -> ['c:', 'a.txt']
148 // This function doesn't handle file shares on Win32, caller must strip them
149 // out before calling this function.
151 // Multiple consecutive path separators are expanded into empty strings, e.g.
152 // 'c:\\a.txt' -> ['c:', '', 'a.txt'] - '\\' becomes ''.
153 static void SplitPath(const std::string16
&path
,
154 PathComponents
*exploded_path
);
160 friend bool TestCollapsePathSeparators();
161 friend bool TestSplitPath();
163 // TODO(miket): someone fix common.h so that it doesn't require a browser
164 // flag to be defined! Or better yet, someone shoot common.h in the head!
165 // DISALLOW_EVIL_CONSTRUCTORS(File);
169 #endif // GEARS_BASE_COMMON_FILE_H__