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