Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
System
KPMCore
Commits
fd7f9b87
Commit
fd7f9b87
authored
Feb 06, 2022
by
Andrius Štikonas
Browse files
Do not repeatedly open and close file when reading from it.
parent
eddbd7a3
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/util/externalcommandhelper.cpp
View file @
fd7f9b87
...
...
@@ -71,25 +71,25 @@ ExternalCommandHelper::ExternalCommandHelper()
@param size the number of bytes to read
@return true on success
*/
bool
ExternalCommandHelper
::
readData
(
const
QString
&
sourceD
evice
,
QByteArray
&
buffer
,
const
qint64
offset
,
const
qint64
size
)
bool
ExternalCommandHelper
::
readData
(
QFile
&
d
evice
,
QByteArray
&
buffer
,
const
qint64
offset
,
const
qint64
size
)
{
QFile
device
(
sourceDevice
);
if
(
!
device
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Unbuffered
))
{
qCritical
()
<<
xi18n
(
"Could not open device <filename>%1</filename> for reading."
,
sourceDevice
)
;
return
false
;
if
(
!
device
.
isOpen
())
{
if
(
!
device
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Unbuffered
))
{
qCritical
()
<<
xi18n
(
"Could not open device <filename>%1</filename> for reading."
,
device
.
fileName
());
return
false
;
}
}
// Sequential devices such as /dev/zero or /dev/urandom return false on seek().
if
(
!
device
.
isSequential
()
&&
!
device
.
seek
(
offset
))
{
qCritical
()
<<
xi18n
(
"Could not seek position %1 on device <filename>%2</filename>."
,
offset
,
sourceDevice
);
qCritical
()
<<
xi18n
(
"Could not seek position %1 on device <filename>%2</filename>."
,
offset
,
device
.
fileName
()
);
return
false
;
}
buffer
=
device
.
read
(
size
);
if
(
size
!=
buffer
.
size
())
{
qCritical
()
<<
xi18n
(
"Could not read from device <filename>%1</filename>."
,
sourceDevice
);
qCritical
()
<<
xi18n
(
"Could not read from device <filename>%1</filename>."
,
device
.
fileName
()
);
return
false
;
}
...
...
@@ -233,12 +233,13 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con
bool
rval
=
true
;
QFile
device
(
targetDevice
);
QFile
target
(
targetDevice
);
QFile
source
(
sourceDevice
);
while
(
blocksCopied
<
blocksToCopy
)
{
if
(
!
(
rval
=
readData
(
source
Device
,
buffer
,
readOffset
+
blockSize
*
blocksCopied
*
copyDirection
,
blockSize
)))
if
(
!
(
rval
=
readData
(
source
,
buffer
,
readOffset
+
blockSize
*
blocksCopied
*
copyDirection
,
blockSize
)))
break
;
if
(
!
(
rval
=
writeData
(
device
,
buffer
,
writeOffset
+
blockSize
*
blocksCopied
*
copyDirection
)))
if
(
!
(
rval
=
writeData
(
target
,
buffer
,
writeOffset
+
blockSize
*
blocksCopied
*
copyDirection
)))
break
;
bytesWritten
+=
buffer
.
size
();
...
...
@@ -264,10 +265,10 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con
const
qint64
lastBlockWriteOffset
=
copyDirection
==
CopyDirection
::
Left
?
writeOffset
+
blockSize
*
blocksCopied
:
targetOffset
;
reportText
=
xi18nc
(
"@info:progress"
,
"Copying remainder of block size %1 from %2 to %3."
,
lastBlock
,
lastBlockReadOffset
,
lastBlockWriteOffset
);
Q_EMIT
report
(
reportText
);
rval
=
readData
(
source
Device
,
buffer
,
lastBlockReadOffset
,
lastBlock
);
rval
=
readData
(
source
,
buffer
,
lastBlockReadOffset
,
lastBlock
);
if
(
rval
)
{
rval
=
writeData
(
device
,
buffer
,
lastBlockWriteOffset
);
rval
=
writeData
(
target
,
buffer
,
lastBlockWriteOffset
);
}
if
(
rval
)
{
...
...
@@ -298,7 +299,8 @@ QByteArray ExternalCommandHelper::ReadData(const QString& device, const qint64 o
}
QByteArray
buffer
;
bool
rval
=
readData
(
device
,
buffer
,
offset
,
length
);
QFile
sourceDevice
(
device
);
bool
rval
=
readData
(
sourceDevice
,
buffer
,
offset
,
length
);
if
(
rval
)
{
return
buffer
;
}
...
...
src/util/externalcommandhelper.h
View file @
fd7f9b87
...
...
@@ -34,7 +34,7 @@ Q_SIGNALS:
public:
ExternalCommandHelper
();
bool
readData
(
const
QString
&
sourceD
evice
,
QByteArray
&
buffer
,
const
qint64
offset
,
const
qint64
size
);
bool
readData
(
QFile
&
d
evice
,
QByteArray
&
buffer
,
const
qint64
offset
,
const
qint64
size
);
bool
writeData
(
QFile
&
device
,
const
QByteArray
&
buffer
,
const
qint64
offset
);
public
Q_SLOTS
:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment