7529d20b6ade0b6d005df47e45dd689d28c107f5
[framework/web/webkit-efl.git] / Source / WebCore / platform / FileSystem.h
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/MathExtras.h>
37 #include <wtf/Vector.h>
38
39 #if USE(CF)
40 #include <wtf/RetainPtr.h>
41 #endif
42
43 #if PLATFORM(QT)
44 #include <QFile>
45 #include <QLibrary>
46 #if defined(Q_OS_WIN32)
47 #include <windows.h>
48 #endif
49 #endif
50
51 #if PLATFORM(WX)
52 #include <wx/defs.h>
53 #include <wx/file.h>
54 #endif
55
56 #if USE(CF) || (PLATFORM(QT) && defined(Q_WS_MAC))
57 typedef struct __CFBundle* CFBundleRef;
58 typedef const struct __CFData* CFDataRef;
59 #endif
60
61 #if OS(WINDOWS)
62 // These are to avoid including <winbase.h> in a header for Chromium
63 typedef void *HANDLE;
64 // Assuming STRICT
65 typedef struct HINSTANCE__* HINSTANCE;
66 typedef HINSTANCE HMODULE;
67 #endif
68
69 #if PLATFORM(GTK)
70 typedef struct _GFileIOStream GFileIOStream;
71 typedef struct _GModule GModule;
72 #endif
73
74 #if PLATFORM(EFL)
75 typedef struct _Eina_Module Eina_Module;
76 #endif
77
78 namespace WebCore {
79
80 // PlatformModule
81 #if OS(WINDOWS)
82 typedef HMODULE PlatformModule;
83 #elif PLATFORM(GTK)
84 typedef GModule* PlatformModule;
85 #elif PLATFORM(EFL)
86 typedef Eina_Module* PlatformModule;
87 #elif PLATFORM(QT)
88 #if defined(Q_WS_MAC)
89 typedef CFBundleRef PlatformModule;
90 #elif !defined(QT_NO_LIBRARY)
91 typedef QLibrary* PlatformModule;
92 #else
93 typedef void* PlatformModule;
94 #endif
95 #elif USE(CF)
96 typedef CFBundleRef PlatformModule;
97 #else
98 typedef void* PlatformModule;
99 #endif
100
101 // PlatformModuleVersion
102 #if OS(WINDOWS)
103 struct PlatformModuleVersion {
104     unsigned leastSig;
105     unsigned mostSig;
106
107     PlatformModuleVersion(unsigned)
108         : leastSig(0)
109         , mostSig(0)
110     {
111     }
112
113     PlatformModuleVersion(unsigned lsb, unsigned msb)
114         : leastSig(lsb)
115         , mostSig(msb)
116     {
117     }
118
119 };
120 #else
121 typedef unsigned PlatformModuleVersion;
122 #endif
123
124 // PlatformFileHandle
125 #if PLATFORM(QT)
126 typedef QFile* PlatformFileHandle;
127 const PlatformFileHandle invalidPlatformFileHandle = 0;
128 #elif PLATFORM(GTK)
129 typedef GFileIOStream* PlatformFileHandle;
130 const PlatformFileHandle invalidPlatformFileHandle = 0;
131 #elif OS(WINDOWS)
132 typedef HANDLE PlatformFileHandle;
133 // FIXME: -1 is INVALID_HANDLE_VALUE, defined in <winbase.h>. Chromium tries to
134 // avoid using Windows headers in headers.  We'd rather move this into the .cpp.
135 const PlatformFileHandle invalidPlatformFileHandle = reinterpret_cast<HANDLE>(-1);
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 struct FileMetadata;
162
163 bool fileExists(const String&);
164 bool deleteFile(const String&);
165 bool deleteEmptyDirectory(const String&);
166 bool getFileSize(const String&, long long& result);
167 bool getFileModificationTime(const String&, time_t& result);
168 bool getFileMetadata(const String&, FileMetadata&);
169 String pathByAppendingComponent(const String& path, const String& component);
170 bool makeAllDirectories(const String& path);
171 String homeDirectoryPath();
172 String pathGetFileName(const String&);
173 String directoryName(const String&);
174
175 bool canExcludeFromBackup(); // Returns true if any file can ever be excluded from backup.
176 bool excludeFromBackup(const String&); // Returns true if successful.
177
178 Vector<String> listDirectory(const String& path, const String& filter = String());
179
180 CString fileSystemRepresentation(const String&);
181
182 inline bool isHandleValid(const PlatformFileHandle& handle) { return handle != invalidPlatformFileHandle; }
183
184 inline double invalidFileTime() { return std::numeric_limits<double>::quiet_NaN(); }
185 inline bool isValidFileTime(double time) { return isfinite(time); }
186
187 // Prefix is what the filename should be prefixed with, not the full path.
188 String openTemporaryFile(const String& prefix, PlatformFileHandle&);
189 PlatformFileHandle openFile(const String& path, FileOpenMode);
190 void closeFile(PlatformFileHandle&);
191 // Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
192 long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin);
193 bool truncateFile(PlatformFileHandle, long long offset);
194 // Returns number of bytes actually read if successful, -1 otherwise.
195 int writeToFile(PlatformFileHandle, const char* data, int length);
196 // Returns number of bytes actually written if successful, -1 otherwise.
197 int readFromFile(PlatformFileHandle, char* data, int length);
198
199 // Functions for working with loadable modules.
200 bool unloadModule(PlatformModule);
201
202 // Encode a string for use within a file name.
203 String encodeForFileName(const String&);
204
205 #if USE(CF)
206 RetainPtr<CFURLRef> pathAsURL(const String&);
207 #endif
208
209 #if PLATFORM(MAC)
210 void setMetadataURL(String& URLString, const String& referrer, const String& path);
211 #endif
212
213 #if PLATFORM(GTK) || PLATFORM(EFL)
214 String filenameToString(const char*);
215 String filenameForDisplay(const String&);
216 CString applicationDirectoryPath();
217 CString sharedResourcesPath();
218 uint64_t getVolumeFreeSizeForPath(const char*);
219 #endif
220
221 #if PLATFORM(WIN) && !OS(WINCE)
222 String localUserSpecificStorageDirectory();
223 String roamingUserSpecificStorageDirectory();
224 #endif
225
226 #if PLATFORM(WIN) && USE(CF)
227 bool safeCreateFile(const String&, CFDataRef);
228 #endif
229
230 #if ENABLE(TIZEN_FILE_SYSTEM)
231 bool copyFile(const String& sourcePath, const String& destinationPath);
232 bool getDirectorySize(const String& path, long long& size);
233 bool linkFile(const String& sourcePath, const String& destinationPath);
234 bool removeDirectory(const String& path);
235 bool renameFile(const String& sourcePath, const String& destinationPath);
236 #endif
237
238 } // namespace WebCore
239
240 #endif // FileSystem_h