1 /*
2 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Collabora, Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef FileSystem_h
31 #define FileSystem_h
32
33 #include "PlatformString.h"
34 #include <time.h>
35 #include <wtf/Forward.h>
36 #include <wtf/Vector.h>
37
38 #if USE(CF)
39 #include <wtf/RetainPtr.h>
40 #endif
41
42 #if PLATFORM(QT)
43 #include <QFile>
44 #include <QLibrary>
45 #if defined(Q_OS_WIN32)
46 #include <windows.h>
47 #endif
48 #endif
49
50 #if PLATFORM(WX)
51 #include <wx/defs.h>
52 #include <wx/file.h>
53 #endif
54
55 #if USE(CF) || (PLATFORM(QT) && defined(Q_WS_MAC))
56 typedef struct __CFBundle* CFBundleRef;
57 typedef const struct __CFData* CFDataRef;
58 #endif
59
60 #if OS(WINDOWS)
61 // These are to avoid including <winbase.h> in a header for Chromium
62 typedef void *HANDLE;
63 // Assuming STRICT
64 typedef struct HINSTANCE__* HINSTANCE;
65 typedef HINSTANCE HMODULE;
66 #endif
67
68 #if PLATFORM(BREWMP)
69 typedef struct _IFile IFile;
70 #endif
71
72 #if PLATFORM(GTK)
73 typedef struct _GFileIOStream GFileIOStream;
74 typedef struct _GModule GModule;
75 #endif
76
77 namespace WebCore {
78
79 // PlatformModule
80 #if PLATFORM(GTK)
81 typedef GModule* PlatformModule;
82 #elif OS(WINDOWS)
83 typedef HMODULE PlatformModule;
84 #elif PLATFORM(QT)
85 #if defined(Q_WS_MAC)
86 typedef CFBundleRef PlatformModule;
87 #elif !defined(QT_NO_LIBRARY)
88 typedef QLibrary* PlatformModule;
89 #else
90 typedef void* PlatformModule;
91 #endif
92 #elif USE(CF)
93 typedef CFBundleRef PlatformModule;
94 #else
95 typedef void* PlatformModule;
96 #endif
97
98 // PlatformModuleVersion
99 #if OS(WINDOWS)
100 struct PlatformModuleVersion {
101 unsigned leastSig;
102 unsigned mostSig;
103
PlatformModuleVersionPlatformModuleVersion104 PlatformModuleVersion(unsigned)
105 : leastSig(0)
106 , mostSig(0)
107 {
108 }
109
PlatformModuleVersionPlatformModuleVersion110 PlatformModuleVersion(unsigned lsb, unsigned msb)
111 : leastSig(lsb)
112 , mostSig(msb)
113 {
114 }
115
116 };
117 #else
118 typedef unsigned PlatformModuleVersion;
119 #endif
120
121 // PlatformFileHandle
122 #if PLATFORM(QT)
123 typedef QFile* PlatformFileHandle;
124 const PlatformFileHandle invalidPlatformFileHandle = 0;
125 #elif PLATFORM(GTK)
126 typedef GFileIOStream* PlatformFileHandle;
127 const PlatformFileHandle invalidPlatformFileHandle = 0;
128 #elif OS(WINDOWS)
129 typedef HANDLE PlatformFileHandle;
130 // FIXME: -1 is INVALID_HANDLE_VALUE, defined in <winbase.h>. Chromium tries to
131 // avoid using Windows headers in headers. We'd rather move this into the .cpp.
132 const PlatformFileHandle invalidPlatformFileHandle = reinterpret_cast<HANDLE>(-1);
133 #elif PLATFORM(BREWMP)
134 typedef IFile* PlatformFileHandle;
135 const PlatformFileHandle invalidPlatformFileHandle = 0;
136 #elif PLATFORM(WX)
137 typedef wxFile* PlatformFileHandle;
138 const PlatformFileHandle invalidPlatformFileHandle = 0;
139 #else
140 typedef int PlatformFileHandle;
141 const PlatformFileHandle invalidPlatformFileHandle = -1;
142 #endif
143
144 enum FileOpenMode {
145 OpenForRead = 0,
146 OpenForWrite
147 };
148
149 enum FileSeekOrigin {
150 SeekFromBeginning = 0,
151 SeekFromCurrent,
152 SeekFromEnd
153 };
154
155 #if OS(WINDOWS)
156 static const char PlatformFilePathSeparator = '\\';
157 #else
158 static const char PlatformFilePathSeparator = '/';
159 #endif
160
161 void revealFolderInOS(const String&);
162 bool fileExists(const String&);
163 bool deleteFile(const String&);
164 bool deleteEmptyDirectory(const String&);
165 bool getFileSize(const String&, long long& result);
166 bool getFileModificationTime(const String&, time_t& result);
167 String pathByAppendingComponent(const String& path, const String& component);
168 bool makeAllDirectories(const String& path);
169 String homeDirectoryPath();
170 String pathGetFileName(const String&);
171 String directoryName(const String&);
172
173 bool canExcludeFromBackup(); // Returns true if any file can ever be excluded from backup.
174 bool excludeFromBackup(const String&); // Returns true if successful.
175
176 Vector<String> listDirectory(const String& path, const String& filter = String());
177
178 CString fileSystemRepresentation(const String&);
179
isHandleValid(const PlatformFileHandle & handle)180 inline bool isHandleValid(const PlatformFileHandle& handle) { return handle != invalidPlatformFileHandle; }
181
182 // Prefix is what the filename should be prefixed with, not the full path.
183 String openTemporaryFile(const String& prefix, PlatformFileHandle&);
184 PlatformFileHandle openFile(const String& path, FileOpenMode);
185 void closeFile(PlatformFileHandle&);
186 // Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
187 long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin);
188 bool truncateFile(PlatformFileHandle, long long offset);
189 // Returns number of bytes actually read if successful, -1 otherwise.
190 int writeToFile(PlatformFileHandle, const char* data, int length);
191 // Returns number of bytes actually written if successful, -1 otherwise.
192 int readFromFile(PlatformFileHandle, char* data, int length);
193
194 // Functions for working with loadable modules.
195 bool unloadModule(PlatformModule);
196
197 // Encode a string for use within a file name.
198 String encodeForFileName(const String&);
199
200 #if USE(CF)
201 RetainPtr<CFURLRef> pathAsURL(const String&);
202 #endif
203
204 #if PLATFORM(CHROMIUM)
205 String pathGetDisplayFileName(const String&);
206 #endif
207
208 #if PLATFORM(GTK)
209 String filenameToString(const char*);
210 String filenameForDisplay(const String&);
211 CString applicationDirectoryPath();
212 #endif
213
214 #if PLATFORM(WIN) && !OS(WINCE)
215 String localUserSpecificStorageDirectory();
216 String roamingUserSpecificStorageDirectory();
217 bool safeCreateFile(const String&, CFDataRef);
218 #endif
219
220 } // namespace WebCore
221
222 #endif // FileSystem_h
223