1############################################################
2#
3#   $Id: Darwin.pm 185 2010-07-15 19:25:30Z trevor $
4#   Sys::Filesystem - Retrieve list of filesystems and their properties
5#
6#   Copyright 2004,2005,2006 Nicola Worthington
7#   Copyright 2008,2009      Jens Rehsack
8#
9#   Licensed under the Apache License, Version 2.0 (the "License");
10#   you may not use this file except in compliance with the License.
11#   You may obtain a copy of the License at
12#
13#       http://www.apache.org/licenses/LICENSE-2.0
14#
15#   Unless required by applicable law or agreed to in writing, software
16#   distributed under the License is distributed on an "AS IS" BASIS,
17#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18#   See the License for the specific language governing permissions and
19#   limitations under the License.
20#
21############################################################
22
23package Sys::Filesystem::Darwin;
24
25# vim:ts=4:sw=4:tw=78
26
27use 5.006;
28
29use strict;
30use warnings;
31use vars qw(@ISA $VERSION);
32
33require Sys::Filesystem::Unix;
34
35use Carp qw(croak);
36
37$VERSION = '1.30';
38@ISA     = qw(Sys::Filesystem::Unix);
39
40sub version()
41{
42    return $VERSION;
43}
44
45my @dt_keys     = qw(fs_spec fs_file fs_vfstype fs_name);
46my @mount_keys1 = qw(fs_spec fs_file fs_vfstype);
47my @mount_keys2 = qw(fs_spec fs_file fs_mntops);
48my %special_fs = (
49                   devfs  => 1,
50                   autofs => 1,
51                 );
52
53my $dt_rx = qr/Disk\sAppeared\s+\('([^']+)',\s*
54               Mountpoint\s*=\s*'([^']+)',\s*
55               fsType\s*=\s*'([^']*)',\s*
56               volName\s*=\s*'([^']*)'\)/x;
57my $mount_rx1 = qr/(.*) on (.*) \((\w+),?.*\)/;    # /dev/disk on / (hfs,...)
58my $mount_rx2 = qr/(.*) on (.*) \(([^)]*)\)/;      # /dev/disk on / (hfs,...)
59
60sub new
61{
62    my ( $class, %args ) = @_;
63    my $self = bless( {}, $class );
64
65    $args{diskutil} ||= '/usr/sbin/diskutil';
66    $args{mount}    ||= '/sbin/mount';
67
68    # don't use backticks, don't use the shell
69    my @fslist  = ();
70    my @mntlist = ();
71    open( my $dt_fh, '-|' ) or exec( $args{diskutil}, 'list' ) or croak("Cannot execute $args{diskutil}: $!\n");
72    @fslist = <$dt_fh>;
73    close($dt_fh);
74    open( my $m_fh, '-|' ) or exec( $args{mount} ) or croak("Cannot execute $args{mount}: $!\n");
75    @mntlist = <$m_fh>;
76    close($m_fh);
77
78    $self->readMounts( $dt_rx, [ 0, 1, 2 ], \@dt_keys, \%special_fs, @fslist );
79
80    #foreach (@fslist)
81    #{
82    #    # For mounted FTP servers, fsType and volName are empty on Mac OS X 10.3
83    #    # However, Mountpoint should not be empty.
84    #    next unless (/Disk Appeared \('([^']+)',Mountpoint = '([^']+)', fsType = '([^']*)', volName = '([^']*)'\)/);
85    #    my ( $device, $mount_point, $fstype, $name ) = ( $1, $2, $3, $4 );
86
87    #    $self->{$mount_point}->{mounted}     = 1;
88    #    $self->{$mount_point}->{special}     = 0;
89    #    $self->{$mount_point}->{device}      = $device;
90    #    $self->{$mount_point}->{mount_point} = $mount_point;
91    #    $self->{$mount_point}->{fs_vfstype}  = $fstype;
92    #    $self->{$mount_point}->{fs_mntops}   = '';
93    #    $self->{$mount_point}->{label}       = $name;
94    #}
95
96    $self->readMounts( $mount_rx1, [ 0, 1, 2 ], \@mount_keys1, \%special_fs, @mntlist );
97    $self->readMounts( $mount_rx2, [ 0, 1 ], \@mount_keys2, undef, @mntlist );
98
99    # set the mount options
100    #foreach (@mntlist)
101    #{
102    #    next unless (/(.*) on (.*) \((.*)\)/);    # /dev/disk on / (hfs,...)
103    #    my ( $device, $mount_point, $mntopts ) = ( $1, $2, $3 );
104    #    if ( exists( $self->{$mount_point} ) )
105    #    {
106    #        $self->{$mount_point}->{fs_mntops} = $mntopts;
107    #    }
108    #}
109
110    $self;
111}
112
1131;
114
115=head1 NAME
116
117Sys::Filesystem::Darwin - Return Darwin (Mac OS X) filesystem information to Sys::Filesystem
118
119=head1 SYNOPSIS
120
121See L<Sys::Filesystem>.
122
123=head1 DESCRIPTION
124
125The filesystem information is taken from diskutil, the system utility
126supplied on Mac OS X.
127
128=head1 INHERITANCE
129
130  Sys::Filesystem::Darwin
131  ISA Sys::Filesystem::Unix
132    ISA UNIVERSAL
133
134=head1 METHODS
135
136=over 4
137
138=item version ()
139
140Return the version of the (sub)module.
141
142=back
143
144=head1 ATTRIBUTES
145
146The following is a list of filesystem properties which may
147be queried as methods through the parent L<Sys::Filesystem> object.
148
149The property 'label' is also set, but cannot be queried by L<Sys::Filesystem>
150yet.
151
152=over 4
153
154=item mount_point
155
156The mount point (usually either '/' or '/Volumes/...').
157
158=item device
159
160The mounted device
161
162=item format
163
164Describes the type of the filesystem. So far I encountered the following types:
165
166=over 4
167
168=item hfs
169
170The standard Mac OS X HFS(+) filesystem. Disk images (.dmg) and
171Mac Software DVDs normally also use the HFS(+) format.
172
173=item msdos
174
175DOS image files (e.g. floppy disk images)
176
177=item cd9660
178
179CD-ROM image files or real CD-ROMs
180
181=item cddafs
182
183Audio CDs
184
185=item udf
186
187UDF filesystem (e.g. DVDs)
188
189=back
190
191=item (empty)
192
193For mounted FTP servers, diskutil returns an empty filesystem type (ie, '').
194
195=back
196
197=head1 BUGS
198
199Doesn't take /etc/fstab or /etc/xtab into account right now, since they are
200normally not used. Contact the author if you need this.
201
202=head1 SEE ALSO
203
204L<Sys::Filesystem>, L<diskutil>
205
206=head1 VERSION
207
208$Id: Darwin.pm 185 2010-07-15 19:25:30Z trevor $
209
210=head1 AUTHOR
211
212Christian Renz <crenz@web42.com>
213
214Jens Rehsack <rehsack@cpan.org> - L<http://www.rehsack.de/>
215
216=head1 COPYRIGHT
217
218Copyright 2004,2005,2006 Nicola Worthington.
219Copyright 2009,2010 Jens Rehsack.
220
221This software is licensed under The Apache Software License, Version 2.0.
222
223L<http://www.apache.org/licenses/LICENSE-2.0>
224
225=cut
226
227