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 <time.h>
34#include <wtf/Forward.h>
35#include <wtf/MathExtras.h>
36#include <wtf/Vector.h>
37#include <wtf/text/WTFString.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 USE(CF) || (PLATFORM(QT) && defined(Q_WS_MAC))
52typedef struct __CFBundle* CFBundleRef;
53typedef const struct __CFData* CFDataRef;
54#endif
55
56#if OS(WINDOWS)
57// These are to avoid including <winbase.h> in a header for Chromium
58typedef void *HANDLE;
59// Assuming STRICT
60typedef struct HINSTANCE__* HINSTANCE;
61typedef HINSTANCE HMODULE;
62#endif
63
64#if PLATFORM(GTK)
65typedef struct _GFileIOStream GFileIOStream;
66typedef struct _GModule GModule;
67#endif
68
69#if PLATFORM(EFL)
70typedef struct _Eina_Module Eina_Module;
71#endif
72
73namespace WebCore {
74
75// PlatformModule
76#if OS(WINDOWS)
77typedef HMODULE PlatformModule;
78#elif PLATFORM(GTK)
79typedef GModule* PlatformModule;
80#elif PLATFORM(EFL)
81typedef Eina_Module* PlatformModule;
82#elif PLATFORM(QT)
83#if defined(Q_WS_MAC)
84typedef CFBundleRef PlatformModule;
85#elif !defined(QT_NO_LIBRARY)
86typedef QLibrary* PlatformModule;
87#else
88typedef void* PlatformModule;
89#endif
90#elif USE(CF)
91typedef CFBundleRef PlatformModule;
92#else
93typedef void* PlatformModule;
94#endif
95
96// PlatformModuleVersion
97#if OS(WINDOWS)
98struct PlatformModuleVersion {
99    unsigned leastSig;
100    unsigned mostSig;
101
102    PlatformModuleVersion(unsigned)
103        : leastSig(0)
104        , mostSig(0)
105    {
106    }
107
108    PlatformModuleVersion(unsigned lsb, unsigned msb)
109        : leastSig(lsb)
110        , mostSig(msb)
111    {
112    }
113
114};
115#else
116typedef unsigned PlatformModuleVersion;
117#endif
118
119// PlatformFileHandle
120#if PLATFORM(QT)
121typedef QFile* PlatformFileHandle;
122const PlatformFileHandle invalidPlatformFileHandle = 0;
123#elif PLATFORM(GTK)
124typedef GFileIOStream* PlatformFileHandle;
125const PlatformFileHandle invalidPlatformFileHandle = 0;
126#elif OS(WINDOWS)
127typedef HANDLE PlatformFileHandle;
128// FIXME: -1 is INVALID_HANDLE_VALUE, defined in <winbase.h>. Chromium tries to
129// avoid using Windows headers in headers.  We'd rather move this into the .cpp.
130const PlatformFileHandle invalidPlatformFileHandle = reinterpret_cast<HANDLE>(-1);
131#else
132typedef int PlatformFileHandle;
133const PlatformFileHandle invalidPlatformFileHandle = -1;
134#endif
135
136enum FileOpenMode {
137    OpenForRead = 0,
138    OpenForWrite
139};
140
141enum FileSeekOrigin {
142    SeekFromBeginning = 0,
143    SeekFromCurrent,
144    SeekFromEnd
145};
146
147enum FileLockMode {
148    LockShared = 1,
149    LockExclusive = 2,
150    LockNonBlocking = 4
151};
152
153#if OS(WINDOWS)
154static const char PlatformFilePathSeparator = '\\';
155#else
156static const char PlatformFilePathSeparator = '/';
157#endif
158
159struct FileMetadata;
160
161bool fileExists(const String&);
162bool deleteFile(const String&);
163bool deleteEmptyDirectory(const String&);
164bool getFileSize(const String&, long long& result);
165bool getFileModificationTime(const String&, time_t& result);
166bool getFileMetadata(const String&, FileMetadata&);
167String pathByAppendingComponent(const String& path, const String& component);
168bool makeAllDirectories(const String& path);
169String homeDirectoryPath();
170String pathGetFileName(const String&);
171String directoryName(const String&);
172
173bool canExcludeFromBackup(); // Returns true if any file can ever be excluded from backup.
174bool excludeFromBackup(const String&); // Returns true if successful.
175
176Vector<String> listDirectory(const String& path, const String& filter = String());
177
178CString fileSystemRepresentation(const String&);
179
180inline bool isHandleValid(const PlatformFileHandle& handle) { return handle != invalidPlatformFileHandle; }
181
182inline double invalidFileTime() { return std::numeric_limits<double>::quiet_NaN(); }
183inline bool isValidFileTime(double time) { return std::isfinite(time); }
184
185// Prefix is what the filename should be prefixed with, not the full path.
186String openTemporaryFile(const String& prefix, PlatformFileHandle&);
187PlatformFileHandle openFile(const String& path, FileOpenMode);
188void closeFile(PlatformFileHandle&);
189// Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
190long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin);
191bool truncateFile(PlatformFileHandle, long long offset);
192// Returns number of bytes actually read if successful, -1 otherwise.
193int writeToFile(PlatformFileHandle, const char* data, int length);
194// Returns number of bytes actually written if successful, -1 otherwise.
195int readFromFile(PlatformFileHandle, char* data, int length);
196#if USE(FILE_LOCK)
197bool lockFile(PlatformFileHandle, FileLockMode);
198bool unlockFile(PlatformFileHandle);
199#endif
200
201// Functions for working with loadable modules.
202bool unloadModule(PlatformModule);
203
204// Encode a string for use within a file name.
205String encodeForFileName(const String&);
206
207#if USE(CF)
208RetainPtr<CFURLRef> pathAsURL(const String&);
209#endif
210
211#if PLATFORM(MAC)
212void setMetadataURL(String& URLString, const String& referrer, const String& path);
213#endif
214
215#if PLATFORM(GTK)
216String filenameToString(const char*);
217String filenameForDisplay(const String&);
218CString applicationDirectoryPath();
219CString sharedResourcesPath();
220#endif
221#if USE(SOUP) || PLATFORM(QT)
222uint64_t getVolumeFreeSizeForPath(const char*);
223#endif
224
225#if PLATFORM(WIN) && !OS(WINCE)
226String localUserSpecificStorageDirectory();
227String roamingUserSpecificStorageDirectory();
228#endif
229
230} // namespace WebCore
231
232#endif // FileSystem_h
233