1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include "util_file.h"
#include <QDataStream>
#include <QFile>
#include <QString>
#include <QTemporaryFile>
#include "scstreamfilter.h"
bool copyData(QIODevice& src, QIODevice& dest)
{
bool success = false;
if ((src.openMode() & QIODevice::ReadOnly) == 0)
return false;
if ((dest.openMode() & QIODevice::WriteOnly) == 0)
return false;
QByteArray bb( 65536, ' ' );
if (bb.size() > 0) // Check for memory allocation failure
{
qint64 byteswritten;
qint64 bytesread = src.read( bb.data(), bb.size() );
success = (bytesread > 0);
while (bytesread > 0)
{
byteswritten = dest.write( bb.data(), bytesread );
success &= (bytesread == byteswritten);
bytesread = src.read( bb.data(), bb.size() );
}
}
return success;
}
bool copyFile(const QString& source, const QString& target)
{
bool success = true;
if ((source.isEmpty()) || (target.isEmpty()))
return false;
if (source == target)
return false;
QFile s(source);
if (!s.exists())
return false;
QFile t(target);
if (s.open(QIODevice::ReadOnly))
{
if (t.open(QIODevice::WriteOnly))
{
success = copyData(s, t);
success &= (s.error() == QFile::NoError && t.error() == QFile::NoError);
t.close();
}
s.close();
}
return success;
}
bool copyFileAtomic(const QString& source, const QString& target)
{
bool success = false;
if ((source.isEmpty()) || (target.isEmpty()))
return false;
if (source == target)
return false;
QFile srcFile(source);
QString tempFileName;
QTemporaryFile* tempFile = new QTemporaryFile(target + "_XXXXXX");
if (!tempFile)
return false;
if (srcFile.open(QIODevice::ReadOnly))
{
if (tempFile->open())
{
tempFileName = tempFile->fileName();
success = copyData(srcFile, *tempFile);
success &= (srcFile.error() == QFile::NoError && tempFile->error() == QFile::NoError);
tempFile->close();
}
srcFile.close();
}
if (success)
{
if (QFile::exists(target))
success = QFile::remove(target);
if (success)
{
// We delete temporary file now to force file close
// QTemporaryFile::close() do not really close file
tempFile->setAutoRemove(false);
delete tempFile;
tempFile = NULL;
success = QFile::rename(tempFileName, target);
}
}
if (tempFile)
delete tempFile;
return success;
}
bool copyFileToFilter(const QString& source, ScStreamFilter& target)
{
bool copySucceed = true;
int bytesread = 0;
if (source.isEmpty())
return false;
if (!QFile::exists(source))
return false;
QFile s(source);
QByteArray bb( 65536, ' ' );
if (bb.size() <= 0) // Check for memory allocation failure
return false;
if (s.open(QIODevice::ReadOnly))
{
bytesread = s.read( bb.data(), bb.size() );
while (bytesread > 0)
{
copySucceed &= target.writeData(bb.data(), bytesread);
bytesread = s.read( bb.data(), bb.size() );
}
copySucceed &= (s.error() == QFile::NoError);
s.close();
}
return copySucceed;
}
bool copyFileToStream(const QString& source, QDataStream& target)
{
bool copySucceed = true;
int bytesread, byteswrite;
if (source.isEmpty())
return false;
if (!QFile::exists(source))
return false;
if (!target.device()->isOpen() || !target.device()->isWritable())
return false;
QFile s(source);
QByteArray bb( 65536, ' ' );
if (bb.size() <= 0) // Check for memory allocation failure
return false;
if (s.open(QIODevice::ReadOnly))
{
bytesread = s.read( bb.data(), bb.size() );
while (bytesread > 0)
{
byteswrite = target.writeRawData(bb.data(), bytesread);
copySucceed &= (byteswrite == bytesread);
bytesread = s.read( bb.data(), bb.size() );
}
copySucceed &= (s.error() == QFile::NoError);
s.close();
}
return copySucceed;
}
bool moveFile(const QString& source, const QString& target)
{
if (source.isEmpty() || target.isEmpty())
return false;
if (source == target)
return false;
bool moveSucceed = copyFile(source, target);
if (moveSucceed)
moveSucceed &= QFile::remove(source);
return moveSucceed;
}
|