Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: base/file_util_win.cc

Issue 99923002: Move temp file functions to base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util_unittest.cc ('k') | base/files/file_util_proxy.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <psapi.h> 8 #include <psapi.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 // trailing slash. We duplicate this here, but it shouldn't be necessary 224 // trailing slash. We duplicate this here, but it shouldn't be necessary
225 // when everyone is using the appropriate FilePath APIs. 225 // when everyone is using the appropriate FilePath APIs.
226 *path = FilePath(temp_path).StripTrailingSeparators(); 226 *path = FilePath(temp_path).StripTrailingSeparators();
227 return true; 227 return true;
228 } 228 }
229 229
230 bool GetShmemTempDir(bool executable, FilePath* path) { 230 bool GetShmemTempDir(bool executable, FilePath* path) {
231 return GetTempDir(path); 231 return GetTempDir(path);
232 } 232 }
233 233
234 } // namespace base
235
236 // -----------------------------------------------------------------------------
237
238 namespace file_util {
239
240 using base::DirectoryExists;
241 using base::FilePath;
242 using base::kFileShareAll;
243
244 bool CreateTemporaryFile(FilePath* path) { 234 bool CreateTemporaryFile(FilePath* path) {
245 base::ThreadRestrictions::AssertIOAllowed(); 235 ThreadRestrictions::AssertIOAllowed();
246 236
247 FilePath temp_file; 237 FilePath temp_file;
248 238
249 if (!GetTempDir(path)) 239 if (!GetTempDir(path))
250 return false; 240 return false;
251 241
252 if (CreateTemporaryFileInDir(*path, &temp_file)) { 242 if (CreateTemporaryFileInDir(*path, &temp_file)) {
253 *path = temp_file; 243 *path = temp_file;
254 return true; 244 return true;
255 } 245 }
256 246
257 return false; 247 return false;
258 } 248 }
259 249
260 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) { 250 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path, bool executable) {
261 base::ThreadRestrictions::AssertIOAllowed(); 251 ThreadRestrictions::AssertIOAllowed();
262 return CreateAndOpenTemporaryFile(path); 252 return CreateAndOpenTemporaryFile(path);
263 } 253 }
264 254
265 // On POSIX we have semantics to create and open a temporary file 255 // On POSIX we have semantics to create and open a temporary file
266 // atomically. 256 // atomically.
267 // TODO(jrg): is there equivalent call to use on Windows instead of 257 // TODO(jrg): is there equivalent call to use on Windows instead of
268 // going 2-step? 258 // going 2-step?
269 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { 259 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
270 base::ThreadRestrictions::AssertIOAllowed(); 260 ThreadRestrictions::AssertIOAllowed();
271 if (!CreateTemporaryFileInDir(dir, path)) { 261 if (!CreateTemporaryFileInDir(dir, path)) {
272 return NULL; 262 return NULL;
273 } 263 }
274 // Open file in binary mode, to avoid problems with fwrite. On Windows 264 // Open file in binary mode, to avoid problems with fwrite. On Windows
275 // it replaces \n's with \r\n's, which may surprise you. 265 // it replaces \n's with \r\n's, which may surprise you.
276 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx 266 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx
277 return OpenFile(*path, "wb+"); 267 return file_util::OpenFile(*path, "wb+");
278 } 268 }
279 269
280 bool CreateTemporaryFileInDir(const FilePath& dir, 270 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
281 FilePath* temp_file) { 271 ThreadRestrictions::AssertIOAllowed();
282 base::ThreadRestrictions::AssertIOAllowed();
283 272
284 wchar_t temp_name[MAX_PATH + 1]; 273 wchar_t temp_name[MAX_PATH + 1];
285 274
286 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { 275 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) {
287 DPLOG(WARNING) << "Failed to get temporary file name in " << dir.value(); 276 DPLOG(WARNING) << "Failed to get temporary file name in "
277 << UTF16ToUTF8(dir.value());
288 return false; 278 return false;
289 } 279 }
290 280
291 wchar_t long_temp_name[MAX_PATH + 1]; 281 wchar_t long_temp_name[MAX_PATH + 1];
292 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); 282 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH);
293 if (long_name_len > MAX_PATH || long_name_len == 0) { 283 if (long_name_len > MAX_PATH || long_name_len == 0) {
294 // GetLongPathName() failed, but we still have a temporary file. 284 // GetLongPathName() failed, but we still have a temporary file.
295 *temp_file = FilePath(temp_name); 285 *temp_file = FilePath(temp_name);
296 return true; 286 return true;
297 } 287 }
298 288
299 FilePath::StringType long_temp_name_str; 289 FilePath::StringType long_temp_name_str;
300 long_temp_name_str.assign(long_temp_name, long_name_len); 290 long_temp_name_str.assign(long_temp_name, long_name_len);
301 *temp_file = FilePath(long_temp_name_str); 291 *temp_file = FilePath(long_temp_name_str);
302 return true; 292 return true;
303 } 293 }
304 294
305 bool CreateTemporaryDirInDir(const FilePath& base_dir, 295 bool CreateTemporaryDirInDir(const FilePath& base_dir,
306 const FilePath::StringType& prefix, 296 const FilePath::StringType& prefix,
307 FilePath* new_dir) { 297 FilePath* new_dir) {
308 base::ThreadRestrictions::AssertIOAllowed(); 298 ThreadRestrictions::AssertIOAllowed();
309 299
310 FilePath path_to_create; 300 FilePath path_to_create;
311 301
312 for (int count = 0; count < 50; ++count) { 302 for (int count = 0; count < 50; ++count) {
313 // Try create a new temporary directory with random generated name. If 303 // Try create a new temporary directory with random generated name. If
314 // the one exists, keep trying another path name until we reach some limit. 304 // the one exists, keep trying another path name until we reach some limit.
315 string16 new_dir_name; 305 string16 new_dir_name;
316 new_dir_name.assign(prefix); 306 new_dir_name.assign(prefix);
317 new_dir_name.append(base::IntToString16(::base::GetCurrentProcId())); 307 new_dir_name.append(IntToString16(GetCurrentProcId()));
318 new_dir_name.push_back('_'); 308 new_dir_name.push_back('_');
319 new_dir_name.append(base::IntToString16(base::RandInt(0, kint16max))); 309 new_dir_name.append(IntToString16(RandInt(0, kint16max)));
320 310
321 path_to_create = base_dir.Append(new_dir_name); 311 path_to_create = base_dir.Append(new_dir_name);
322 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) { 312 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) {
323 *new_dir = path_to_create; 313 *new_dir = path_to_create;
324 return true; 314 return true;
325 } 315 }
326 } 316 }
327 317
328 return false; 318 return false;
329 } 319 }
330 320
331 bool CreateNewTempDirectory(const FilePath::StringType& prefix, 321 bool CreateNewTempDirectory(const FilePath::StringType& prefix,
332 FilePath* new_temp_path) { 322 FilePath* new_temp_path) {
333 base::ThreadRestrictions::AssertIOAllowed(); 323 ThreadRestrictions::AssertIOAllowed();
334 324
335 FilePath system_temp_dir; 325 FilePath system_temp_dir;
336 if (!GetTempDir(&system_temp_dir)) 326 if (!GetTempDir(&system_temp_dir))
337 return false; 327 return false;
338 328
339 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); 329 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path);
340 } 330 }
341 331
332 } // namespace base
333
334 // -----------------------------------------------------------------------------
335
336 namespace file_util {
337
338 using base::DirectoryExists;
339 using base::FilePath;
340 using base::kFileShareAll;
341
342 bool CreateDirectoryAndGetError(const FilePath& full_path, 342 bool CreateDirectoryAndGetError(const FilePath& full_path,
343 base::PlatformFileError* error) { 343 base::PlatformFileError* error) {
344 base::ThreadRestrictions::AssertIOAllowed(); 344 base::ThreadRestrictions::AssertIOAllowed();
345 345
346 // If the path exists, we've succeeded if it's a directory, failed otherwise. 346 // If the path exists, we've succeeded if it's a directory, failed otherwise.
347 const wchar_t* full_path_str = full_path.value().c_str(); 347 const wchar_t* full_path_str = full_path.value().c_str();
348 DWORD fileattr = ::GetFileAttributes(full_path_str); 348 DWORD fileattr = ::GetFileAttributes(full_path_str);
349 if (fileattr != INVALID_FILE_ATTRIBUTES) { 349 if (fileattr != INVALID_FILE_ATTRIBUTES) {
350 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { 350 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
351 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " 351 DVLOG(1) << "CreateDirectory(" << full_path_str << "), "
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 // Like Move, this function is not transactional, so we just 744 // Like Move, this function is not transactional, so we just
745 // leave the copied bits behind if deleting from_path fails. 745 // leave the copied bits behind if deleting from_path fails.
746 // If to_path exists previously then we have already overwritten 746 // If to_path exists previously then we have already overwritten
747 // it by now, we don't get better off by deleting the new bits. 747 // it by now, we don't get better off by deleting the new bits.
748 } 748 }
749 return false; 749 return false;
750 } 750 }
751 751
752 } // namespace internal 752 } // namespace internal
753 } // namespace base 753 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_unittest.cc ('k') | base/files/file_util_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698