summaryrefslogtreecommitdiff
path: root/media.pl.in
blob: 7a872ad7a1851ab91e280d521ab6af20fde76544 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

# Detect, list and manipulate local media devices.
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Hans Petter Jansson <hpj@ximian.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.


require "___scriptsdir___/xml.pl";
require "___scriptsdir___/parse.pl";


# xst_media_get_ide_device_from_proc
#
# Read IDE device specs for a single device from constructed /proc subdir and
# a couple of other places.

sub xst_media_get_ide_device_from_proc
{
  my ($path) = "/proc/ide/$_[0]/$_[1]";
  my (%device);

  %device->{"device"}   = $_[1];
  %device->{"type"}     = "ide";
  %device->{"media"}    = &xst_parse_line_first ("$path/media");
  %device->{"model"}    = &xst_parse_line_first ("$path/model");
  %device->{"cache"}    = &xst_parse_line_first ("$path/cache");
  %device->{"capacity"} = &xst_parse_line_first ("$path/capacity");
  %device->{"driver"}   = (split ' ', &xst_parse_line_first ("$path/driver")) [0];

  # TODO: We need a detailed list of media types that can occur, and what
  # special handling they need. Currently recognized are: disk, cdrom.
  #
  # Currently, everything that's not a disk is removable.

  if (%device->{"media"} eq "disk")
  {
    %device->{"is_removable"} = 0;

    # Disk devices can't be mounted and don't have file systems. Their
    # partitions can and do, however.

    # TODO: Partition information gathered in a sub-hash.
  }
  else  # (%device->{"media"} eq "cdrom")
  {
    %device->{"is_removable"} = 1;

    %device->{"point_listed"} = &xst_parse_split_first_str ("/etc/fstab", "/dev/$_[1]", "[ \t]+");
    %device->{"point_actual"} = &xst_parse_split_first_str ("/etc/mtab", "/dev/$_[1]", "[ \t]+");
    %device->{"fs_listed"}    = (&xst_parse_split_all ("/etc/fstab", "/dev/$_[1]", "[ \t]+")) -> [1];
    %device->{"fs_actual"}    = (&xst_parse_split_all ("/etc/mtab", "/dev/$_[1]", "[ \t]+")) -> [1];

    if (%device->{"point_actual"})
    {
      %device->{"is_mounted"} = 1;
    }
    else
    {
      %device->{"is_mounted"} = 0;
    }
  }

  return %device;
}


# xst_media_get_list_from_proc
#
# Scan /proc files for media devices, and return a list of hashes.

sub xst_media_get_list_from_proc
{
  local (*PROC_IDE_DIR, *PROC_IDE_CHANNEL_DIR);
  my (@devices);

  if (!(stat ("/proc"))) { return undef; }

  # IDE devices.

  if (!(opendir (PROC_IDE_DIR, "/proc/ide"))) { return undef; }

  foreach $ide_entry (readdir (PROC_IDE_DIR))
  {
    if ($ide_entry =~ /ide[0-9]/)
    {
      if (!(opendir (PROC_IDE_CHANNEL_DIR, "/proc/ide/$ide_entry"))) { next; }

      foreach $ide_channel_entry (readdir (PROC_IDE_CHANNEL_DIR))
      {
        # NOTE: This is just checking if the entry is a directory. I have a
        # feeling it's more portable than stat().

        if ($ide_channel_entry eq "." || $ide_channel_entry eq ".." ||
            !(opendir (PROC_IDE_DEVICE, "/proc/ide/$ide_entry/$ide_channel_entry")))
        {
          next;
        }

        closedir (PROC_IDE_DEVICE);

        my %device = &xst_media_get_ide_device_from_proc ($ide_entry, $ide_channel_entry);
        if (%device)
        {
          push @devices, \%device;
        }
      }
    }
  }

  return @devices;
}


# xst_media_get_list
#
# Return a list of hashes describing the media devices present
# on this machine.

sub xst_media_get_list
{
  my @devices;

  @devices = &xst_media_get_list_from_proc();
  return @devices;
}


# xst_media_xml_print
#
# Given a media hash-list, prints the media inside a <media> tag
# pair, using current indent levels.

sub xst_media_xml_print
{
  my @devices = @_;

  &xst_xml_print_vspace ();
  &xst_xml_print_line ("<media>\n");
  &xst_xml_enter ();

  for $dev (@devices)
  {
    &xst_xml_print_vspace ();
    &xst_xml_print_line ("<device>\n");
    &xst_xml_enter ();
    &xst_xml_print_line ("<name>" . $dev->{"device"} . "</name>\n"); 
    &xst_xml_print_line ("<type>" . $dev->{"media"} . "</type>\n"); 
    &xst_xml_print_line ("<interface>" . $dev->{"type"} . "</interface>\n"); 
    &xst_xml_print_line ("<driver>" . $dev->{"driver"} . "</driver>\n"); 
    &xst_xml_print_line ("<model>" . $dev->{"model"} . "</model>\n");

    if ($dev->{"media"} ne "disk")
    {
      &xst_xml_print_vspace ();
      if ($dev->{"point_listed"})
      {
        &xst_xml_print_line ("<point_listed>" . $dev->{"point_listed"} . "</point_listed>\n");
      }

      xst_xml_print_state_tag ("mounted", $dev->{"is_mounted"});
    }

    &xst_xml_leave ();
    &xst_xml_print_line ("</device>\n");
    &xst_xml_print_vspace ();
  }

  &xst_xml_leave ();
  &xst_xml_print_line ("</media>\n");
  &xst_xml_print_vspace ();
}


# xst_media_xml_parse
#
#

sub xst_media_xml_parse
{
}