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

« back to all changes in this revision

Viewing changes to src/fs/jfs.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/jfs.h"
 
21
 
 
22
#include "util/externalcommand.h"
 
23
#include "util/report.h"
 
24
#include "util/capacity.h"
 
25
 
 
26
#include <QStringList>
 
27
#include <QRegExp>
 
28
 
 
29
#include <klocale.h>
 
30
#include <ktempdir.h>
 
31
 
 
32
#include <unistd.h>
 
33
 
 
34
namespace FS
 
35
{
 
36
        FileSystem::SupportType jfs::m_GetUsed = FileSystem::SupportNone;
 
37
        FileSystem::SupportType jfs::m_GetLabel = FileSystem::SupportNone;
 
38
        FileSystem::SupportType jfs::m_Create = FileSystem::SupportNone;
 
39
        FileSystem::SupportType jfs::m_Grow = FileSystem::SupportNone;
 
40
        FileSystem::SupportType jfs::m_Move = FileSystem::SupportNone;
 
41
        FileSystem::SupportType jfs::m_Check = FileSystem::SupportNone;
 
42
        FileSystem::SupportType jfs::m_Copy = FileSystem::SupportNone;
 
43
        FileSystem::SupportType jfs::m_Backup = FileSystem::SupportNone;
 
44
        FileSystem::SupportType jfs::m_SetLabel = FileSystem::SupportNone;
 
45
 
 
46
        jfs::jfs(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label) :
 
47
                FileSystem(firstsector, lastsector, sectorsused, label, FileSystem::Jfs)
 
48
        {
 
49
        }
 
50
 
 
51
        void jfs::init()
 
52
        {
 
53
                m_GetUsed = findExternal("jfs_debugfs") ? SupportExternal : SupportNone;
 
54
                m_SetLabel = m_GetLabel = findExternal("jfs_tune", QStringList() << "-V") ? SupportExternal : SupportNone;
 
55
                m_Create = findExternal("mkfs.jfs", QStringList() << "-V") ? SupportExternal : SupportNone;
 
56
                m_Grow = m_Check = findExternal("fsck.jfs", QStringList() << "-V") ? SupportExternal : SupportNone;
 
57
                m_Copy = m_Move = (m_Check != SupportNone) ? SupportInternal : SupportNone;
 
58
                m_Backup = SupportInternal;
 
59
        }
 
60
 
 
61
        qint64 jfs::minCapacity() const
 
62
        {
 
63
                return 16 * Capacity::unitFactor(Capacity::Byte, Capacity::MiB);
 
64
        }
 
65
        
 
66
        qint64 jfs::readUsedCapacity(const QString& deviceNode) const
 
67
        {
 
68
                ExternalCommand cmd("jfs_debugfs", QStringList() << deviceNode);
 
69
 
 
70
                if (cmd.start() && cmd.write("dm") == 2 && cmd.waitFor())
 
71
                {
 
72
                        qint64 blockSize = -1;
 
73
                        QRegExp rxBlockSize("Block Size: (\\d+)");
 
74
 
 
75
                        if (rxBlockSize.indexIn(cmd.output()) != -1)
 
76
                                blockSize = rxBlockSize.cap(1).toLongLong();
 
77
 
 
78
                        qint64 nBlocks = -1;
 
79
                        QRegExp rxnBlocks("dn_mapsize:\\s+0x([0-9a-f]+)");
 
80
 
 
81
                        bool ok = false;
 
82
                        if (rxnBlocks.indexIn(cmd.output()) != -1)
 
83
                        {
 
84
                                nBlocks = rxnBlocks.cap(1).toLongLong(&ok, 16);
 
85
                                if (!ok)
 
86
                                        nBlocks = -1;
 
87
                        }
 
88
                        
 
89
                        qint64 nFree = -1;
 
90
                        QRegExp rxnFree("dn_nfree:\\s+0x([0-9a-f]+)");
 
91
 
 
92
                        if (rxnFree.indexIn(cmd.output()) != -1)
 
93
                        {
 
94
                                nFree = rxnFree.cap(1).toLongLong(&ok, 16);
 
95
                                if (!ok)
 
96
                                        nFree = -1;
 
97
                        }
 
98
 
 
99
                        if (nBlocks > -1 && blockSize > -1 && nFree > -1)
 
100
                                return (nBlocks - nFree) * blockSize;
 
101
                }
 
102
 
 
103
                return -1;
 
104
        }
 
105
 
 
106
        QString jfs::readLabel(const QString& deviceNode) const
 
107
        {
 
108
                ExternalCommand cmd("jfs_tune", QStringList() << "-l" << deviceNode);
 
109
 
 
110
                if (cmd.run())
 
111
                {
 
112
                        QRegExp rxLabel("Volume label:\\s+'(\\w+)'");
 
113
 
 
114
                        if (rxLabel.indexIn(cmd.output()) != -1)
 
115
                                return rxLabel.cap(1).simplified();
 
116
                }
 
117
 
 
118
                return QString();
 
119
        }
 
120
 
 
121
        bool jfs::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
 
122
        {
 
123
                return ExternalCommand(report, "jfs_tune", QStringList() << "-L" << newLabel << deviceNode).run(-1);
 
124
        }
 
125
 
 
126
        bool jfs::check(Report& report, const QString& deviceNode) const
 
127
        {
 
128
                ExternalCommand cmd(report, "fsck.jfs", QStringList() << "-f" << deviceNode);
 
129
                return cmd.run(-1) && (cmd.exitCode() == 0 || cmd.exitCode() == 1);
 
130
        }
 
131
 
 
132
        bool jfs::create(Report& report, const QString& deviceNode) const
 
133
        {
 
134
                return ExternalCommand(report, "mkfs.jfs", QStringList() << "-q" << deviceNode).run(-1);
 
135
        }
 
136
        
 
137
        bool jfs::resize(Report& report, const QString& deviceNode, qint64) const
 
138
        {
 
139
                KTempDir tempDir;
 
140
                if (!tempDir.exists())
 
141
                {
 
142
                        report.line() << i18nc("@info/plain", "Resizing JFS file system on partition <filename>%1</filename> failed: Could not create temp dir.", deviceNode);
 
143
                        return false;
 
144
                }
 
145
 
 
146
                bool rval = false;
 
147
 
 
148
                ExternalCommand mountCmd(report, "mount", QStringList() << "-v" << "-t" << "jfs" << deviceNode << tempDir.name());
 
149
                
 
150
                if (mountCmd.run(-1))
 
151
                {
 
152
                        ExternalCommand resizeMountCmd(report, "mount", QStringList() << "-v" << "-t" << "jfs" << "-o" << "remount,resize" << deviceNode << tempDir.name());
 
153
                
 
154
                        if (resizeMountCmd.run(-1))
 
155
                                rval = true;
 
156
                        else
 
157
                                report.line() << i18nc("@info/plain", "Resizing JFS file system on partition <filename>%1</filename> failed: Remount failed.", deviceNode);
 
158
                                
 
159
                        ExternalCommand unmountCmd(report, "umount", QStringList() << tempDir.name());
 
160
 
 
161
                        if (!unmountCmd.run(-1))
 
162
                                report.line() << i18nc("@info/plain", "Warning: Resizing JFS file system on partition <filename>%1</filename>: Unmount failed.", deviceNode);
 
163
                }
 
164
                else
 
165
                        report.line() << i18nc("@info/plain", "Resizing JFS file system on partition <filename>%1</filename> failed: Initial mount failed.", deviceNode);
 
166
                
 
167
                return rval;
 
168
        }
 
169
}