author | ar <ar> | 2004-06-13 17:46:33 (UTC) |
---|---|---|
committer | ar <ar> | 2004-06-13 17:46:33 (UTC) |
commit | ecf1fb42bef9d64869349abd4e550f0b6d2cfa59 (patch) (unidiff) | |
tree | 2001746dd484b155d04b7f1500b14538bf0b8b08 | |
parent | 07d700581b9f608d3105eb8d82d9b4cfa0084317 (diff) | |
download | opie-ecf1fb42bef9d64869349abd4e550f0b6d2cfa59.zip opie-ecf1fb42bef9d64869349abd4e550f0b6d2cfa59.tar.gz opie-ecf1fb42bef9d64869349abd4e550f0b6d2cfa59.tar.bz2 |
- fix error-handling again ;)
-rw-r--r-- | library/filemanager.cpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/library/filemanager.cpp b/library/filemanager.cpp index 99837fe..adfe590 100644 --- a/library/filemanager.cpp +++ b/library/filemanager.cpp | |||
@@ -1,440 +1,440 @@ | |||
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 | #include "filemanager.h" | 20 | #include "filemanager.h" |
21 | #include "applnk.h" | 21 | #include "applnk.h" |
22 | 22 | ||
23 | #include <qfileinfo.h> | 23 | #include <qfileinfo.h> |
24 | #include <qtextstream.h> | 24 | #include <qtextstream.h> |
25 | 25 | ||
26 | #include <errno.h> | 26 | #include <errno.h> |
27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
28 | #include <unistd.h> | 28 | #include <unistd.h> |
29 | #include <sys/stat.h> | 29 | #include <sys/stat.h> |
30 | #include <dirent.h> | 30 | #include <dirent.h> |
31 | #ifdef Q_OS_MACX | 31 | #ifdef Q_OS_MACX |
32 | // MacOS X does not have sendfile.. :( | 32 | // MacOS X does not have sendfile.. :( |
33 | // But maybe in the future.. !? | 33 | // But maybe in the future.. !? |
34 | # ifdef SENDFILE | 34 | # ifdef SENDFILE |
35 | # include <sys/types.h> | 35 | # include <sys/types.h> |
36 | # include <sys/socket.h> | 36 | # include <sys/socket.h> |
37 | # endif | 37 | # endif |
38 | #else | 38 | #else |
39 | # include <sys/sendfile.h> | 39 | # include <sys/sendfile.h> |
40 | #endif /* Q_OS_MACX */ | 40 | #endif /* Q_OS_MACX */ |
41 | #include <fcntl.h> | 41 | #include <fcntl.h> |
42 | 42 | ||
43 | /*! | 43 | /*! |
44 | \class FileManager | 44 | \class FileManager |
45 | \brief The FileManager class assists with AppLnk input/output. | 45 | \brief The FileManager class assists with AppLnk input/output. |
46 | */ | 46 | */ |
47 | 47 | ||
48 | /*! | 48 | /*! |
49 | Constructs a FileManager. | 49 | Constructs a FileManager. |
50 | */ | 50 | */ |
51 | FileManager::FileManager() | 51 | FileManager::FileManager() |
52 | { | 52 | { |
53 | } | 53 | } |
54 | 54 | ||
55 | /*! | 55 | /*! |
56 | Destroys a FileManager. | 56 | Destroys a FileManager. |
57 | */ | 57 | */ |
58 | FileManager::~FileManager() | 58 | FileManager::~FileManager() |
59 | { | 59 | { |
60 | 60 | ||
61 | } | 61 | } |
62 | 62 | ||
63 | /*! | 63 | /*! |
64 | Saves \a data as the document specified by \a f. | 64 | Saves \a data as the document specified by \a f. |
65 | 65 | ||
66 | Returns whether the operation succeeded. | 66 | Returns whether the operation succeeded. |
67 | */ | 67 | */ |
68 | bool FileManager::saveFile( const DocLnk &f, const QByteArray &data ) | 68 | bool FileManager::saveFile( const DocLnk &f, const QByteArray &data ) |
69 | { | 69 | { |
70 | QString fn = f.file() + ".new"; | 70 | QString fn = f.file() + ".new"; |
71 | ensurePathExists( fn ); | 71 | ensurePathExists( fn ); |
72 | QFile fl( fn ); | 72 | QFile fl( fn ); |
73 | if ( !fl.open( IO_WriteOnly|IO_Raw ) ) { | 73 | if ( !fl.open( IO_WriteOnly|IO_Raw ) ) { |
74 | qWarning("open failed"); | 74 | qWarning("open failed"); |
75 | return FALSE; | 75 | return FALSE; |
76 | } | 76 | } |
77 | int total_written = fl.writeBlock( data ); | 77 | int total_written = fl.writeBlock( data ); |
78 | fl.close(); | 78 | fl.close(); |
79 | if ( total_written != int(data.size()) || !f.writeLink() ) { | 79 | if ( total_written != int(data.size()) || !f.writeLink() ) { |
80 | QFile::remove( fn ); | 80 | QFile::remove( fn ); |
81 | return FALSE; | 81 | return FALSE; |
82 | } | 82 | } |
83 | qDebug("total written %d out of %d", total_written, data.size()); | 83 | qDebug("total written %d out of %d", total_written, data.size()); |
84 | // else rename the file... | 84 | // else rename the file... |
85 | if ( !renameFile( fn.latin1(), f.file().latin1() ) ) { | 85 | if ( !renameFile( fn.latin1(), f.file().latin1() ) ) { |
86 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), | 86 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), |
87 | f.file().latin1(), errno ); | 87 | f.file().latin1(), errno ); |
88 | // remove the file... | 88 | // remove the file... |
89 | } | 89 | } |
90 | return TRUE; | 90 | return TRUE; |
91 | } | 91 | } |
92 | 92 | ||
93 | /*! | 93 | /*! |
94 | Saves \a text as the document specified by \a f. | 94 | Saves \a text as the document specified by \a f. |
95 | 95 | ||
96 | The text is saved in UTF8 format. | 96 | The text is saved in UTF8 format. |
97 | 97 | ||
98 | Returns whether the operation succeeded. | 98 | Returns whether the operation succeeded. |
99 | */ | 99 | */ |
100 | bool FileManager::saveFile( const DocLnk &f, const QString &text ) | 100 | bool FileManager::saveFile( const DocLnk &f, const QString &text ) |
101 | { | 101 | { |
102 | QString fn = f.file() + ".new"; | 102 | QString fn = f.file() + ".new"; |
103 | ensurePathExists( fn ); | 103 | ensurePathExists( fn ); |
104 | QFile fl( fn ); | 104 | QFile fl( fn ); |
105 | if ( !fl.open( IO_WriteOnly|IO_Raw ) ) { | 105 | if ( !fl.open( IO_WriteOnly|IO_Raw ) ) { |
106 | qWarning("open failed"); | 106 | qWarning("open failed"); |
107 | return FALSE; | 107 | return FALSE; |
108 | } | 108 | } |
109 | 109 | ||
110 | QCString cstr = text.utf8(); | 110 | QCString cstr = text.utf8(); |
111 | int total_written; | 111 | int total_written; |
112 | total_written = fl.writeBlock( cstr.data(), cstr.length() ); | 112 | total_written = fl.writeBlock( cstr.data(), cstr.length() ); |
113 | fl.close(); | 113 | fl.close(); |
114 | if ( total_written != int(cstr.length()) || !f.writeLink() ) { | 114 | if ( total_written != int(cstr.length()) || !f.writeLink() ) { |
115 | QFile::remove( fn ); | 115 | QFile::remove( fn ); |
116 | return FALSE; | 116 | return FALSE; |
117 | } | 117 | } |
118 | // okay now rename the file.. | 118 | // okay now rename the file.. |
119 | if ( !renameFile( fn.latin1(), f.file().latin1() ) ) { | 119 | if ( !renameFile( fn.latin1(), f.file().latin1() ) ) { |
120 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), | 120 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), |
121 | f.file().latin1(), errno ); | 121 | f.file().latin1(), errno ); |
122 | 122 | ||
123 | } | 123 | } |
124 | return TRUE; | 124 | return TRUE; |
125 | } | 125 | } |
126 | 126 | ||
127 | 127 | ||
128 | /*! | 128 | /*! |
129 | Loads \a text from the document specified by \a f. | 129 | Loads \a text from the document specified by \a f. |
130 | 130 | ||
131 | The text is required to be in UTF8 format. | 131 | The text is required to be in UTF8 format. |
132 | 132 | ||
133 | Returns whether the operation succeeded. | 133 | Returns whether the operation succeeded. |
134 | */ | 134 | */ |
135 | bool FileManager::loadFile( const DocLnk &f, QString &text ) | 135 | bool FileManager::loadFile( const DocLnk &f, QString &text ) |
136 | { | 136 | { |
137 | QString fn = f.file(); | 137 | QString fn = f.file(); |
138 | QFile fl( fn ); | 138 | QFile fl( fn ); |
139 | if ( !fl.open( IO_ReadOnly ) ) | 139 | if ( !fl.open( IO_ReadOnly ) ) |
140 | return FALSE; | 140 | return FALSE; |
141 | QTextStream ts( &fl ); | 141 | QTextStream ts( &fl ); |
142 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | 142 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) |
143 | // The below should work, but doesn't in Qt 2.3.0 | 143 | // The below should work, but doesn't in Qt 2.3.0 |
144 | ts.setCodec( QTextCodec::codecForMib( 106 ) ); | 144 | ts.setCodec( QTextCodec::codecForMib( 106 ) ); |
145 | #else | 145 | #else |
146 | ts.setEncoding( QTextStream::UnicodeUTF8 ); | 146 | ts.setEncoding( QTextStream::UnicodeUTF8 ); |
147 | #endif | 147 | #endif |
148 | text = ts.read(); | 148 | text = ts.read(); |
149 | fl.close(); | 149 | fl.close(); |
150 | return TRUE; | 150 | return TRUE; |
151 | } | 151 | } |
152 | 152 | ||
153 | 153 | ||
154 | /*! | 154 | /*! |
155 | Loads \a ba from the document specified by \a f. | 155 | Loads \a ba from the document specified by \a f. |
156 | 156 | ||
157 | Returns whether the operation succeeded. | 157 | Returns whether the operation succeeded. |
158 | */ | 158 | */ |
159 | bool FileManager::loadFile( const DocLnk &f, QByteArray &ba ) | 159 | bool FileManager::loadFile( const DocLnk &f, QByteArray &ba ) |
160 | { | 160 | { |
161 | QString fn = f.file(); | 161 | QString fn = f.file(); |
162 | QFile fl( fn ); | 162 | QFile fl( fn ); |
163 | if ( !fl.open( IO_ReadOnly ) ) | 163 | if ( !fl.open( IO_ReadOnly ) ) |
164 | return FALSE; | 164 | return FALSE; |
165 | ba.resize( fl.size() ); | 165 | ba.resize( fl.size() ); |
166 | if ( fl.size() > 0 ) | 166 | if ( fl.size() > 0 ) |
167 | fl.readBlock( ba.data(), fl.size() ); | 167 | fl.readBlock( ba.data(), fl.size() ); |
168 | fl.close(); | 168 | fl.close(); |
169 | return TRUE; | 169 | return TRUE; |
170 | } | 170 | } |
171 | 171 | ||
172 | /*! | 172 | /*! |
173 | Copies the document specified by \a src to the document specified | 173 | Copies the document specified by \a src to the document specified |
174 | by \a dest. | 174 | by \a dest. |
175 | 175 | ||
176 | Returns whether the operation succeeded. | 176 | Returns whether the operation succeeded. |
177 | */ | 177 | */ |
178 | bool FileManager::copyFile( const AppLnk &src, const AppLnk &dest ) | 178 | bool FileManager::copyFile( const AppLnk &src, const AppLnk &dest ) |
179 | { | 179 | { |
180 | QFile sf( src.file() ); | 180 | QFile sf( src.file() ); |
181 | if ( !sf.open( IO_ReadOnly ) ) | 181 | if ( !sf.open( IO_ReadOnly ) ) |
182 | return FALSE; | 182 | return FALSE; |
183 | 183 | ||
184 | QString fn = dest.file() + ".new"; | 184 | QString fn = dest.file() + ".new"; |
185 | ensurePathExists( fn ); | 185 | ensurePathExists( fn ); |
186 | QFile df( fn ); | 186 | QFile df( fn ); |
187 | if ( !df.open( IO_WriteOnly|IO_Raw ) ) | 187 | if ( !df.open( IO_WriteOnly|IO_Raw ) ) |
188 | return FALSE; | 188 | return FALSE; |
189 | 189 | ||
190 | const int bufsize = 16384; | 190 | const int bufsize = 16384; |
191 | char buffer[bufsize]; | 191 | char buffer[bufsize]; |
192 | bool ok = TRUE; | 192 | bool ok = TRUE; |
193 | int bytesRead = 0; | 193 | int bytesRead = 0; |
194 | while ( ok && !sf.atEnd() ) { | 194 | while ( ok && !sf.atEnd() ) { |
195 | bytesRead = sf.readBlock( buffer, bufsize ); | 195 | bytesRead = sf.readBlock( buffer, bufsize ); |
196 | if ( bytesRead < 0 ) | 196 | if ( bytesRead < 0 ) |
197 | ok = FALSE; | 197 | ok = FALSE; |
198 | while ( ok && bytesRead > 0 ) { | 198 | while ( ok && bytesRead > 0 ) { |
199 | int bytesWritten = df.writeBlock( buffer, bytesRead ); | 199 | int bytesWritten = df.writeBlock( buffer, bytesRead ); |
200 | if ( bytesWritten < 0 ) | 200 | if ( bytesWritten < 0 ) |
201 | ok = FALSE; | 201 | ok = FALSE; |
202 | else | 202 | else |
203 | bytesRead -= bytesWritten; | 203 | bytesRead -= bytesWritten; |
204 | } | 204 | } |
205 | } | 205 | } |
206 | 206 | ||
207 | if ( ok ) | 207 | if ( ok ) |
208 | ok = dest.writeLink(); | 208 | ok = dest.writeLink(); |
209 | 209 | ||
210 | if ( ok ) { | 210 | if ( ok ) { |
211 | // okay now rename the file... | 211 | // okay now rename the file... |
212 | if ( !renameFile( fn.latin1(), dest.file().latin1() ) ) { | 212 | if ( !renameFile( fn.latin1(), dest.file().latin1() ) ) { |
213 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), | 213 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), |
214 | dest.file().latin1(), errno ); | 214 | dest.file().latin1(), errno ); |
215 | // remove the tmp file, otherwise, it will just lay around... | 215 | // remove the tmp file, otherwise, it will just lay around... |
216 | QFile::remove( fn.latin1() ); | 216 | QFile::remove( fn.latin1() ); |
217 | } | 217 | } |
218 | } else { | 218 | } else { |
219 | QFile::remove( fn.latin1() ); | 219 | QFile::remove( fn.latin1() ); |
220 | } | 220 | } |
221 | 221 | ||
222 | return ok; | 222 | return ok; |
223 | } | 223 | } |
224 | 224 | ||
225 | bool FileManager::copyFile( const QString & src, const QString & dest ) { | 225 | bool FileManager::copyFile( const QString & src, const QString & dest ) { |
226 | bool success = true; | 226 | bool success = true; |
227 | struct stat status; | 227 | struct stat status; |
228 | int read_fd=0; | 228 | int read_fd=0; |
229 | int write_fd=0; | 229 | int write_fd=0; |
230 | struct stat stat_buf; | 230 | struct stat stat_buf; |
231 | off_t offset = 0; | 231 | off_t offset = 0; |
232 | QFile srcFile(src); | 232 | QFile srcFile(src); |
233 | QFile destFile(dest); | 233 | QFile destFile(dest); |
234 | 234 | ||
235 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { | 235 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { |
236 | return success = false; | 236 | return success = false; |
237 | } | 237 | } |
238 | read_fd = srcFile.handle(); | 238 | read_fd = srcFile.handle(); |
239 | if(read_fd != -1) { | 239 | if(read_fd != -1) { |
240 | fstat (read_fd, &stat_buf); | 240 | fstat (read_fd, &stat_buf); |
241 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) | 241 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) |
242 | return success = false; | 242 | return success = false; |
243 | write_fd = destFile.handle(); | 243 | write_fd = destFile.handle(); |
244 | if(write_fd != -1) { | 244 | if(write_fd != -1) { |
245 | int err=0; | 245 | int err=0; |
246 | QString msg; | 246 | QString msg; |
247 | #ifdef Q_OS_MACX | 247 | #ifdef Q_OS_MACX |
248 | #ifdef SENDFILE | 248 | #ifdef SENDFILE |
249 | /* FreeBSD does support a different kind of | 249 | /* FreeBSD does support a different kind of |
250 | * sendfile. (eilers) | 250 | * sendfile. (eilers) |
251 | * I took this from Very Secure FTPd | 251 | * I took this from Very Secure FTPd |
252 | * Licence: GPL | 252 | * Licence: GPL |
253 | * Author: Chris Evans | 253 | * Author: Chris Evans |
254 | * sysdeputil.c | 254 | * sysdeputil.c |
255 | */ | 255 | */ |
256 | /* XXX - start_pos will truncate on 32-bit machines - can we | 256 | /* XXX - start_pos will truncate on 32-bit machines - can we |
257 | * say "start from current pos"? | 257 | * say "start from current pos"? |
258 | */ | 258 | */ |
259 | off_t written = 0; | 259 | off_t written = 0; |
260 | int retval = 0; | 260 | int retval = 0; |
261 | retval = sendfile(read_fd, write_fd, offset, stat_buf.st_size, NULL, | 261 | retval = sendfile(read_fd, write_fd, offset, stat_buf.st_size, NULL, |
262 | &written, 0); | 262 | &written, 0); |
263 | /* Translate to Linux-like retval */ | 263 | /* Translate to Linux-like retval */ |
264 | if (written > 0) | 264 | if (written > 0) |
265 | { | 265 | { |
266 | err = (int) written; | 266 | err = (int) written; |
267 | } | 267 | } |
268 | #else /* SENDFILE */ | 268 | #else /* SENDFILE */ |
269 | err == -1; | 269 | err == -1; |
270 | msg = "FAILURE: Using unsupported function \"sendfile()\" Need Workaround !!"; | 270 | msg = "FAILURE: Using unsupported function \"sendfile()\" Need Workaround !!"; |
271 | success = false; | 271 | success = false; |
272 | # warning "Need workaround for sendfile!!(eilers)" | 272 | # warning "Need workaround for sendfile!!(eilers)" |
273 | #endif /* SENDFILE */ | 273 | #endif /* SENDFILE */ |
274 | 274 | ||
275 | #else | 275 | #else |
276 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); | 276 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); |
277 | if( err != -1) { | 277 | if( err == -1) { |
278 | switch(err) { | 278 | switch(errno) { |
279 | case EBADF : msg = "The input file was not opened for reading or the output file was not opened for writing. "; | 279 | case EBADF : msg = "The input file was not opened for reading or the output file was not opened for writing. "; |
280 | case EINVAL: msg = "Descriptor is not valid or locked. "; | 280 | case EINVAL: msg = "Descriptor is not valid or locked. "; |
281 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; | 281 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; |
282 | case EIO: msg = "Unspecified error while reading from in_fd."; | 282 | case EIO: msg = "Unspecified error while reading from in_fd."; |
283 | }; | 283 | }; |
284 | success = false; | 284 | success = false; |
285 | } | 285 | } |
286 | #endif /* Q_OS_MACX */ | 286 | #endif /* Q_OS_MACX */ |
287 | if( !success ) | 287 | if( !success ) |
288 | qWarning( msg ); | 288 | qWarning( msg ); |
289 | } else { | 289 | } else { |
290 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); | 290 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); |
291 | success = false; | 291 | success = false; |
292 | } | 292 | } |
293 | } else { | 293 | } else { |
294 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); | 294 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); |
295 | success = false; | 295 | success = false; |
296 | } | 296 | } |
297 | srcFile.close(); | 297 | srcFile.close(); |
298 | destFile.close(); | 298 | destFile.close(); |
299 | // Set file permissions | 299 | // Set file permissions |
300 | if( stat( (const char *) src, &status ) == 0 ) { | 300 | if( stat( (const char *) src, &status ) == 0 ) { |
301 | chmod( (const char *) dest, status.st_mode ); | 301 | chmod( (const char *) dest, status.st_mode ); |
302 | } | 302 | } |
303 | 303 | ||
304 | return success; | 304 | return success; |
305 | } | 305 | } |
306 | 306 | ||
307 | 307 | ||
308 | bool FileManager::renameFile( const QString & src, const QString & dest ) { | 308 | bool FileManager::renameFile( const QString & src, const QString & dest ) { |
309 | if(copyFile( src, dest )) { | 309 | if(copyFile( src, dest )) { |
310 | if(QFile::remove(src) ) { | 310 | if(QFile::remove(src) ) { |
311 | return true; | 311 | return true; |
312 | } | 312 | } |
313 | } | 313 | } |
314 | return false; | 314 | return false; |
315 | } | 315 | } |
316 | 316 | ||
317 | /* | 317 | /* |
318 | bool FileManager::copyFile( const QString & src, const QString & dest ) { | 318 | bool FileManager::copyFile( const QString & src, const QString & dest ) { |
319 | bool success = true; | 319 | bool success = true; |
320 | struct stat status; | 320 | struct stat status; |
321 | int read_fd=0; | 321 | int read_fd=0; |
322 | int write_fd=0; | 322 | int write_fd=0; |
323 | struct stat stat_buf; | 323 | struct stat stat_buf; |
324 | off_t offset = 0; | 324 | off_t offset = 0; |
325 | QFile srcFile(src); | 325 | QFile srcFile(src); |
326 | QFile destFile(dest); | 326 | QFile destFile(dest); |
327 | 327 | ||
328 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { | 328 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { |
329 | return success = false; | 329 | return success = false; |
330 | } | 330 | } |
331 | read_fd = srcFile.handle(); | 331 | read_fd = srcFile.handle(); |
332 | if(read_fd != -1) { | 332 | if(read_fd != -1) { |
333 | fstat (read_fd, &stat_buf); | 333 | fstat (read_fd, &stat_buf); |
334 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) | 334 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) |
335 | return success = false; | 335 | return success = false; |
336 | write_fd = destFile.handle(); | 336 | write_fd = destFile.handle(); |
337 | if(write_fd != -1) { | 337 | if(write_fd != -1) { |
338 | int err=0; | 338 | int err=0; |
339 | QString msg; | 339 | QString msg; |
340 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); | 340 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); |
341 | if( err == -1) { | 341 | if( err == -1) { |
342 | switch(err) { | 342 | switch(err) { |
343 | case EBADF : msg = "The input file was not opened for reading or the output file was not opened for writing. "; | 343 | case EBADF : msg = "The input file was not opened for reading or the output file was not opened for writing. "; |
344 | case EINVAL: msg = "Descriptor is not valid or locked. "; | 344 | case EINVAL: msg = "Descriptor is not valid or locked. "; |
345 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; | 345 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; |
346 | case EIO: msg = "Unspecified error while reading from in_fd."; | 346 | case EIO: msg = "Unspecified error while reading from in_fd."; |
347 | }; | 347 | }; |
348 | success = false; | 348 | success = false; |
349 | } | 349 | } |
350 | } else { | 350 | } else { |
351 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); | 351 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); |
352 | success = false; | 352 | success = false; |
353 | } | 353 | } |
354 | } else { | 354 | } else { |
355 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); | 355 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); |
356 | success = false; | 356 | success = false; |
357 | } | 357 | } |
358 | srcFile.close(); | 358 | srcFile.close(); |
359 | destFile.close(); | 359 | destFile.close(); |
360 | // Set file permissions | 360 | // Set file permissions |
361 | if( stat( (const char *) src, &status ) == 0 ) { | 361 | if( stat( (const char *) src, &status ) == 0 ) { |
362 | chmod( (const char *) dest, status.st_mode ); | 362 | chmod( (const char *) dest, status.st_mode ); |
363 | } | 363 | } |
364 | 364 | ||
365 | return success; | 365 | return success; |
366 | } | 366 | } |
367 | 367 | ||
368 | 368 | ||
369 | bool FileManager::renameFile( const QString & src, const QString & dest ) { | 369 | bool FileManager::renameFile( const QString & src, const QString & dest ) { |
370 | if(copyFile( src, dest )) { | 370 | if(copyFile( src, dest )) { |
371 | if(QFile::remove(src) ) { | 371 | if(QFile::remove(src) ) { |
372 | return true; | 372 | return true; |
373 | } | 373 | } |
374 | } | 374 | } |
375 | return false; | 375 | return false; |
376 | } | 376 | } |
377 | */ | 377 | */ |
378 | 378 | ||
379 | /*! | 379 | /*! |
380 | Opens the document specified by \a f as a readable QIODevice. | 380 | Opens the document specified by \a f as a readable QIODevice. |
381 | The caller must delete the return value. | 381 | The caller must delete the return value. |
382 | 382 | ||
383 | Returns 0 if the operation fails. | 383 | Returns 0 if the operation fails. |
384 | */ | 384 | */ |
385 | QIODevice* FileManager::openFile( const DocLnk& f ) | 385 | QIODevice* FileManager::openFile( const DocLnk& f ) |
386 | { | 386 | { |
387 | QString fn = f.file(); | 387 | QString fn = f.file(); |
388 | QFile* fl = new QFile( fn ); | 388 | QFile* fl = new QFile( fn ); |
389 | if ( !fl->open( IO_ReadOnly ) ) { | 389 | if ( !fl->open( IO_ReadOnly ) ) { |
390 | delete fl; | 390 | delete fl; |
391 | fl = 0; | 391 | fl = 0; |
392 | } | 392 | } |
393 | return fl; | 393 | return fl; |
394 | } | 394 | } |
395 | 395 | ||
396 | /*! | 396 | /*! |
397 | Opens the document specified by \a f as a writable QIODevice. | 397 | Opens the document specified by \a f as a writable QIODevice. |
398 | The caller must delete the return value. | 398 | The caller must delete the return value. |
399 | 399 | ||
400 | Returns 0 if the operation fails. | 400 | Returns 0 if the operation fails. |
401 | */ | 401 | */ |
402 | QIODevice* FileManager::saveFile( const DocLnk& f ) | 402 | QIODevice* FileManager::saveFile( const DocLnk& f ) |
403 | { | 403 | { |
404 | QString fn = f.file(); | 404 | QString fn = f.file(); |
405 | ensurePathExists( fn ); | 405 | ensurePathExists( fn ); |
406 | QFile* fl = new QFile( fn ); | 406 | QFile* fl = new QFile( fn ); |
407 | if ( fl->open( IO_WriteOnly ) ) { | 407 | if ( fl->open( IO_WriteOnly ) ) { |
408 | f.writeLink(); | 408 | f.writeLink(); |
409 | } else { | 409 | } else { |
410 | delete fl; | 410 | delete fl; |
411 | fl = 0; | 411 | fl = 0; |
412 | } | 412 | } |
413 | return fl; | 413 | return fl; |
414 | } | 414 | } |
415 | 415 | ||
416 | /*! | 416 | /*! |
417 | Returns whether the document specified by \a f current exists | 417 | Returns whether the document specified by \a f current exists |
418 | as a file on disk. | 418 | as a file on disk. |
419 | */ | 419 | */ |
420 | bool FileManager::exists( const DocLnk &f ) | 420 | bool FileManager::exists( const DocLnk &f ) |
421 | { | 421 | { |
422 | return QFile::exists(f.file()); | 422 | return QFile::exists(f.file()); |
423 | } | 423 | } |
424 | 424 | ||
425 | 425 | ||
426 | /*! | 426 | /*! |
427 | Ensures that the path \a fn exists, by creating required directories. | 427 | Ensures that the path \a fn exists, by creating required directories. |
428 | Returns TRUE if successful. | 428 | Returns TRUE if successful. |
429 | */ | 429 | */ |
430 | bool FileManager::ensurePathExists( const QString &fn ) | 430 | bool FileManager::ensurePathExists( const QString &fn ) |
431 | { | 431 | { |
432 | QFileInfo fi(fn); | 432 | QFileInfo fi(fn); |
433 | fi.setFile( fi.dirPath(TRUE) ); | 433 | fi.setFile( fi.dirPath(TRUE) ); |
434 | if ( !fi.exists() ) { | 434 | if ( !fi.exists() ) { |
435 | if ( system(("mkdir -p "+fi.filePath())) ) | 435 | if ( system(("mkdir -p "+fi.filePath())) ) |
436 | return FALSE; | 436 | return FALSE; |
437 | } | 437 | } |
438 | 438 | ||
439 | return TRUE; | 439 | return TRUE; |
440 | } | 440 | } |