author | ar <ar> | 2004-06-18 19:58:14 (UTC) |
---|---|---|
committer | ar <ar> | 2004-06-18 19:58:14 (UTC) |
commit | d45caef648bce4a73f6f847bc6e9aad125977deb (patch) (unidiff) | |
tree | 2e57aec5300028df4b90b1d07aeabd84e4ebc72f /library | |
parent | 589dc38a4c44ed7cdd151cf6c136b199ec273398 (diff) | |
download | opie-d45caef648bce4a73f6f847bc6e9aad125977deb.zip opie-d45caef648bce4a73f6f847bc6e9aad125977deb.tar.gz opie-d45caef648bce4a73f6f847bc6e9aad125977deb.tar.bz2 |
- use QFile::encodeName instead of (const char*) or latin1()
- use POSIX rename
-rw-r--r-- | library/filemanager.cpp | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/library/filemanager.cpp b/library/filemanager.cpp index 47af1c6..2e5efdd 100644 --- a/library/filemanager.cpp +++ b/library/filemanager.cpp | |||
@@ -1,328 +1,327 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #include "filemanager.h" | 21 | #include "filemanager.h" |
22 | #include "applnk.h" | 22 | #include "applnk.h" |
23 | 23 | ||
24 | /* QT */ | 24 | /* QT */ |
25 | #include <qdir.h> | 25 | #include <qdir.h> |
26 | #include <qfileinfo.h> | 26 | #include <qfileinfo.h> |
27 | #include <qtextstream.h> | 27 | #include <qtextstream.h> |
28 | 28 | ||
29 | /* STD */ | 29 | /* STD */ |
30 | #include <stdlib.h> | 30 | #include <stdlib.h> |
31 | #include <errno.h> | ||
31 | #include <sys/stat.h> | 32 | #include <sys/stat.h> |
32 | 33 | ||
33 | /*! | 34 | /*! |
34 | \class FileManager | 35 | \class FileManager |
35 | \brief The FileManager class assists with AppLnk input/output. | 36 | \brief The FileManager class assists with AppLnk input/output. |
36 | */ | 37 | */ |
37 | 38 | ||
38 | /*! | 39 | /*! |
39 | Constructs a FileManager. | 40 | Constructs a FileManager. |
40 | */ | 41 | */ |
41 | FileManager::FileManager() | 42 | FileManager::FileManager() |
42 | { | 43 | { |
43 | } | 44 | } |
44 | 45 | ||
45 | /*! | 46 | /*! |
46 | Destroys a FileManager. | 47 | Destroys a FileManager. |
47 | */ | 48 | */ |
48 | FileManager::~FileManager() | 49 | FileManager::~FileManager() |
49 | { | 50 | { |
50 | } | 51 | } |
51 | 52 | ||
52 | /*! | 53 | /*! |
53 | Saves \a data as the document specified by \a f. | 54 | Saves \a data as the document specified by \a f. |
54 | 55 | ||
55 | Returns whether the operation succeeded. | 56 | Returns whether the operation succeeded. |
56 | */ | 57 | */ |
57 | bool FileManager::saveFile( const DocLnk &f, const QByteArray &data ) | 58 | bool FileManager::saveFile( const DocLnk &f, const QByteArray &data ) |
58 | { | 59 | { |
59 | QString fileName = f.file() + ".new"; | 60 | QString fileName = f.file() + ".new"; |
60 | ensurePathExists( fileName ); | 61 | ensurePathExists( fileName ); |
61 | QFile file( fileName ); | 62 | QFile file( fileName ); |
62 | 63 | ||
63 | //write data in temporary .new file | 64 | //write data in temporary .new file |
64 | if ( !file.open( IO_WriteOnly|IO_Raw ) ) | 65 | if ( !file.open( IO_WriteOnly|IO_Raw ) ) |
65 | { | 66 | { |
66 | qWarning("open failed"); | 67 | qWarning( "open failed" ); |
67 | return FALSE; | 68 | return FALSE; |
68 | } | 69 | } |
69 | int total_written = file.writeBlock( data ); | 70 | int total_written = file.writeBlock( data ); |
70 | file.close(); | 71 | file.close(); |
71 | //check if every was written | 72 | //check if every was written |
72 | if ( total_written != int(data.size()) || !f.writeLink() ) | 73 | if ( total_written != int( data.size() ) || !f.writeLink() ) |
73 | { | 74 | { |
74 | QFile::remove( fileName ); | 75 | QFile::remove( fileName ); |
75 | return FALSE; | 76 | return FALSE; |
76 | } | 77 | } |
77 | qDebug("total written %d out of %d", total_written, data.size()); | 78 | qDebug( "total written %d out of %d", total_written, data.size() ); |
78 | 79 | ||
79 | //rename temporary .new file in original filenam | 80 | //rename temporary .new file in original filenam |
80 | if ( !renameFile( fileName, f.file() ) ) | 81 | if ( !renameFile( fileName, f.file() ) ) |
81 | QFile::remove( fileName); | 82 | QFile::remove( fileName); |
82 | return TRUE; | 83 | return TRUE; |
83 | } | 84 | } |
84 | 85 | ||
85 | /*! | 86 | /*! |
86 | Saves \a text as the document specified by \a f. | 87 | Saves \a text as the document specified by \a f. |
87 | 88 | ||
88 | The text is saved in UTF8 format. | 89 | The text is saved in UTF8 format. |
89 | 90 | ||
90 | Returns whether the operation succeeded. | 91 | Returns whether the operation succeeded. |
91 | */ | 92 | */ |
92 | bool FileManager::saveFile( const DocLnk &f, const QString &text ) | 93 | bool FileManager::saveFile( const DocLnk &f, const QString &text ) |
93 | { | 94 | { |
94 | QString fileName = f.file() + ".new"; | 95 | QString fileName = f.file() + ".new"; |
95 | ensurePathExists( fileName ); | 96 | ensurePathExists( fileName ); |
96 | QFile file( fileName ); | 97 | QFile file( fileName ); |
97 | 98 | ||
98 | //write data in temporary .new file | 99 | //write data in temporary .new file |
99 | if ( !file.open( IO_WriteOnly|IO_Raw ) ) | 100 | if ( !file.open( IO_WriteOnly|IO_Raw ) ) |
100 | { | 101 | { |
101 | qWarning("open failed"); | 102 | qWarning( "open failed" ); |
102 | return FALSE; | 103 | return FALSE; |
103 | } | 104 | } |
104 | 105 | ||
105 | QCString cstr = text.utf8(); | 106 | QCString cstr = text.utf8(); |
106 | int total_written; | 107 | int total_written; |
107 | total_written = file.writeBlock( cstr.data(), cstr.length() ); | 108 | total_written = file.writeBlock( cstr.data(), cstr.length() ); |
108 | file.close(); | 109 | file.close(); |
109 | if ( total_written != int(cstr.length()) || !f.writeLink() ) | 110 | if ( total_written != int( cstr.length()) || !f.writeLink() ) |
110 | { | 111 | { |
111 | QFile::remove( fileName ); | 112 | QFile::remove( fileName ); |
112 | return FALSE; | 113 | return FALSE; |
113 | } | 114 | } |
114 | 115 | ||
115 | // okay now rename the file.. | 116 | // okay now rename the file.. |
116 | if ( !renameFile( fileName, f.file() ) ) | 117 | if ( !renameFile( fileName, f.file() ) ) |
117 | QFile::remove( fileName); | 118 | QFile::remove( fileName); |
118 | return TRUE; | 119 | return TRUE; |
119 | } | 120 | } |
120 | 121 | ||
121 | 122 | ||
122 | /*! | 123 | /*! |
123 | Loads \a text from the document specified by \a f. | 124 | Loads \a text from the document specified by \a f. |
124 | 125 | ||
125 | The text is required to be in UTF8 format. | 126 | The text is required to be in UTF8 format. |
126 | 127 | ||
127 | Returns whether the operation succeeded. | 128 | Returns whether the operation succeeded. |
128 | */ | 129 | */ |
129 | bool FileManager::loadFile( const DocLnk &f, QString &text ) | 130 | bool FileManager::loadFile( const DocLnk &f, QString &text ) |
130 | { | 131 | { |
131 | QString fn = f.file(); | 132 | QString fn = f.file(); |
132 | QFile fl( fn ); | 133 | QFile fl( fn ); |
133 | if ( !fl.open( IO_ReadOnly ) ) | 134 | if ( !fl.open( IO_ReadOnly ) ) |
134 | return FALSE; | 135 | return FALSE; |
135 | QTextStream ts( &fl ); | 136 | QTextStream ts( &fl ); |
136 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | 137 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) |
137 | // The below should work, but doesn't in Qt 2.3.0 | 138 | // The below should work, but doesn't in Qt 2.3.0 |
138 | ts.setCodec( QTextCodec::codecForMib( 106 ) ); | 139 | ts.setCodec( QTextCodec::codecForMib( 106 ) ); |
139 | #else | 140 | #else |
140 | ts.setEncoding( QTextStream::UnicodeUTF8 ); | 141 | ts.setEncoding( QTextStream::UnicodeUTF8 ); |
141 | #endif | 142 | #endif |
142 | text = ts.read(); | 143 | text = ts.read(); |
143 | fl.close(); | 144 | fl.close(); |
144 | return TRUE; | 145 | return TRUE; |
145 | } | 146 | } |
146 | 147 | ||
147 | 148 | ||
148 | /*! | 149 | /*! |
149 | Loads \a ba from the document specified by \a f. | 150 | Loads \a ba from the document specified by \a f. |
150 | 151 | ||
151 | Returns whether the operation succeeded. | 152 | Returns whether the operation succeeded. |
152 | */ | 153 | */ |
153 | bool FileManager::loadFile( const DocLnk &f, QByteArray &ba ) | 154 | bool FileManager::loadFile( const DocLnk &f, QByteArray &ba ) |
154 | { | 155 | { |
155 | QString fn = f.file(); | 156 | QString fn = f.file(); |
156 | QFile fl( fn ); | 157 | QFile fl( fn ); |
157 | if ( !fl.open( IO_ReadOnly ) ) | 158 | if ( !fl.open( IO_ReadOnly ) ) |
158 | return FALSE; | 159 | return FALSE; |
159 | ba.resize( fl.size() ); | 160 | ba.resize( fl.size() ); |
160 | if ( fl.size() > 0 ) | 161 | if ( fl.size() > 0 ) |
161 | fl.readBlock( ba.data(), fl.size() ); | 162 | fl.readBlock( ba.data(), fl.size() ); |
162 | fl.close(); | 163 | fl.close(); |
163 | return TRUE; | 164 | return TRUE; |
164 | } | 165 | } |
165 | 166 | ||
166 | /*! | 167 | /*! |
167 | Copies the document specified by \a src to the document specified | 168 | Copies the document specified by \a src to the document specified |
168 | by \a dest. | 169 | by \a dest. |
169 | 170 | ||
170 | Returns whether the operation succeeded. | 171 | Returns whether the operation succeeded. |
171 | */ | 172 | */ |
172 | bool FileManager::copyFile( const AppLnk &src, const AppLnk &dest ) | 173 | bool FileManager::copyFile( const AppLnk &src, const AppLnk &dest ) |
173 | { | 174 | { |
174 | QFile srcFile( src.file() ); | 175 | QFile srcFile( src.file() ); |
175 | if ( !srcFile.open( IO_ReadOnly ) ) | 176 | if ( !srcFile.open( IO_ReadOnly ) ) |
176 | return FALSE; | 177 | return FALSE; |
177 | 178 | ||
178 | QString fileName = dest.file() + ".new"; | 179 | QString fileName = dest.file() + ".new"; |
179 | 180 | ||
180 | ensurePathExists( fileName ); | 181 | ensurePathExists( fileName ); |
181 | 182 | ||
182 | bool ok = TRUE; | 183 | bool ok = TRUE; |
183 | ok = copyFile( src.file(), fileName ); | 184 | ok = copyFile( src.file(), fileName ); |
184 | 185 | ||
185 | if ( ok ) | 186 | if ( ok ) |
186 | ok = dest.writeLink(); | 187 | ok = dest.writeLink(); |
187 | 188 | ||
188 | if ( ok ) | 189 | if ( ok ) |
189 | { | 190 | { |
190 | // okay now rename the file... | 191 | // okay now rename the file... |
191 | if ( !renameFile( fileName.latin1(), dest.file().latin1() ) ) | 192 | if ( !renameFile( fileName, dest.file() ) ) |
192 | // remove the tmp file, otherwise, it will just lay around... | 193 | // remove the tmp file, otherwise, it will just lay around... |
193 | QFile::remove( fileName.latin1() ); | 194 | QFile::remove( fileName ); |
194 | } | 195 | } |
195 | else | 196 | else |
196 | { | 197 | { |
197 | QFile::remove( fileName.latin1() ); | 198 | QFile::remove( fileName ); |
198 | } | 199 | } |
199 | return ok; | 200 | return ok; |
200 | } | 201 | } |
201 | 202 | ||
202 | bool FileManager::copyFile( const QString & src, const QString & dest ) | 203 | bool FileManager::copyFile( const QString & src, const QString & dest ) |
203 | { | 204 | { |
204 | //open read file | 205 | //open read file |
205 | QFile srcFile( src ); | 206 | QFile srcFile( src ); |
206 | if( !srcFile.open( IO_ReadOnly|IO_Raw) ) | 207 | if( !srcFile.open( IO_ReadOnly|IO_Raw) ) |
207 | { | 208 | { |
208 | qWarning( "open read failed %s, %s", src.latin1(), dest.latin1() ); | 209 | qWarning( "open read failed %s, %s", src.latin1(), dest.latin1() ); |
209 | return FALSE; | 210 | return FALSE; |
210 | } | 211 | } |
211 | 212 | ||
212 | //open write file | 213 | //open write file |
213 | QFile destFile( dest ); | 214 | QFile destFile( dest ); |
214 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) | 215 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) |
215 | { | 216 | { |
216 | qWarning( "open write failed %s, %s", src.latin1(), dest.latin1() ); | 217 | qWarning( "open write failed %s, %s", src.latin1(), dest.latin1() ); |
217 | srcFile.close(); | 218 | srcFile.close(); |
218 | return FALSE; | 219 | return FALSE; |
219 | } | 220 | } |
220 | 221 | ||
221 | //copy content | 222 | //copy content |
222 | const int bufsize = 16384; | 223 | const int bufsize = 16384; |
223 | char buffer[bufsize]; | 224 | char buffer[bufsize]; |
224 | bool ok = TRUE; | 225 | bool ok = TRUE; |
225 | int bytesRead = 0; | 226 | int bytesRead = 0; |
226 | while ( ok && !srcFile.atEnd() ) | 227 | while ( ok && !srcFile.atEnd() ) |
227 | { | 228 | { |
228 | bytesRead = srcFile.readBlock( buffer, bufsize ); | 229 | bytesRead = srcFile.readBlock( buffer, bufsize ); |
229 | if ( bytesRead < 0 ) | 230 | if ( bytesRead < 0 ) |
230 | ok = FALSE; | 231 | ok = FALSE; |
231 | while ( ok && bytesRead > 0 ) | 232 | while ( ok && bytesRead > 0 ) |
232 | { | 233 | { |
233 | int bytesWritten = destFile.writeBlock( buffer, bytesRead ); | 234 | int bytesWritten = destFile.writeBlock( buffer, bytesRead ); |
234 | if ( bytesWritten < 0 ) | 235 | if ( bytesWritten < 0 ) |
235 | ok = FALSE; | 236 | ok = FALSE; |
236 | else | 237 | else |
237 | bytesRead -= bytesWritten; | 238 | bytesRead -= bytesWritten; |
238 | } | 239 | } |
239 | } | 240 | } |
240 | srcFile.close(); | 241 | srcFile.close(); |
241 | destFile.close(); | 242 | destFile.close(); |
242 | // Set file permissions | 243 | // Set file permissions |
243 | struct stat status; | 244 | struct stat status; |
244 | if( stat( (const char *) src, &status ) == 0 ) | 245 | if( stat( QFile::encodeName( src ), &status ) == 0 ) |
245 | { | 246 | { |
246 | chmod( (const char *) dest, status.st_mode ); | 247 | chmod( QFile::encodeName( dest ), status.st_mode ); |
247 | } | 248 | } |
248 | return ok; | 249 | return ok; |
249 | } | 250 | } |
250 | 251 | ||
251 | 252 | ||
252 | bool FileManager::renameFile( const QString & src, const QString & dest ) | 253 | bool FileManager::renameFile( const QString & src, const QString & dest ) |
253 | { | 254 | { |
254 | QDir dir( QFileInfo( src ).absFilePath() ); | 255 | if( rename( QFile::encodeName( src ), QFile::encodeName( dest ) ) == -1); |
255 | if ( !dir.rename( src, dest ) ) | ||
256 | { | 256 | { |
257 | qWarning( "problem renaming file %s to %s", src, dest ); | 257 | qWarning( "problem renaming file %s to %s, errno: %d", src.latin1(), dest.latin1(), errno ); |
258 | return false; | 258 | return false; |
259 | } | 259 | } |
260 | return true; | 260 | return true; |
261 | } | 261 | } |
262 | 262 | ||
263 | /*! | 263 | /*! |
264 | Opens the document specified by \a f as a readable QIODevice. | 264 | Opens the document specified by \a f as a readable QIODevice. |
265 | The caller must delete the return value. | 265 | The caller must delete the return value. |
266 | 266 | ||
267 | Returns 0 if the operation fails. | 267 | Returns 0 if the operation fails. |
268 | */ | 268 | */ |
269 | QIODevice* FileManager::openFile( const DocLnk& f ) | 269 | QIODevice* FileManager::openFile( const DocLnk& f ) |
270 | { | 270 | { |
271 | QString fn = f.file(); | 271 | QString fn = f.file(); |
272 | QFile* fl = new QFile( fn ); | 272 | QFile* fl = new QFile( fn ); |
273 | if ( !fl->open( IO_ReadOnly ) ) | 273 | if ( !fl->open( IO_ReadOnly ) ) |
274 | { | 274 | { |
275 | delete fl; | 275 | delete fl; |
276 | fl = 0; | 276 | fl = 0; |
277 | } | 277 | } |
278 | return fl; | 278 | return fl; |
279 | } | 279 | } |
280 | 280 | ||
281 | /*! | 281 | /*! |
282 | Opens the document specified by \a f as a writable QIODevice. | 282 | Opens the document specified by \a f as a writable QIODevice. |
283 | The caller must delete the return value. | 283 | The caller must delete the return value. |
284 | 284 | ||
285 | Returns 0 if the operation fails. | 285 | Returns 0 if the operation fails. |
286 | */ | 286 | */ |
287 | QIODevice* FileManager::saveFile( const DocLnk& f ) | 287 | QIODevice* FileManager::saveFile( const DocLnk& f ) |
288 | { | 288 | { |
289 | QString fn = f.file(); | 289 | QString fn = f.file(); |
290 | ensurePathExists( fn ); | 290 | ensurePathExists( fn ); |
291 | QFile* fl = new QFile( fn ); | 291 | QFile* fl = new QFile( fn ); |
292 | if ( fl->open( IO_WriteOnly ) ) | 292 | if ( fl->open( IO_WriteOnly ) ) |
293 | { | 293 | { |
294 | f.writeLink(); | 294 | f.writeLink(); |
295 | } | 295 | } |
296 | else | 296 | else |
297 | { | 297 | { |
298 | delete fl; | 298 | delete fl; |
299 | fl = 0; | 299 | fl = 0; |
300 | } | 300 | } |
301 | return fl; | 301 | return fl; |
302 | } | 302 | } |
303 | 303 | ||
304 | /*! | 304 | /*! |
305 | Returns whether the document specified by \a f current exists | 305 | Returns whether the document specified by \a f current exists |
306 | as a file on disk. | 306 | as a file on disk. |
307 | */ | 307 | */ |
308 | bool FileManager::exists( const DocLnk &f ) | 308 | bool FileManager::exists( const DocLnk &f ) |
309 | { | 309 | { |
310 | return QFile::exists(f.file()); | 310 | return QFile::exists(f.file()); |
311 | } | 311 | } |
312 | 312 | ||
313 | /*! | 313 | /*! |
314 | Ensures that the path \a fn exists, by creating required directories. | 314 | Ensures that the path \a fn exists, by creating required directories. |
315 | Returns TRUE if successful. | 315 | Returns TRUE if successful. |
316 | */ | 316 | */ |
317 | bool FileManager::ensurePathExists( const QString &fn ) | 317 | bool FileManager::ensurePathExists( const QString &fn ) |
318 | { | 318 | { |
319 | QFileInfo fi(fn); | 319 | QFileInfo fi(fn); |
320 | fi.setFile( fi.dirPath(TRUE) ); | 320 | fi.setFile( fi.dirPath(TRUE) ); |
321 | if ( !fi.exists() ) | 321 | if ( !fi.exists() ) |
322 | { | 322 | { |
323 | if ( system(("mkdir -p "+fi.filePath())) ) | 323 | if ( system( ("mkdir -p " + QFile::encodeName( fi.filePath() ) ) ) ) |
324 | return FALSE; | 324 | return FALSE; |
325 | } | 325 | } |
326 | |||
327 | return TRUE; | 326 | return TRUE; |
328 | } | 327 | } |