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 #include "base/files/file.h" 6 #include "base/files/file_path.h" 7 #include "util/build_config.h" 8 9 #if defined(OS_POSIX) || defined(OS_FUCHSIA) 10 #include <errno.h> 11 #endif 12 13 namespace base { 14 15 File::Info::Info() = default; 16 17 File::Info::~Info() = default; 18 19 File::File() = default; 20 File(const FilePath & path,uint32_t flags)21File::File(const FilePath& path, uint32_t flags) 22 : error_details_(FILE_OK) { 23 Initialize(path, flags); 24 } 25 File(PlatformFile platform_file)26File::File(PlatformFile platform_file) 27 : file_(platform_file), 28 error_details_(FILE_OK) { 29 #if defined(OS_POSIX) || defined(OS_FUCHSIA) 30 DCHECK_GE(platform_file, -1); 31 #endif 32 } 33 File(Error error_details)34File::File(Error error_details) 35 : error_details_(error_details) {} 36 File(File && other)37File::File(File&& other) 38 : file_(other.TakePlatformFile()), 39 error_details_(other.error_details()) {} 40 ~File()41File::~File() { 42 // Go through the AssertIOAllowed logic. 43 Close(); 44 } 45 operator =(File && other)46File& File::operator=(File&& other) { 47 Close(); 48 SetPlatformFile(other.TakePlatformFile()); 49 error_details_ = other.error_details(); 50 return *this; 51 } 52 Initialize(const FilePath & path,uint32_t flags)53void File::Initialize(const FilePath& path, uint32_t flags) { 54 if (path.ReferencesParent()) { 55 #if defined(OS_WIN) 56 ::SetLastError(ERROR_ACCESS_DENIED); 57 #elif defined(OS_POSIX) || defined(OS_FUCHSIA) 58 errno = EACCES; 59 #else 60 #error Unsupported platform 61 #endif 62 error_details_ = FILE_ERROR_ACCESS_DENIED; 63 return; 64 } 65 DoInitialize(path, flags); 66 } 67 ErrorToString(Error error)68std::string File::ErrorToString(Error error) { 69 switch (error) { 70 case FILE_OK: 71 return "FILE_OK"; 72 case FILE_ERROR_FAILED: 73 return "FILE_ERROR_FAILED"; 74 case FILE_ERROR_IN_USE: 75 return "FILE_ERROR_IN_USE"; 76 case FILE_ERROR_EXISTS: 77 return "FILE_ERROR_EXISTS"; 78 case FILE_ERROR_NOT_FOUND: 79 return "FILE_ERROR_NOT_FOUND"; 80 case FILE_ERROR_ACCESS_DENIED: 81 return "FILE_ERROR_ACCESS_DENIED"; 82 case FILE_ERROR_TOO_MANY_OPENED: 83 return "FILE_ERROR_TOO_MANY_OPENED"; 84 case FILE_ERROR_NO_MEMORY: 85 return "FILE_ERROR_NO_MEMORY"; 86 case FILE_ERROR_NO_SPACE: 87 return "FILE_ERROR_NO_SPACE"; 88 case FILE_ERROR_NOT_A_DIRECTORY: 89 return "FILE_ERROR_NOT_A_DIRECTORY"; 90 case FILE_ERROR_INVALID_OPERATION: 91 return "FILE_ERROR_INVALID_OPERATION"; 92 case FILE_ERROR_SECURITY: 93 return "FILE_ERROR_SECURITY"; 94 case FILE_ERROR_ABORT: 95 return "FILE_ERROR_ABORT"; 96 case FILE_ERROR_NOT_A_FILE: 97 return "FILE_ERROR_NOT_A_FILE"; 98 case FILE_ERROR_NOT_EMPTY: 99 return "FILE_ERROR_NOT_EMPTY"; 100 case FILE_ERROR_INVALID_URL: 101 return "FILE_ERROR_INVALID_URL"; 102 case FILE_ERROR_IO: 103 return "FILE_ERROR_IO"; 104 case FILE_ERROR_MAX: 105 break; 106 } 107 108 NOTREACHED(); 109 return ""; 110 } 111 112 } // namespace base 113