~ubuntu-branches/ubuntu/oneiric/partitionmanager/oneiric

« back to all changes in this revision

Viewing changes to src/fs/ext2.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2009-01-23 17:57:36 UTC
  • Revision ID: james.westby@ubuntu.com-20090123175736-2ltrhgg3m55dokbm
Tags: upstream-1.0.0~beta1a
ImportĀ upstreamĀ versionĀ 1.0.0~beta1a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Volker Lanz <vl@fidra.de>                       *
 
3
 *                                                                         *
 
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.                                   *
 
8
 *                                                                         *
 
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.                          *
 
13
 *                                                                         *
 
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
 ***************************************************************************/
 
19
 
 
20
#include "fs/ext2.h"
 
21
 
 
22
#include "util/externalcommand.h"
 
23
#include "util/capacity.h"
 
24
 
 
25
#include <QString>
 
26
#include <QRegExp>
 
27
 
 
28
namespace FS
 
29
{
 
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;
 
41
 
 
42
        ext2::ext2(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, FileSystem::Type t) :
 
43
                FileSystem(firstsector, lastsector, sectorsused, label, t)
 
44
        {
 
45
        }
 
46
 
 
47
        void ext2::init()
 
48
        {
 
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;
 
59
        }
 
60
 
 
61
        qint64 ext2::maxCapacity() const
 
62
        {
 
63
                 return Capacity::unitFactor(Capacity::Byte, Capacity::EiB);
 
64
        }
 
65
 
 
66
        qint64 ext2::readUsedCapacity(const QString& deviceNode) const
 
67
        {
 
68
                ExternalCommand cmd("dumpe2fs", QStringList() << "-h" << deviceNode);
 
69
 
 
70
                if (cmd.run())
 
71
                {
 
72
                        qint64 blockCount = -1;
 
73
                        QRegExp rxBlockCount("Block count:\\s*(\\d+)");
 
74
 
 
75
                        if (rxBlockCount.indexIn(cmd.output()) != -1)
 
76
                                blockCount = rxBlockCount.cap(1).toLongLong();
 
77
 
 
78
                        qint64 freeBlocks = -1;
 
79
                        QRegExp rxFreeBlocks("Free blocks:\\s*(\\d+)");
 
80
 
 
81
                        if (rxFreeBlocks.indexIn(cmd.output()) != -1)
 
82
                                freeBlocks = rxFreeBlocks.cap(1).toLongLong();
 
83
 
 
84
                        qint64 blockSize = -1;
 
85
                        QRegExp rxBlockSize("Block size:\\s*(\\d+)");
 
86
 
 
87
                        if (rxBlockSize.indexIn(cmd.output()) != -1)
 
88
                                blockSize = rxBlockSize.cap(1).toLongLong();
 
89
 
 
90
                        if (blockCount > -1 && freeBlocks > -1 && blockSize > -1)
 
91
                                return (blockCount - freeBlocks) * blockSize;
 
92
                }
 
93
 
 
94
                return -1;
 
95
        }
 
96
 
 
97
        QString ext2::readLabel(const QString& deviceNode) const
 
98
        {
 
99
                ExternalCommand cmd("e2label", QStringList() << deviceNode);
 
100
                return cmd.run() ? cmd.output().simplified() : QString();
 
101
        }
 
102
 
 
103
        bool ext2::check(Report& report, const QString& deviceNode) const
 
104
        {
 
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);
 
107
        }
 
108
 
 
109
        bool ext2::create(Report& report, const QString& deviceNode) const
 
110
        {
 
111
                return ExternalCommand(report, "mkfs.ext2", QStringList() << deviceNode).run(-1);
 
112
        }
 
113
 
 
114
        bool ext2::resize(Report& report, const QString& deviceNode, qint64 length) const
 
115
        {
 
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;
 
119
        }
 
120
 
 
121
        bool ext2::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
 
122
        {
 
123
                return ExternalCommand(report, "e2label", QStringList() << deviceNode << newLabel).run(-1);
 
124
        }
 
125
 
 
126
        bool ext2::updateUUID(Report& report, const QString& deviceNode) const
 
127
        {
 
128
                return ExternalCommand(report, "tune2fs", QStringList() << "-U" << "random" << deviceNode).run(-1);
 
129
        }
 
130
}