• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1.  Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  * 2.  Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17  *     its contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "config.h"
33 #include "FileSystem.h"
34 
35 #include "CString.h"
36 #include "NotImplemented.h"
37 #include "PlatformString.h"
38 
39 #include <QDateTime>
40 #include <QFile>
41 #include <QTemporaryFile>
42 #include <QFileInfo>
43 #include <QDateTime>
44 #include <QDir>
45 
46 namespace WebCore {
47 
fileExists(const String & path)48 bool fileExists(const String& path)
49 {
50     return QFile::exists(path);
51 }
52 
53 
deleteFile(const String & path)54 bool deleteFile(const String& path)
55 {
56     return QFile::remove(path);
57 }
58 
deleteEmptyDirectory(const String & path)59 bool deleteEmptyDirectory(const String& path)
60 {
61     return QDir::root().rmdir(path);
62 }
63 
getFileSize(const String & path,long long & result)64 bool getFileSize(const String& path, long long& result)
65 {
66     QFileInfo info(path);
67     result = info.size();
68     return info.exists();
69 }
70 
getFileModificationTime(const String & path,time_t & result)71 bool getFileModificationTime(const String& path, time_t& result)
72 {
73     QFileInfo info(path);
74     result = info.lastModified().toTime_t();
75     return info.exists();
76 }
77 
makeAllDirectories(const String & path)78 bool makeAllDirectories(const String& path)
79 {
80     return QDir::root().mkpath(path);
81 }
82 
pathByAppendingComponent(const String & path,const String & component)83 String pathByAppendingComponent(const String& path, const String& component)
84 {
85     return QDir(path).filePath(component);
86 }
87 
homeDirectoryPath()88 String homeDirectoryPath()
89 {
90     return QDir::homePath();
91 }
92 
pathGetFileName(const String & path)93 String pathGetFileName(const String& path)
94 {
95     return QFileInfo(path).fileName();
96 }
97 
directoryName(const String & path)98 String directoryName(const String& path)
99 {
100     return String(QFileInfo(path).baseName());
101 }
102 
listDirectory(const String & path,const String & filter)103 Vector<String> listDirectory(const String& path, const String& filter)
104 {
105     Vector<String> entries;
106 
107     QStringList nameFilters;
108     if (!filter.isEmpty())
109         nameFilters.append(filter);
110     QFileInfoList fileInfoList = QDir(path).entryInfoList(nameFilters, QDir::AllEntries | QDir::NoDotAndDotDot);
111     foreach (const QFileInfo fileInfo, fileInfoList) {
112         String entry = String(fileInfo.canonicalFilePath());
113         entries.append(entry);
114     }
115 
116     return entries;
117 }
118 
openTemporaryFile(const char * prefix,PlatformFileHandle & handle)119 CString openTemporaryFile(const char* prefix, PlatformFileHandle& handle)
120 {
121     QFile *temp = new QTemporaryFile(QLatin1String(prefix));
122     if (temp->open(QIODevice::ReadWrite)) {
123         handle = temp;
124         return String(temp->fileName()).utf8();
125     }
126     handle = invalidPlatformFileHandle;
127     return 0;
128 }
129 
closeFile(PlatformFileHandle & handle)130 void closeFile(PlatformFileHandle& handle)
131 {
132     if (handle) {
133         handle->close();
134         delete handle;
135     }
136 }
137 
writeToFile(PlatformFileHandle handle,const char * data,int length)138 int writeToFile(PlatformFileHandle handle, const char* data, int length)
139 {
140     if (handle && handle->exists() && handle->isWritable())
141         return handle->write(data, length);
142 
143     return 0;
144 }
145 
146 #if defined(Q_WS_X11) || defined(Q_WS_QWS)
unloadModule(PlatformModule module)147 bool unloadModule(PlatformModule module)
148 {
149     if (module->unload()) {
150         delete module;
151         return true;
152     }
153 
154     return false;
155 }
156 #endif
157 
158 #if defined(Q_WS_MAC)
unloadModule(PlatformModule module)159 bool unloadModule(PlatformModule module)
160 {
161     CFRelease(module);
162     return true;
163 }
164 #endif
165 
166 #if defined(Q_OS_WIN)
unloadModule(PlatformModule module)167 bool unloadModule(PlatformModule module)
168 {
169     return ::FreeLibrary(module);
170 }
171 #endif
172 
173 }
174 
175 // vim: ts=4 sw=4 et
176