• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 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/platform_file.h"
6 
7 #include "base/file_path.h"
8 #include "base/logging.h"
9 
10 namespace base {
11 
CreatePlatformFile(const FilePath & name,int flags,bool * created)12 PlatformFile CreatePlatformFile(const FilePath& name,
13                                 int flags,
14                                 bool* created) {
15   DWORD disposition = 0;
16 
17   if (flags & PLATFORM_FILE_OPEN)
18     disposition = OPEN_EXISTING;
19 
20   if (flags & PLATFORM_FILE_CREATE) {
21     DCHECK(!disposition);
22     disposition = CREATE_NEW;
23   }
24 
25   if (flags & PLATFORM_FILE_OPEN_ALWAYS) {
26     DCHECK(!disposition);
27     disposition = OPEN_ALWAYS;
28   }
29 
30   if (flags & PLATFORM_FILE_CREATE_ALWAYS) {
31     DCHECK(!disposition);
32     disposition = CREATE_ALWAYS;
33   }
34 
35   if (!disposition) {
36     NOTREACHED();
37     return NULL;
38   }
39 
40   DWORD access = (flags & PLATFORM_FILE_READ) ? GENERIC_READ : 0;
41   if (flags & PLATFORM_FILE_WRITE)
42     access |= GENERIC_WRITE;
43 
44   DWORD sharing = (flags & PLATFORM_FILE_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ;
45   if (!(flags & PLATFORM_FILE_EXCLUSIVE_WRITE))
46     sharing |= FILE_SHARE_WRITE;
47 
48   DWORD create_flags = 0;
49   if (flags & PLATFORM_FILE_ASYNC)
50     create_flags |= FILE_FLAG_OVERLAPPED;
51   if (flags & PLATFORM_FILE_TEMPORARY)
52     create_flags |= FILE_ATTRIBUTE_TEMPORARY;
53   if (flags & PLATFORM_FILE_HIDDEN)
54     create_flags |= FILE_ATTRIBUTE_HIDDEN;
55   if (flags & PLATFORM_FILE_DELETE_ON_CLOSE)
56     create_flags |= FILE_FLAG_DELETE_ON_CLOSE;
57 
58   HANDLE file = CreateFile(name.value().c_str(), access, sharing, NULL,
59                            disposition, create_flags, NULL);
60 
61   if ((flags & PLATFORM_FILE_OPEN_ALWAYS) && created &&
62       INVALID_HANDLE_VALUE != file) {
63     *created = (ERROR_ALREADY_EXISTS != GetLastError());
64   }
65 
66   return file;
67 }
68 
CreatePlatformFile(const std::wstring & name,int flags,bool * created)69 PlatformFile CreatePlatformFile(const std::wstring& name, int flags,
70                                 bool* created) {
71   return CreatePlatformFile(FilePath::FromWStringHack(name), flags, created);
72 }
73 
ClosePlatformFile(PlatformFile file)74 bool ClosePlatformFile(PlatformFile file) {
75   return (CloseHandle(file) == 0);
76 }
77 
78 }  // namespace disk_cache
79