Debian Codegraph @

src/fs/fs.cpp

raw symbols SCIP
1 // Copyright (C) 2014 Sacha Refshauge
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 3.0.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 3.0 for more details.
11
12 // A copy of the GPL 3.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official GIT repository and contact information can be found at
16 // http://github.com/xsacha/Sachesi
17
18 #include "fs.h"
19
20 #ifdef _WIN32
21 void fixFileTime(QString filename, int time) {
22 FILETIME pft;
23 LONGLONG ll = Int32x32To64(time, 10000000) + 116444736000000000;
24 pft.dwLowDateTime = (DWORD)ll;
25 pft.dwHighDateTime = ll >> 32;
26 HANDLE fd_handle = CreateFile(filename.toStdWString().c_str(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
27 SetFileTime(fd_handle, &pft,(LPFILETIME) nullptr, &pft);
28 CloseHandle(fd_handle);
29 }
30 #endif
31
32 QFileSystem::QFileSystem(QString filename, QIODevice* file, qint64 offset, qint64 size, QString path, QString imageExt)
33 : QObject(nullptr)
34 , _file(file)
35 , _offset(offset)
36 , _size(size)
37 , _path(path)
38 , _filename(filename)
39 , _imageExt(imageExt)
40 {
41 if (_file == nullptr) {
42 _file = new QFile(filename);
43 _file->open(QIODevice::ReadOnly);
44 size = _file->size();
45 }
46 }
47
48 // Free anything we made here
49 QFileSystem::~QFileSystem() {
50 // TODO: Better tracking of whether we created this?
51 // Assume offset of 0 means we decided to let QFileSystem make it
52 if (_offset == 0) {
53 if (_file->isOpen())
54 _file->close();
55 delete _file;
56 }
57 }
58
59 // A method to append numbers to a filename/folder until it is unique
60 QString QFileSystem::uniqueDir(QString name) {
61 int counter = 0;
62 while (QDir(name + (counter++ > 0 ? QString::number(counter + 1) : "")).exists());
63
64 return name + (counter > 1 ? QString::number(counter) : "");
65 }
66 QString QFileSystem::uniqueFile(QString name) {
67 int counter = 0;
68 while (QFile::exists(name + (counter++ > 0 ? QString::number(counter + 1) : "")));
69
70 return name + (counter > 1 ? QString::number(counter) : "");
71 }
72
73 // A generic method of working out new name based on old name
74 QString QFileSystem::generateName(QString imageExt) {
75 QString name = QFileInfo(_filename).completeBaseName();
76 if (imageExt.isEmpty())
77 return uniqueDir(name);
78
79 return uniqueFile(name + imageExt);
80 }
81
82 // Entry for requesting an extration of the filesystem image
83 bool QFileSystem::extractImage() {
84 curSize = 0;
85 maxSize = _size;
86 return this->createImage(this->generateName(_imageExt));
87 }
88
89 // A generic method for extracting an entire image of maxSize
90 bool QFileSystem::createImage(QString name) {
91 return writeFile(name, _offset, maxSize);
92 }
93
94 // Entry for requesting an extration of the filesystem contents
95 bool QFileSystem::extractContents() {
96 curSize = 0;
97 maxSize = _size;
98 _path += "/" + this->generateName();
99 return this->createContents();
100 }
101
102 // A method to write writeSize bytes from a QIODevice to a new file, named filename
103 bool QFileSystem::writeFile(QString fileName, qint64 offset, qint64 writeSize, bool absolute) {
104 _file->seek(offset);
105 QFile newFile;
106 if (absolute)
107 newFile.setFileName(fileName);
108 else
109 newFile.setFileName(_path + "/" + fileName);
110 if (!newFile.open(QIODevice::WriteOnly))
111 return false;
112 qint64 endSize = curSize + writeSize;
113 while (endSize > curSize) {
114 int diff = newFile.write(_file->read(qMin(BUFFER_LEN, endSize - curSize)));
115 if (diff <= 0)
116 return false;
117 increaseCurSize(diff);
118 }
119 newFile.close();
120
121 return true;
122 }