2 * Copyright (C) 2007 Apple Inc. All rights reserved.
\r
4 * Redistribution and use in source and binary forms, with or without
\r
5 * modification, are permitted provided that the following conditions
\r
8 * 1. Redistributions of source code must retain the above copyright
\r
9 * notice, this list of conditions and the following disclaimer.
\r
10 * 2. Redistributions in binary form must reproduce the above copyright
\r
11 * notice, this list of conditions and the following disclaimer in the
\r
12 * documentation and/or other materials provided with the distribution.
\r
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
\r
14 * its contributors may be used to endorse or promote products derived
\r
15 * from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
\r
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
\r
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
\r
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
30 #include "FileSystem.h"
\r
32 #include "CString.h"
\r
33 #include "NotImplemented.h"
\r
34 #include "PlatformString.h"
\r
36 #include <windows.h>
\r
37 #include <winbase.h>
\r
39 #include <shlwapi.h>
\r
43 bool fileSize(const String& path, long long& result)
\r
45 struct _stat32i64 sb;
\r
46 String filename = path;
\r
47 int statResult = _wstat32i64(filename.charactersWithNullTermination(), &sb);
\r
48 if (statResult != 0 || (sb.st_mode & S_IFMT) != S_IFREG)
\r
50 result = sb.st_size;
\r
54 bool fileExists(const String& path)
\r
56 struct _stat32i64 sb;
\r
57 String filename = path;
\r
58 return !_wstat32i64(filename.charactersWithNullTermination(), &sb);
\r
61 bool deleteFile(const String& path)
\r
63 String filename = path;
\r
64 return !!DeleteFileW(filename.charactersWithNullTermination());
\r
67 String pathByAppendingComponent(const String& path, const String& component)
\r
69 Vector<UChar> buffer(MAX_PATH);
\r
71 if (path.length() + 1 > buffer.size())
\r
74 memcpy(buffer.data(), path.characters(), path.length() * sizeof(UChar));
\r
75 buffer[path.length()] = '\0';
\r
77 String componentCopy = component;
\r
78 if (!PathAppendW(buffer.data(), componentCopy.charactersWithNullTermination()))
\r
81 buffer.resize(wcslen(buffer.data()));
\r
83 return String::adopt(buffer);
\r
86 CString fileSystemRepresentation(const String&)
\r
91 bool makeAllDirectories(const String& path)
\r
93 String fullPath = path;
\r
94 if (!SHCreateDirectoryEx(0, fullPath.charactersWithNullTermination(), 0)) {
\r
95 DWORD error = GetLastError();
\r
96 if (error != ERROR_FILE_EXISTS && error != ERROR_ALREADY_EXISTS) {
\r
97 LOG_ERROR("Failed to create path %s", path.ascii().data());
\r
104 String homeDirectoryPath()
\r
110 static String bundleName()
\r
112 static bool initialized;
\r
113 static String name = "WebKit";
\r
115 if (!initialized) {
\r
116 initialized = true;
\r
118 if (CFBundleRef bundle = CFBundleGetMainBundle())
119 if (CFTypeRef bundleExecutable = CFBundleGetValueForInfoDictionaryKey(bundle, kCFBundleExecutableKey))
120 if (CFGetTypeID(bundleExecutable) == CFStringGetTypeID())
121 name = reinterpret_cast<CFStringRef>(bundleExecutable);
\r
127 static String storageDirectory(DWORD pathIdentifier)
129 Vector<UChar> buffer(MAX_PATH);
130 if (FAILED(SHGetFolderPathW(0, pathIdentifier | CSIDL_FLAG_CREATE, 0, 0, buffer.data())))
132 buffer.resize(wcslen(buffer.data()));
133 String directory = String::adopt(buffer);
135 static const String companyNameDirectory = "Apple Computer\\";
136 directory = pathByAppendingComponent(directory, companyNameDirectory + bundleName());
137 if (!makeAllDirectories(directory))
143 static String cachedStorageDirectory(DWORD pathIdentifier)
\r
145 static HashMap<DWORD, String> directories;
\r
147 HashMap<DWORD, String>::iterator it = directories.find(pathIdentifier);
\r
148 if (it != directories.end())
\r
151 String directory = storageDirectory(pathIdentifier);
\r
152 directories.add(pathIdentifier, directory);
\r
157 String localUserSpecificStorageDirectory()
159 return cachedStorageDirectory(CSIDL_LOCAL_APPDATA);
162 String roamingUserSpecificStorageDirectory()
164 return cachedStorageDirectory(CSIDL_APPDATA);
167 bool safeCreateFile(const String& path, CFDataRef data)
169 // Create a temporary file.
170 WCHAR tempDirPath[MAX_PATH];
171 if (!GetTempPathW(ARRAYSIZE(tempDirPath), tempDirPath))
174 WCHAR tempPath[MAX_PATH];
175 if (!GetTempFileNameW(tempDirPath, L"WEBKIT", 0, tempPath))
178 HANDLE tempFileHandle = CreateFileW(tempPath, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
179 if (tempFileHandle == INVALID_HANDLE_VALUE)
182 // Write the data to this temp file.
184 if (!WriteFile(tempFileHandle, CFDataGetBytePtr(data), static_cast<DWORD>(CFDataGetLength(data)), &written, 0))
187 CloseHandle(tempFileHandle);
189 // Copy the temp file to the destination file.
190 String destination = path;
191 if (!MoveFileExW(tempPath, destination.charactersWithNullTermination(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
197 } // namespace WebCore
\r