File indexing completed on 2024-11-24 04:31:15

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "preallocationthread.h"
0007 #include <errno.h>
0008 #include <fcntl.h>
0009 #include <sys/stat.h>
0010 #include <sys/types.h>
0011 #include <unistd.h>
0012 
0013 #include "chunkmanager.h"
0014 #include <klocalizedstring.h>
0015 #include <qfile.h>
0016 #include <util/error.h>
0017 #include <util/log.h>
0018 
0019 #ifndef O_LARGEFILE
0020 #define O_LARGEFILE 0
0021 #endif
0022 
0023 namespace bt
0024 {
0025 PreallocationThread::PreallocationThread()
0026     : stopped(false)
0027     , not_finished(false)
0028     , done(false)
0029     , bytes_written(0)
0030 {
0031 }
0032 
0033 PreallocationThread::~PreallocationThread()
0034 {
0035 }
0036 
0037 void PreallocationThread::add(CacheFile::Ptr cache_file)
0038 {
0039     if (cache_file)
0040         todo.append(cache_file);
0041 }
0042 
0043 void PreallocationThread::run()
0044 {
0045     try {
0046         for (CacheFile::Ptr cache_file : std::as_const(todo)) {
0047             if (!isStopped()) {
0048                 cache_file->preallocate(this);
0049             } else {
0050                 setNotFinished();
0051                 break;
0052             }
0053         }
0054 
0055     } catch (Error &err) {
0056         setErrorMsg(err.toString());
0057     }
0058 
0059     QMutexLocker lock(&mutex);
0060     done = true;
0061     Out(SYS_DIO | LOG_NOTICE) << "PreallocationThread has finished" << endl;
0062 }
0063 
0064 void PreallocationThread::stop()
0065 {
0066     QMutexLocker lock(&mutex);
0067     stopped = true;
0068 }
0069 
0070 void PreallocationThread::setErrorMsg(const QString &msg)
0071 {
0072     QMutexLocker lock(&mutex);
0073     error_msg = msg;
0074     stopped = true;
0075 }
0076 
0077 bool PreallocationThread::isStopped() const
0078 {
0079     QMutexLocker lock(&mutex);
0080     return stopped;
0081 }
0082 
0083 bool PreallocationThread::errorHappened() const
0084 {
0085     QMutexLocker lock(&mutex);
0086     return !error_msg.isNull();
0087 }
0088 
0089 void PreallocationThread::written(Uint64 nb)
0090 {
0091     QMutexLocker lock(&mutex);
0092     bytes_written += nb;
0093 }
0094 
0095 Uint64 PreallocationThread::bytesWritten()
0096 {
0097     QMutexLocker lock(&mutex);
0098     return bytes_written;
0099 }
0100 
0101 bool PreallocationThread::isDone() const
0102 {
0103     QMutexLocker lock(&mutex);
0104     return done;
0105 }
0106 
0107 bool PreallocationThread::isNotFinished() const
0108 {
0109     QMutexLocker lock(&mutex);
0110     return not_finished;
0111 }
0112 
0113 void PreallocationThread::setNotFinished()
0114 {
0115     QMutexLocker lock(&mutex);
0116     not_finished = true;
0117 }
0118 }