File: LabeledPartition.pm

package info (click to toggle)
yaird 0.0.12-18etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 1,432 kB
  • ctags: 725
  • sloc: perl: 4,161; xml: 3,233; ansic: 3,105; sh: 876; makefile: 150
file content (152 lines) | stat: -rw-r--r-- 3,964 bytes parent folder | download | duplicates (2)
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
#!perl -w
#
# LabeledPartition -- a partition with ext2 or other label.
#   Copyright (C) 2005  Erik van Konijnenburg
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# descriptors contain:
# - label - string such as volume name for matching in /etc/fstab
# - uuid - similar but hex
# - path - the block special file where the label was found
# - type - what kind of superblock the label was found in.
#
# NOTE: consider delegating this whole mess to findfs from the
# e2fsprogs package.  That package can probe for an extensive
# set of superblocks, knows about LVM, but does not make fs type
# information available via the command line.
#
use strict;
use warnings;
package LabeledPartition;
use base 'Obj';


sub fill {
	my $self = shift;
	$self->SUPER::fill();
	$self->takeArgs ('label', 'uuid', 'path', 'type');
}

sub label	{ return $_[0]->{label}; }
sub uuid	{ return $_[0]->{uuid}; }
sub type	{ return $_[0]->{type}; }
sub path	{ return $_[0]->{path}; }

sub string {
	my $self = shift;
	my $label = ($self->label or '-');
	my $uuid = ($self->uuid or '-');
	my $type = $self->type;
	my $path = $self->path;
	return "labeled($type) $label, $uuid at $path";
}


#
# try -- return descriptor for labeled fs, or undef,
# trying a number of formats for superblock.
#
sub try ($) {
	my ($path) = @_;
	my $result = undef;
	my $fh;
	if (! open ($fh, "<", "$path")) {
		return $result;
	}
	binmode ($fh);
	$result = tryExt2 ($fh, $path) unless defined $result;
	$result = tryReiser ($fh, $path) unless defined $result;

	# ignore errors, could not have been very wrong
	# if a working superblock came out of it.
	close ($fh);
	return $result;
}


#
# lets hope the sysadm labeled it in ascii not utf8.
# based on util-linux-2.12h mount; they have lots of other
# superblocks to match.  Later.
#
sub tryExt2 ($$) {
	my ($fh, $path) = @_;
	my $result = undef;

	if (! seek ($fh, 1024, 0)) {
		return $result;
	}
	# undef on read error - is this worth a warning?
	my $superBlock;
	my $rc = read ($fh, $superBlock, 1024);
	if (! defined ($rc) || $rc != 1024) {
		return $result;
	}
	my ($magic, $uuid, $label) =
		unpack
		"x[56] v x[34] x[VVV] H[32] Z[16]",
		$superBlock;

	if ($magic == 0xEF53) {
		$uuid = fancyUuid ($uuid);
		$result = LabeledPartition->new (
			type => "ext2",
			label => $label,
			uuid => $uuid,
			path => $path,
			);
	}
	return $result;
}

# fancyUuid - given a hex uuid, add hyphens at conventional place.
sub fancyUuid ($) {
	my ($uuid) = @_;
	$uuid =~ s/^(.{8})(.{4})(.{4})(.{4})(.{12})$/$1-$2-$3-$4-$5/;
	return lc ($uuid);
}

sub tryReiser ($$) {
	my ($fh, $path) = @_;
	my $result = undef;
	# offset in reisser 3.6 (3.5-3.5.10 had 8k)
	if (! seek ($fh, (64*1024), 0)) {
		return $result;
	}
	# undef on read error - is this worth a warning?
	my $superBlock;
	my $rc = read ($fh, $superBlock, 1024);
	if (! defined ($rc) || $rc != 1024) {
		return $result;
	}
	my ($magic, $uuid, $label) =
		unpack
		"x[52] Z[10] x[22] a[16] Z[16]",
		$superBlock;
	my $t = join ("", map { sprintf "%02x", ord($_) } split(//, $uuid));
	if ($magic eq "ReIsEr2Fs" || $magic eq "ReIsEr3Fs") {
		$uuid = fancyUuid ($uuid);
		$result = LabeledPartition->new (
			type => "reiserfs",
			label => $label,
			uuid => fancyUuid($t),
			path => $path,
			);
	}
	return $result;
}

1;