Last change
on this file since 29678 was 29678, checked in by Jeremy Symon, 9 years ago |
Moved xml i/o handling to the module for simplicity of use
|
File size:
1.6 KB
|
Line | |
---|
1 | package Greenstone::Directory;
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 | use utf8;
|
---|
6 | use base 'Exporter';
|
---|
7 |
|
---|
8 | use Greenstone::Config;
|
---|
9 |
|
---|
10 | our $VERSION = 1.00;
|
---|
11 | our @EXPORT = ( );
|
---|
12 |
|
---|
13 | sub new {
|
---|
14 | my ($parent, $class, $name, %args) = @_;
|
---|
15 | ref $class and die "Constructor used as object method\n";
|
---|
16 | defined $name or die "Must have a name\n";
|
---|
17 | my $self = bless (
|
---|
18 | {
|
---|
19 | name => $name,
|
---|
20 | parent => $parent,
|
---|
21 | },
|
---|
22 | $class
|
---|
23 | );
|
---|
24 | for my $param (keys %args) {
|
---|
25 | $self->{$param} = $args{$param};
|
---|
26 | }
|
---|
27 | return $self;
|
---|
28 | }
|
---|
29 |
|
---|
30 | sub list {
|
---|
31 | my $parent = shift;
|
---|
32 | ref $parent and die "Static method called on object\n";
|
---|
33 | my $dirs = {};
|
---|
34 | my $dir = "$Greenstone::Config::GSDL3HOME/$parent";
|
---|
35 | opendir DH, $dir or die "Could not open directory '$dir'\n";
|
---|
36 | while (my $entry = readdir DH) {
|
---|
37 | # skip hidden files and current/parent directory
|
---|
38 | $entry =~ /^\./ and next;
|
---|
39 | # skip non-directories
|
---|
40 | -d "$dir/$entry" or next;
|
---|
41 | # We don't really need to store anything more than the fact that the directory exists
|
---|
42 | # So we just store nothing, which is enough to put the key in the hash
|
---|
43 | # (i.e. it acts as a hashset)
|
---|
44 | $dirs->{$entry} = ();
|
---|
45 | }
|
---|
46 | closedir DH;
|
---|
47 | return $dirs;
|
---|
48 | }
|
---|
49 |
|
---|
50 | sub exists {
|
---|
51 | my $self = shift;
|
---|
52 | my $dirs = list $self->{parent};
|
---|
53 | return exists $dirs->{$self->{name}};
|
---|
54 | }
|
---|
55 |
|
---|
56 | sub dir {
|
---|
57 | my $self = shift;
|
---|
58 | return "$Greenstone::Config::GSDL3HOME/$self->{parent}/$self->{name}";
|
---|
59 | }
|
---|
60 |
|
---|
61 | sub create {
|
---|
62 | my $self = shift;
|
---|
63 | $self->exists and return 0;
|
---|
64 | mkdir $self->dir or die "Failed to create directory: $!\n";
|
---|
65 | return 1;
|
---|
66 | }
|
---|
67 |
|
---|
68 | 1;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.