1/* 2 * Copyright (C) 2007 Staikos Computing Services Inc. 3 * Copyright (C) 2007 Holger Hans Peter Freyther 4 * Copyright (C) 2008 Apple, Inc. All rights reserved. 5 * Copyright (C) 2008 Collabora, Ltd. All rights reserved. 6 * Copyright (C) 2010 Sencha, Inc. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 18 * its contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include "config.h" 34#include "FileSystem.h" 35 36#include "PlatformString.h" 37#include <QDateTime> 38#include <QDir> 39#include <QFile> 40#include <QFileInfo> 41#include <QTemporaryFile> 42#include <wtf/text/CString.h> 43 44namespace WebCore { 45 46bool fileExists(const String& path) 47{ 48 return QFile::exists(path); 49} 50 51 52bool deleteFile(const String& path) 53{ 54 return QFile::remove(path); 55} 56 57bool deleteEmptyDirectory(const String& path) 58{ 59 return QDir::root().rmdir(path); 60} 61 62bool getFileSize(const String& path, long long& result) 63{ 64 QFileInfo info(path); 65 result = info.size(); 66 return info.exists(); 67} 68 69bool getFileModificationTime(const String& path, time_t& result) 70{ 71 QFileInfo info(path); 72 result = info.lastModified().toTime_t(); 73 return info.exists(); 74} 75 76bool makeAllDirectories(const String& path) 77{ 78 return QDir::root().mkpath(path); 79} 80 81String pathByAppendingComponent(const String& path, const String& component) 82{ 83 return QDir::toNativeSeparators(QDir(path).filePath(component)); 84} 85 86String homeDirectoryPath() 87{ 88 return QDir::homePath(); 89} 90 91String pathGetFileName(const String& path) 92{ 93 return QFileInfo(path).fileName(); 94} 95 96String directoryName(const String& path) 97{ 98 return QFileInfo(path).absolutePath(); 99} 100 101Vector<String> listDirectory(const String& path, const String& filter) 102{ 103 Vector<String> entries; 104 105 QStringList nameFilters; 106 if (!filter.isEmpty()) 107 nameFilters.append(filter); 108 QFileInfoList fileInfoList = QDir(path).entryInfoList(nameFilters, QDir::AllEntries | QDir::NoDotAndDotDot); 109 foreach (const QFileInfo fileInfo, fileInfoList) { 110 String entry = String(fileInfo.canonicalFilePath()); 111 entries.append(entry); 112 } 113 114 return entries; 115} 116 117String openTemporaryFile(const String& prefix, PlatformFileHandle& handle) 118{ 119#ifndef QT_NO_TEMPORARYFILE 120 QTemporaryFile* tempFile = new QTemporaryFile(QDir::tempPath() + QLatin1Char('/') + QString(prefix)); 121 tempFile->setAutoRemove(false); 122 QFile* temp = tempFile; 123 if (temp->open(QIODevice::ReadWrite)) { 124 handle = temp; 125 return temp->fileName(); 126 } 127#endif 128 handle = invalidPlatformFileHandle; 129 return String(); 130} 131 132PlatformFileHandle openFile(const String& path, FileOpenMode mode) 133{ 134 QIODevice::OpenMode platformMode; 135 136 if (mode == OpenForRead) 137 platformMode = QIODevice::ReadOnly; 138 else if (mode == OpenForWrite) 139 platformMode = (QIODevice::WriteOnly | QIODevice::Truncate); 140 else 141 return invalidPlatformFileHandle; 142 143 QFile* file = new QFile(path); 144 if (file->open(platformMode)) 145 return file; 146 147 return invalidPlatformFileHandle; 148} 149 150int readFromFile(PlatformFileHandle handle, char* data, int length) 151{ 152 if (handle && handle->exists() && handle->isReadable()) 153 return handle->read(data, length); 154 return 0; 155} 156 157void closeFile(PlatformFileHandle& handle) 158{ 159 if (handle) { 160 handle->close(); 161 delete handle; 162 } 163} 164 165long long seekFile(PlatformFileHandle handle, long long offset, FileSeekOrigin origin) 166{ 167 if (handle) { 168 long long current = 0; 169 170 switch (origin) { 171 case SeekFromBeginning: 172 break; 173 case SeekFromCurrent: 174 current = handle->pos(); 175 break; 176 case SeekFromEnd: 177 current = handle->size(); 178 break; 179 } 180 181 // Add the offset to the current position and seek to the new position 182 // Return our new position if the seek is successful 183 current += offset; 184 if (handle->seek(current)) 185 return current; 186 else 187 return -1; 188 } 189 190 return -1; 191} 192 193int writeToFile(PlatformFileHandle handle, const char* data, int length) 194{ 195 if (handle && handle->exists() && handle->isWritable()) 196 return handle->write(data, length); 197 198 return 0; 199} 200 201bool unloadModule(PlatformModule module) 202{ 203#if defined(Q_WS_MAC) 204 CFRelease(module); 205 return true; 206 207#elif defined(Q_OS_WIN) 208 return ::FreeLibrary(module); 209 210#else 211#ifndef QT_NO_LIBRARY 212 if (module->unload()) { 213 delete module; 214 return true; 215 } 216#endif 217 return false; 218#endif 219} 220 221} 222 223// vim: ts=4 sw=4 et 224