-rw-r--r-- | library/filemanager.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/library/filemanager.cpp b/library/filemanager.cpp index eff8429..408be20 100644 --- a/library/filemanager.cpp +++ b/library/filemanager.cpp | |||
@@ -123,282 +123,282 @@ bool FileManager::saveFile( const DocLnk &f, const QString &text ) | |||
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 SENDFILE |
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 /* SENDFILE */ |
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 /* SENDFILE */ |
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 | bool FileManager::copyFile( const QString & src, const QString & dest ) { | 321 | bool FileManager::copyFile( const QString & src, const QString & dest ) { |
322 | bool success = true; | 322 | bool success = true; |
323 | struct stat status; | 323 | struct stat status; |
324 | int read_fd=0; | 324 | int read_fd=0; |
325 | int write_fd=0; | 325 | int write_fd=0; |
326 | struct stat stat_buf; | 326 | struct stat stat_buf; |
327 | off_t offset = 0; | 327 | off_t offset = 0; |
328 | QFile srcFile(src); | 328 | QFile srcFile(src); |
329 | QFile destFile(dest); | 329 | QFile destFile(dest); |
330 | 330 | ||
331 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { | 331 | if(!srcFile.open( IO_ReadOnly|IO_Raw)) { |
332 | return success = false; | 332 | return success = false; |
333 | } | 333 | } |
334 | read_fd = srcFile.handle(); | 334 | read_fd = srcFile.handle(); |
335 | if(read_fd != -1) { | 335 | if(read_fd != -1) { |
336 | fstat (read_fd, &stat_buf); | 336 | fstat (read_fd, &stat_buf); |
337 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) | 337 | if( !destFile.open( IO_WriteOnly|IO_Raw ) ) |
338 | return success = false; | 338 | return success = false; |
339 | write_fd = destFile.handle(); | 339 | write_fd = destFile.handle(); |
340 | if(write_fd != -1) { | 340 | if(write_fd != -1) { |
341 | int err=0; | 341 | int err=0; |
342 | QString msg; | 342 | QString msg; |
343 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); | 343 | err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size); |
344 | if( err == -1) { | 344 | if( err == -1) { |
345 | switch(err) { | 345 | switch(err) { |
346 | 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. "; |
347 | case EINVAL: msg = "Descriptor is not valid or locked. "; | 347 | case EINVAL: msg = "Descriptor is not valid or locked. "; |
348 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; | 348 | case ENOMEM: msg = "Insufficient memory to read from in_fd."; |
349 | case EIO: msg = "Unspecified error while reading from in_fd."; | 349 | case EIO: msg = "Unspecified error while reading from in_fd."; |
350 | }; | 350 | }; |
351 | success = false; | 351 | success = false; |
352 | } | 352 | } |
353 | } else { | 353 | } else { |
354 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); | 354 | qWarning("open write failed %s, %s",src.latin1(), dest.latin1()); |
355 | success = false; | 355 | success = false; |
356 | } | 356 | } |
357 | } else { | 357 | } else { |
358 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); | 358 | qWarning("open read failed %s, %s",src.latin1(), dest.latin1()); |
359 | success = false; | 359 | success = false; |
360 | } | 360 | } |
361 | srcFile.close(); | 361 | srcFile.close(); |
362 | destFile.close(); | 362 | destFile.close(); |
363 | // Set file permissions | 363 | // Set file permissions |
364 | if( stat( (const char *) src, &status ) == 0 ) { | 364 | if( stat( (const char *) src, &status ) == 0 ) { |
365 | chmod( (const char *) dest, status.st_mode ); | 365 | chmod( (const char *) dest, status.st_mode ); |
366 | } | 366 | } |
367 | 367 | ||
368 | return success; | 368 | return success; |
369 | } | 369 | } |
370 | 370 | ||
371 | 371 | ||
372 | bool FileManager::renameFile( const QString & src, const QString & dest ) { | 372 | bool FileManager::renameFile( const QString & src, const QString & dest ) { |
373 | if(copyFile( src, dest )) { | 373 | if(copyFile( src, dest )) { |
374 | if(QFile::remove(src) ) { | 374 | if(QFile::remove(src) ) { |
375 | return true; | 375 | return true; |
376 | } | 376 | } |
377 | } | 377 | } |
378 | return false; | 378 | return false; |
379 | } | 379 | } |
380 | */ | 380 | */ |
381 | 381 | ||
382 | /*! | 382 | /*! |
383 | Opens the document specified by \a f as a readable QIODevice. | 383 | Opens the document specified by \a f as a readable QIODevice. |
384 | The caller must delete the return value. | 384 | The caller must delete the return value. |
385 | 385 | ||
386 | Returns 0 if the operation fails. | 386 | Returns 0 if the operation fails. |
387 | */ | 387 | */ |
388 | QIODevice* FileManager::openFile( const DocLnk& f ) | 388 | QIODevice* FileManager::openFile( const DocLnk& f ) |
389 | { | 389 | { |
390 | QString fn = f.file(); | 390 | QString fn = f.file(); |
391 | QFile* fl = new QFile( fn ); | 391 | QFile* fl = new QFile( fn ); |
392 | if ( !fl->open( IO_ReadOnly ) ) { | 392 | if ( !fl->open( IO_ReadOnly ) ) { |
393 | delete fl; | 393 | delete fl; |
394 | fl = 0; | 394 | fl = 0; |
395 | } | 395 | } |
396 | return fl; | 396 | return fl; |
397 | } | 397 | } |
398 | 398 | ||
399 | /*! | 399 | /*! |
400 | Opens the document specified by \a f as a writable QIODevice. | 400 | Opens the document specified by \a f as a writable QIODevice. |
401 | The caller must delete the return value. | 401 | The caller must delete the return value. |
402 | 402 | ||
403 | Returns 0 if the operation fails. | 403 | Returns 0 if the operation fails. |
404 | */ | 404 | */ |