2 * Copyright (C) 2007, 2009 Holger Hans Peter Freyther
3 * Copyright (C) 2008 Collabora, Ltd.
4 * Copyright (C) 2008 Apple Inc. All rights reserved.
5 * Portions Copyright (c) 2010 Motorola Mobility, Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #include "FileSystem.h"
26 #include "FileMetadata.h"
30 #include <glib/gstdio.h>
31 #include <wtf/gobject/GOwnPtr.h>
32 #include <wtf/gobject/GRefPtr.h>
33 #include <wtf/gobject/GlibUtilities.h>
34 #include <wtf/text/CString.h>
35 #include <wtf/text/WTFString.h>
39 /* On linux file names are just raw bytes, so also strings that cannot be encoded in any way
40 * are valid file names. This mean that we cannot just store a file name as-is in a String
41 * but we have to escape it.
42 * On Windows the GLib file name encoding is always UTF-8 so we can optimize this case. */
43 String filenameToString(const char* filename)
49 return String::fromUTF8(filename);
51 GOwnPtr<gchar> escapedString(g_uri_escape_string(filename, "/:", false));
52 return escapedString.get();
56 CString fileSystemRepresentation(const String& path)
61 GOwnPtr<gchar> filename(g_uri_unescape_string(path.utf8().data(), 0));
62 return filename.get();
66 // Converts a string to something suitable to be displayed to the user.
67 String filenameForDisplay(const String& string)
72 CString filename = fileSystemRepresentation(string);
73 GOwnPtr<gchar> display(g_filename_to_utf8(filename.data(), 0, 0, 0, 0));
77 return String::fromUTF8(display.get());
81 bool fileExists(const String& path)
84 CString filename = fileSystemRepresentation(path);
86 if (!filename.isNull())
87 result = g_file_test(filename.data(), G_FILE_TEST_EXISTS);
92 bool deleteFile(const String& path)
95 CString filename = fileSystemRepresentation(path);
97 if (!filename.isNull())
98 result = g_remove(filename.data()) == 0;
103 bool deleteEmptyDirectory(const String& path)
106 CString filename = fileSystemRepresentation(path);
108 if (!filename.isNull())
109 result = g_rmdir(filename.data()) == 0;
114 bool getFileSize(const String& path, long long& resultSize)
116 CString filename = fileSystemRepresentation(path);
117 if (filename.isNull())
121 gint result = g_stat(filename.data(), &statResult);
125 resultSize = statResult.st_size;
129 bool getFileModificationTime(const String& path, time_t& modifiedTime)
131 CString filename = fileSystemRepresentation(path);
132 if (filename.isNull())
136 gint result = g_stat(filename.data(), &statResult);
140 modifiedTime = statResult.st_mtime;
145 bool getFileMetadata(const String& path, FileMetadata& metadata)
147 CString filename = fileSystemRepresentation(path);
148 if (filename.isNull())
151 struct stat statResult;
152 gint result = g_stat(filename.data(), &statResult);
156 metadata.modificationTime = statResult.st_mtime;
157 metadata.length = statResult.st_size;
158 metadata.type = S_ISDIR(statResult.st_mode) ? FileMetadata::TypeDirectory : FileMetadata::TypeFile;
163 String pathByAppendingComponent(const String& path, const String& component)
165 if (path.endsWith(G_DIR_SEPARATOR_S))
166 return path + component;
168 return path + G_DIR_SEPARATOR_S + component;
171 bool makeAllDirectories(const String& path)
173 CString filename = fileSystemRepresentation(path);
174 if (filename.isNull())
177 gint result = g_mkdir_with_parents(filename.data(), S_IRWXU);
182 String homeDirectoryPath()
184 return filenameToString(g_get_home_dir());
187 String pathGetFileName(const String& pathName)
189 if (pathName.isEmpty())
192 CString tmpFilename = fileSystemRepresentation(pathName);
193 GOwnPtr<gchar> baseName(g_path_get_basename(tmpFilename.data()));
194 return String::fromUTF8(baseName.get());
197 CString applicationDirectoryPath()
199 CString path = getCurrentExecutablePath();
203 // If the above fails, check the PATH env variable.
204 GOwnPtr<char> currentExePath(g_find_program_in_path(g_get_prgname()));
205 if (!currentExePath.get())
208 GOwnPtr<char> dirname(g_path_get_dirname(currentExePath.get()));
209 return dirname.get();
212 CString sharedResourcesPath()
214 static CString cachedPath;
215 if (!cachedPath.isNull())
220 GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char*>(sharedResourcesPath), &hmodule);
222 GOwnPtr<gchar> runtimeDir(g_win32_get_package_installation_directory_of_module(hmodule));
223 GOwnPtr<gchar> dataPath(g_build_filename(runtimeDir.get(), "share", "webkitgtk-"WEBKITGTK_API_VERSION_STRING, NULL));
225 GOwnPtr<gchar> dataPath(g_build_filename(DATA_DIR, "webkitgtk-" WEBKITGTK_API_VERSION_STRING, NULL));
228 cachedPath = dataPath.get();
232 uint64_t getVolumeFreeSizeForPath(const char* path)
234 GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(path));
235 GRefPtr<GFileInfo> fileInfo = adoptGRef(g_file_query_filesystem_info(file.get(), G_FILE_ATTRIBUTE_FILESYSTEM_FREE, 0, 0));
239 return g_file_info_get_attribute_uint64(fileInfo.get(), G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
242 String directoryName(const String& path)
244 /* No null checking needed */
245 GOwnPtr<char> dirname(g_path_get_dirname(fileSystemRepresentation(path).data()));
246 return String::fromUTF8(dirname.get());
249 Vector<String> listDirectory(const String& path, const String& filter)
251 Vector<String> entries;
253 CString filename = fileSystemRepresentation(path);
254 GDir* dir = g_dir_open(filename.data(), 0, 0);
258 GPatternSpec *pspec = g_pattern_spec_new((filter.utf8()).data());
259 while (const char* name = g_dir_read_name(dir)) {
260 if (!g_pattern_match_string(pspec, name))
263 GOwnPtr<gchar> entry(g_build_filename(filename.data(), name, NULL));
264 entries.append(filenameToString(entry.get()));
266 g_pattern_spec_free(pspec);
272 String openTemporaryFile(const String& prefix, PlatformFileHandle& handle)
274 GOwnPtr<gchar> filename(g_strdup_printf("%s%s", prefix.utf8().data(), createCanonicalUUIDString().utf8().data()));
275 GOwnPtr<gchar> tempPath(g_build_filename(g_get_tmp_dir(), filename.get(), NULL));
276 GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(tempPath.get()));
278 handle = g_file_create_readwrite(file.get(), G_FILE_CREATE_NONE, 0, 0);
279 if (!isHandleValid(handle))
281 return String::fromUTF8(tempPath.get());
284 PlatformFileHandle openFile(const String& path, FileOpenMode mode)
286 CString fsRep = fileSystemRepresentation(path);
288 return invalidPlatformFileHandle;
290 GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(fsRep.data()));
291 GFileIOStream* ioStream = 0;
292 if (mode == OpenForRead)
293 ioStream = g_file_open_readwrite(file.get(), 0, 0);
294 else if (mode == OpenForWrite) {
295 if (g_file_test(fsRep.data(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
296 ioStream = g_file_open_readwrite(file.get(), 0, 0);
298 ioStream = g_file_create_readwrite(file.get(), G_FILE_CREATE_NONE, 0, 0);
304 void closeFile(PlatformFileHandle& handle)
306 if (!isHandleValid(handle))
309 g_io_stream_close(G_IO_STREAM(handle), 0, 0);
310 g_object_unref(handle);
311 handle = invalidPlatformFileHandle;
314 long long seekFile(PlatformFileHandle handle, long long offset, FileSeekOrigin origin)
316 GSeekType seekType = G_SEEK_SET;
318 case SeekFromBeginning:
319 seekType = G_SEEK_SET;
321 case SeekFromCurrent:
322 seekType = G_SEEK_CUR;
325 seekType = G_SEEK_END;
328 ASSERT_NOT_REACHED();
331 if (!g_seekable_seek(G_SEEKABLE(g_io_stream_get_input_stream(G_IO_STREAM(handle))),
332 offset, seekType, 0, 0))
334 return g_seekable_tell(G_SEEKABLE(g_io_stream_get_input_stream(G_IO_STREAM(handle))));
337 int writeToFile(PlatformFileHandle handle, const char* data, int length)
340 g_output_stream_write_all(g_io_stream_get_output_stream(G_IO_STREAM(handle)),
341 data, length, &bytesWritten, 0, 0);
345 int readFromFile(PlatformFileHandle handle, char* data, int length)
347 GOwnPtr<GError> error;
349 gssize bytesRead = g_input_stream_read(g_io_stream_get_input_stream(G_IO_STREAM(handle)),
350 data, length, 0, &error.outPtr());
353 } while (error && error->code == G_FILE_ERROR_INTR);
357 bool unloadModule(PlatformModule module)
360 return ::FreeLibrary(module);
362 return g_module_close(module);