-rw-r--r-- | library/filemanager.cpp | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/library/filemanager.cpp b/library/filemanager.cpp index 91986a0..1c1c998 100644 --- a/library/filemanager.cpp +++ b/library/filemanager.cpp | |||
@@ -197,45 +197,43 @@ bool FileManager::copyFile( const AppLnk &src, const AppLnk &dest ) | |||
197 | while ( ok && !sf.atEnd() ) { | 197 | while ( ok && !sf.atEnd() ) { |
198 | bytesRead = sf.readBlock( buffer, bufsize ); | 198 | bytesRead = sf.readBlock( buffer, bufsize ); |
199 | if ( bytesRead < 0 ) | 199 | if ( bytesRead < 0 ) |
200 | ok = FALSE; | 200 | ok = FALSE; |
201 | while ( ok && bytesRead > 0 ) { | 201 | while ( ok && bytesRead > 0 ) { |
202 | int bytesWritten = df.writeBlock( buffer, bytesRead ); | 202 | int bytesWritten = df.writeBlock( buffer, bytesRead ); |
203 | if ( bytesWritten < 0 ) | 203 | if ( bytesWritten < 0 ) |
204 | ok = FALSE; | 204 | ok = FALSE; |
205 | else | 205 | else |
206 | bytesRead -= bytesWritten; | 206 | bytesRead -= bytesWritten; |
207 | } | 207 | } |
208 | } | 208 | } |
209 | 209 | ||
210 | if ( ok ) | 210 | if ( ok ) |
211 | ok = dest.writeLink(); | 211 | ok = dest.writeLink(); |
212 | 212 | ||
213 | if ( ok ) { | 213 | if ( ok ) { |
214 | // okay now rename the file... | 214 | // okay now rename the file... |
215 | if ( !renameFile( fn.latin1(), dest.file().latin1() ) ) { | 215 | if ( !renameFile( fn.latin1(), dest.file().latin1() ) ) { |
216 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), | 216 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), |
217 | dest.file().latin1(), errno ); | 217 | dest.file().latin1(), errno ); |
218 | // remove the tmp file, otherwise, it will just lay around... | 218 | // remove the tmp file, otherwise, it will just lay around... |
219 | QFile::remove( fn.latin1() ); | 219 | QFile::remove( fn.latin1() ); |
220 | } | 220 | } |
221 | } else { | 221 | } else { |
222 | QFile::remove( fn.latin1() ); | 222 | QFile::remove( fn.latin1() ); |
223 | } | 223 | } |
224 | 224 | ||
225 | return ok; | 225 | return ok; |
226 | } | 226 | } |
227 | 227 | ||
228 | |||
229 | |||
230 | bool FileManager::copyFile( const QString & src, const QString & dest ) { | 228 | bool FileManager::copyFile( const QString & src, const QString & dest ) { |
231 | bool success = true; | 229 | bool success = true; |
232 | struct stat status; | 230 | struct stat status; |
233 | int read_fd=0; | 231 | int read_fd=0; |
234 | int write_fd=0; | 232 | int write_fd=0; |
235 | struct stat stat_buf; | 233 | struct stat stat_buf; |
236 | off_t offset = 0; | 234 | off_t offset = 0; |
237 | QFile srcFile(src); | 235 | QFile srcFile(src); |
238 | QFile destFile(dest); | 236 | QFile destFile(dest); |
239 | 237 | ||
240 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { | 238 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { |
241 | return success = false; | 239 | return success = false; |
@@ -311,24 +309,86 @@ bool FileManager::copyFile( const QString & src, const QString & dest ) { | |||
311 | 309 | ||
312 | 310 | ||
313 | bool FileManager::renameFile( const QString & src, const QString & dest ) { | 311 | bool FileManager::renameFile( const QString & src, const QString & dest ) { |
314 | if(copyFile( src, dest )) { | 312 | if(copyFile( src, dest )) { |
315 | if(QFile::remove(src) ) { | 313 | if(QFile::remove(src) ) { |
316 | return true; | 314 | return true; |
317 | } | 315 | } |
318 | } | 316 | } |
319 | return false; | 317 | return false; |
320 | } | 318 | } |
321 | 319 | ||
322 | 320 | ||
321 | ======= | ||
322 | bool FileManager::copyFile( const QString & src, const QString & dest ) { | ||
323 | bool success = true; | ||
324 | struct stat status; | ||
325 | int read_fd=0; | ||
326 | int write_fd=0; | ||
327 | struct stat stat_buf; | ||
328 | off_t offset = 0; | ||
329 | QFile srcFile(src); | ||
330 | QFile destFile(dest); | ||
331 | |||
332 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { | ||
333 | return success = false; | ||
334 | } | ||
335 | read_fd = srcFile.handle(); | ||
336 | if(read_fd != -1) { | ||
337 | fstat (read_fd, &stat_buf); | ||
338 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) | ||
339 | return success = false; | ||
340 | write_fd = destFile.handle(); | ||
341 | if(write_fd != -1) { | ||
342 | int err=0; | ||
343 | QString msg; | ||
344 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); | ||
345 | if( err == -1) { | ||
346 | switch(err) { | ||
347 | case EBADF : msg = "The input file was not opened for reading or the output file was not opened for writing. "; | ||
348 | case EINVAL: msg = "Descriptor is not valid or locked. "; | ||
349 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; | ||
350 | case EIO: msg = "Unspecified error while reading from in_fd."; | ||
351 | }; | ||
352 | success = false; | ||
353 | } | ||
354 | } else { | ||
355 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); | ||
356 | success = false; | ||
357 | } | ||
358 | } else { | ||
359 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); | ||
360 | success = false; | ||
361 | } | ||
362 | srcFile.close(); | ||
363 | destFile.close(); | ||
364 | // Set file permissions | ||
365 | if( stat( (const char *) src, &status ) == 0 ) { | ||
366 | chmod( (const char *) dest, status.st_mode ); | ||
367 | } | ||
368 | |||
369 | return success; | ||
370 | } | ||
371 | |||
372 | |||
373 | bool FileManager::renameFile( const QString & src, const QString & dest ) { | ||
374 | if(copyFile( src, dest )) { | ||
375 | if(QFile::remove(src) ) { | ||
376 | return true; | ||
377 | } | ||
378 | } | ||
379 | return false; | ||
380 | } | ||
381 | |||
382 | |||
323 | /*! | 383 | /*! |
324 | Opens the document specified by \a f as a readable QIODevice. | 384 | Opens the document specified by \a f as a readable QIODevice. |
325 | The caller must delete the return value. | 385 | The caller must delete the return value. |
326 | 386 | ||
327 | Returns 0 if the operation fails. | 387 | Returns 0 if the operation fails. |
328 | */ | 388 | */ |
329 | QIODevice* FileManager::openFile( const DocLnk& f ) | 389 | QIODevice* FileManager::openFile( const DocLnk& f ) |
330 | { | 390 | { |
331 | QString fn = f.file(); | 391 | QString fn = f.file(); |
332 | QFile* fl = new QFile( fn ); | 392 | QFile* fl = new QFile( fn ); |
333 | if ( !fl->open( IO_ReadOnly ) ) { | 393 | if ( !fl->open( IO_ReadOnly ) ) { |
334 | delete fl; | 394 | delete fl; |