-rw-r--r-- | library/filemanager.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/library/filemanager.cpp b/library/filemanager.cpp index cc657fa..91986a0 100644 --- a/library/filemanager.cpp +++ b/library/filemanager.cpp | |||
@@ -10,49 +10,58 @@ | |||
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 <qdir.h> | 23 | #include <qdir.h> |
24 | #include <qfile.h> | 24 | #include <qfile.h> |
25 | #include <qfileinfo.h> | 25 | #include <qfileinfo.h> |
26 | #include <qtextstream.h> | 26 | #include <qtextstream.h> |
27 | #include <qtextcodec.h> | 27 | #include <qtextcodec.h> |
28 | 28 | ||
29 | #include <errno.h> | 29 | #include <errno.h> |
30 | #include <stdlib.h> | 30 | #include <stdlib.h> |
31 | #include <unistd.h> | 31 | #include <unistd.h> |
32 | #include <sys/stat.h> | 32 | #include <sys/stat.h> |
33 | #include <dirent.h> | 33 | #include <dirent.h> |
34 | #ifdef Q_OS_MACX | ||
35 | // MacOS X does not have sendfile.. :( | ||
36 | // But maybe in the future.. !? | ||
37 | # ifdef SENDFILE | ||
38 | # include <sys/types.h> | ||
39 | # include <sys/socket.h> | ||
40 | # endif | ||
41 | #else | ||
34 | #include <sys/sendfile.h> | 42 | #include <sys/sendfile.h> |
43 | #endif /* Q_OS_MACX */ | ||
35 | #include <fcntl.h> | 44 | #include <fcntl.h> |
36 | 45 | ||
37 | /*! | 46 | /*! |
38 | \class FileManager | 47 | \class FileManager |
39 | \brief The FileManager class assists with AppLnk input/output. | 48 | \brief The FileManager class assists with AppLnk input/output. |
40 | */ | 49 | */ |
41 | 50 | ||
42 | /*! | 51 | /*! |
43 | Constructs a FileManager. | 52 | Constructs a FileManager. |
44 | */ | 53 | */ |
45 | FileManager::FileManager() | 54 | FileManager::FileManager() |
46 | { | 55 | { |
47 | } | 56 | } |
48 | 57 | ||
49 | /*! | 58 | /*! |
50 | Destroys a FileManager. | 59 | Destroys a FileManager. |
51 | */ | 60 | */ |
52 | FileManager::~FileManager() | 61 | FileManager::~FileManager() |
53 | { | 62 | { |
54 | 63 | ||
55 | } | 64 | } |
56 | 65 | ||
57 | /*! | 66 | /*! |
58 | Saves \a data as the document specified by \a f. | 67 | Saves \a data as the document specified by \a f. |
@@ -195,80 +204,114 @@ bool FileManager::copyFile( const AppLnk &src, const AppLnk &dest ) | |||
195 | ok = FALSE; | 204 | ok = FALSE; |
196 | else | 205 | else |
197 | bytesRead -= bytesWritten; | 206 | bytesRead -= bytesWritten; |
198 | } | 207 | } |
199 | } | 208 | } |
200 | 209 | ||
201 | if ( ok ) | 210 | if ( ok ) |
202 | ok = dest.writeLink(); | 211 | ok = dest.writeLink(); |
203 | 212 | ||
204 | if ( ok ) { | 213 | if ( ok ) { |
205 | // okay now rename the file... | 214 | // okay now rename the file... |
206 | if ( !renameFile( fn.latin1(), dest.file().latin1() ) ) { | 215 | if ( !renameFile( fn.latin1(), dest.file().latin1() ) ) { |
207 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), | 216 | qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(), |
208 | dest.file().latin1(), errno ); | 217 | dest.file().latin1(), errno ); |
209 | // remove the tmp file, otherwise, it will just lay around... | 218 | // remove the tmp file, otherwise, it will just lay around... |
210 | QFile::remove( fn.latin1() ); | 219 | QFile::remove( fn.latin1() ); |
211 | } | 220 | } |
212 | } else { | 221 | } else { |
213 | QFile::remove( fn.latin1() ); | 222 | QFile::remove( fn.latin1() ); |
214 | } | 223 | } |
215 | 224 | ||
216 | return ok; | 225 | return ok; |
217 | } | 226 | } |
218 | 227 | ||
228 | |||
229 | |||
219 | bool FileManager::copyFile( const QString & src, const QString & dest ) { | 230 | bool FileManager::copyFile( const QString & src, const QString & dest ) { |
220 | bool success = true; | 231 | bool success = true; |
221 | struct stat status; | 232 | struct stat status; |
222 | int read_fd=0; | 233 | int read_fd=0; |
223 | int write_fd=0; | 234 | int write_fd=0; |
224 | struct stat stat_buf; | 235 | struct stat stat_buf; |
225 | off_t offset = 0; | 236 | off_t offset = 0; |
226 | QFile srcFile(src); | 237 | QFile srcFile(src); |
227 | QFile destFile(dest); | 238 | QFile destFile(dest); |
228 | 239 | ||
229 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { | 240 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { |
230 | return success = false; | 241 | return success = false; |
231 | } | 242 | } |
232 | read_fd = srcFile.handle(); | 243 | read_fd = srcFile.handle(); |
233 | if(read_fd != -1) { | 244 | if(read_fd != -1) { |
234 | fstat (read_fd, &stat_buf); | 245 | fstat (read_fd, &stat_buf); |
235 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) | 246 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) |
236 | return success = false; | 247 | return success = false; |
237 | write_fd = destFile.handle(); | 248 | write_fd = destFile.handle(); |
238 | if(write_fd != -1) { | 249 | if(write_fd != -1) { |
239 | int err=0; | 250 | int err=0; |
240 | QString msg; | 251 | QString msg; |
252 | #ifdef Q_OS_MACX | ||
253 | #ifdef SENDMAIL | ||
254 | /* FreeBSD does support a different kind of | ||
255 | * sendfile. (eilers) | ||
256 | * I took this from Very Secure FTPd | ||
257 | * Licence: GPL | ||
258 | * Author: Chris Evans | ||
259 | * sysdeputil.c | ||
260 | */ | ||
261 | /* XXX - start_pos will truncate on 32-bit machines - can we | ||
262 | * say "start from current pos"? | ||
263 | */ | ||
264 | off_t written = 0; | ||
265 | int retval = 0; | ||
266 | retval = sendfile(read_fd, write_fd, offset, stat_buf.st_size, NULL, | ||
267 | &written, 0); | ||
268 | /* Translate to Linux-like retval */ | ||
269 | if (written > 0) | ||
270 | { | ||
271 | err = (int) written; | ||
272 | } | ||
273 | #else /* SENDMAIL */ | ||
274 | err == -1; | ||
275 | msg = "FAILURE: Using unsupported function \"sendfile()\" Need Workaround !!"; | ||
276 | success = false; | ||
277 | # warning "Need workaround for sendfile!!(eilers)" | ||
278 | #endif /* SENDMAIL */ | ||
279 | |||
280 | #else | ||
241 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); | 281 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); |
242 | if( err == -1) { | 282 | if( err == -1) { |
243 | switch(err) { | 283 | switch(err) { |
244 | case EBADF : msg = "The input file was not opened for reading or the output file was not opened for writing. "; | 284 | case EBADF : msg = "The input file was not opened for reading or the output file was not opened for writing. "; |
245 | case EINVAL: msg = "Descriptor is not valid or locked. "; | 285 | case EINVAL: msg = "Descriptor is not valid or locked. "; |
246 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; | 286 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; |
247 | case EIO: msg = "Unspecified error while reading from in_fd."; | 287 | case EIO: msg = "Unspecified error while reading from in_fd."; |
248 | }; | 288 | }; |
249 | success = false; | 289 | success = false; |
250 | } | 290 | } |
291 | #endif /* Q_OS_MACX */ | ||
292 | if( !success ) | ||
293 | qWarning( msg ); | ||
251 | } else { | 294 | } else { |
252 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); | 295 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); |
253 | success = false; | 296 | success = false; |
254 | } | 297 | } |
255 | } else { | 298 | } else { |
256 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); | 299 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); |
257 | success = false; | 300 | success = false; |
258 | } | 301 | } |
259 | srcFile.close(); | 302 | srcFile.close(); |
260 | destFile.close(); | 303 | destFile.close(); |
261 | // Set file permissions | 304 | // Set file permissions |
262 | if( stat( (const char *) src, &status ) == 0 ) { | 305 | if( stat( (const char *) src, &status ) == 0 ) { |
263 | chmod( (const char *) dest, status.st_mode ); | 306 | chmod( (const char *) dest, status.st_mode ); |
264 | } | 307 | } |
265 | 308 | ||
266 | return success; | 309 | return success; |
267 | } | 310 | } |
268 | 311 | ||
269 | 312 | ||
270 | bool FileManager::renameFile( const QString & src, const QString & dest ) { | 313 | bool FileManager::renameFile( const QString & src, const QString & dest ) { |
271 | if(copyFile( src, dest )) { | 314 | if(copyFile( src, dest )) { |
272 | if(QFile::remove(src) ) { | 315 | if(QFile::remove(src) ) { |
273 | return true; | 316 | return true; |
274 | } | 317 | } |