1
/***************************************************************************
2
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18
***************************************************************************/
22
#include "util/externalcommand.h"
23
#include "util/capacity.h"
30
FileSystem::SupportType ext2::m_GetUsed = FileSystem::SupportNone;
31
FileSystem::SupportType ext2::m_GetLabel = FileSystem::SupportNone;
32
FileSystem::SupportType ext2::m_Create = FileSystem::SupportNone;
33
FileSystem::SupportType ext2::m_Grow = FileSystem::SupportNone;
34
FileSystem::SupportType ext2::m_Shrink = FileSystem::SupportNone;
35
FileSystem::SupportType ext2::m_Move = FileSystem::SupportNone;
36
FileSystem::SupportType ext2::m_Check = FileSystem::SupportNone;
37
FileSystem::SupportType ext2::m_Copy = FileSystem::SupportNone;
38
FileSystem::SupportType ext2::m_Backup = FileSystem::SupportNone;
39
FileSystem::SupportType ext2::m_SetLabel = FileSystem::SupportNone;
40
FileSystem::SupportType ext2::m_UpdateUUID = FileSystem::SupportNone;
42
ext2::ext2(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, FileSystem::Type t) :
43
FileSystem(firstsector, lastsector, sectorsused, label, t)
49
m_GetUsed = findExternal("dumpe2fs") ? SupportExternal : SupportNone;
50
m_SetLabel = m_GetLabel = findExternal("e2label") ? SupportExternal : SupportNone;
51
m_Create = findExternal("mkfs.ext2") ? SupportExternal : SupportNone;
52
m_Check = findExternal("e2fsck", QStringList() << "-V") ? SupportExternal : SupportNone;
53
m_UpdateUUID = findExternal("tune2fs") ? SupportExternal : SupportNone;
54
m_Grow = (m_Check != SupportNone && findExternal("resize2fs")) ? SupportExternal : SupportNone;
55
m_Shrink = m_GetUsed != SupportNone ? SupportExternal : SupportNone;
56
m_Copy = (m_Check != SupportNone) ? SupportInternal : SupportNone;
57
m_Move = (m_Check != SupportNone) ? SupportInternal : SupportNone;
58
m_Backup = SupportInternal;
61
qint64 ext2::maxCapacity() const
63
return Capacity::unitFactor(Capacity::Byte, Capacity::EiB);
66
qint64 ext2::readUsedCapacity(const QString& deviceNode) const
68
ExternalCommand cmd("dumpe2fs", QStringList() << "-h" << deviceNode);
72
qint64 blockCount = -1;
73
QRegExp rxBlockCount("Block count:\\s*(\\d+)");
75
if (rxBlockCount.indexIn(cmd.output()) != -1)
76
blockCount = rxBlockCount.cap(1).toLongLong();
78
qint64 freeBlocks = -1;
79
QRegExp rxFreeBlocks("Free blocks:\\s*(\\d+)");
81
if (rxFreeBlocks.indexIn(cmd.output()) != -1)
82
freeBlocks = rxFreeBlocks.cap(1).toLongLong();
84
qint64 blockSize = -1;
85
QRegExp rxBlockSize("Block size:\\s*(\\d+)");
87
if (rxBlockSize.indexIn(cmd.output()) != -1)
88
blockSize = rxBlockSize.cap(1).toLongLong();
90
if (blockCount > -1 && freeBlocks > -1 && blockSize > -1)
91
return (blockCount - freeBlocks) * blockSize;
97
QString ext2::readLabel(const QString& deviceNode) const
99
ExternalCommand cmd("e2label", QStringList() << deviceNode);
100
return cmd.run() ? cmd.output().simplified() : QString();
103
bool ext2::check(Report& report, const QString& deviceNode) const
105
ExternalCommand cmd(report, "e2fsck", QStringList() << "-f" << "-y" << "-v" << deviceNode);
106
return cmd.run(-1) && (cmd.exitCode() == 0 || cmd.exitCode() == 1 || cmd.exitCode() == 2 || cmd.exitCode() == 256);
109
bool ext2::create(Report& report, const QString& deviceNode) const
111
return ExternalCommand(report, "mkfs.ext2", QStringList() << deviceNode).run(-1);
114
bool ext2::resize(Report& report, const QString& deviceNode, qint64 length) const
116
const QString len = QString::number(length / 512) + 's';
117
ExternalCommand cmd(report, "resize2fs", QStringList() << deviceNode << len);
118
return cmd.run(-1) && cmd.exitCode() == 0;
121
bool ext2::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
123
return ExternalCommand(report, "e2label", QStringList() << deviceNode << newLabel).run(-1);
126
bool ext2::updateUUID(Report& report, const QString& deviceNode) const
128
return ExternalCommand(report, "tune2fs", QStringList() << "-U" << "random" << deviceNode).run(-1);