summaryrefslogtreecommitdiff
path: root/storage/ndb/tools/old_dirs/ndbnet/lib/NDB/Util/Dir.pm
blob: 90609b971c754c8b1943a7b4c3fec9181aaa6d43 (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
package NDB::Util::Dir;

use strict;
use Carp;
use Symbol;
use Errno;
use File::Basename;

require NDB::Util::Base;

use vars qw(@ISA);
@ISA = qw(NDB::Util::Base);

# constructors

my $log;

sub initmodule {
    $log = NDB::Util::Log->instance;
}

NDB::Util::Dir->attributes(
    path => sub { length > 0 },
);

sub new {
    my $class = shift;
    @_ % 2 == 0 or confess 0+@_;
    my(%attr) = @_;
    my $dir = $class->SUPER::new(%attr);
    $dir->setpath($attr{path})
	or $log->push, return undef;
    return $dir;
}

sub desc {
    my $dir = shift;
    return $dir->getpath;
}

sub getparent {
    my $dir = shift;
    @_ == 0 or confess 0+@_;
    my $ppath = dirname($dir->getpath);
    my $pdir = NDB::Util::Dir->new(path => $ppath);
    return $pdir;
}

sub getdir {
    my $dir = shift;
    @_ == 1 or confess 0+@_;
    my($name) = @_;
    my $dirpath = $dir->getpath;
    my $path = $dirpath eq '.' ? $name : File::Spec->catfile($dirpath, $name);
    my $entry = NDB::Util::Dir->new(path => $path);
    return $entry;
}

sub getfile {
    my $dir = shift;
    @_ == 1 or confess 0+@_;
    my($name) = @_;
    my $dirpath = $dir->getpath;
    my $path = $dirpath eq '.' ? $name : File::Spec->catfile($dirpath, $name);
    my $entry = NDB::Util::File->new(path => $path);
    return $entry;
}

# list

sub listdirs {
    my $dir = shift;
    @_ == 0 or confess 0+@_;
    my @list = ();
    my $dirpath = $dir->getpath;
    my $dh = gensym();
    if (! opendir($dh, $dirpath)) {
	$log->put("opendir failed: $!")->push($dir);
	return undef;
    }
    while (defined(my $name = readdir($dh))) {
	if ($name eq '.' || $name eq '..') {
	    next;
	}
	my $path = $dirpath eq '.' ? $name : "$dirpath/$name";
	if (! -l $path && -d $path) {
	    my $dir2 = NDB::Util::Dir->new(path => $path)
		or $log->push, return undef;
	    push(@list, $dir2);
	}
    }
    close($dh);
    return \@list;
}

sub listfiles {
    my $dir = shift;
    @_ == 0 or confess 0+@_;
    my @list = ();
    my $dirpath = $dir->getpath;
    my $dh = gensym();
    if (! opendir($dh, $dirpath)) {
	$log->put("opendir failed: $!")->push($dir);
	return undef;
    }
    while (defined(my $name = readdir($dh))) {
	if ($name eq '.' || $name eq '..') {
	    next;
	}
	my $path = $dirpath eq '.' ? $name : "$dirpath/$name";
	if (! -d $path && -e $path) {
	    my $file2 = NDB::Util::File->new(path => $path)
		or $log->push, return undef;
	    push(@list, $file2);
	}
    }
    close($dh);
    return \@list;
}

# create / remove

sub mkdir {
    my $dir = shift;
    @_ == 0 or confess 0+@_;
    if (! -d $dir->getpath) {
	my $pdir = $dir->getparent;
	if (length($pdir->getpath) >= length($dir->getpath)) {
	    $log->put("mkdir looping")->push($dir);
	    return undef;
	}
	$pdir->mkdir or return undef;
	if (! mkdir($dir->getpath, 0777)) {
	    my $errstr = "$!";
	    if (-d $dir->getpath) {
		return 1;
	    }
	    $log->put("mkdir failed: $errstr")->push($dir);
	    return undef;
	}
    }
    return 1;
}

sub rmdir {
    my $dir = shift;
    my $keep = shift;		# keep top level
    $log->put("remove")->push($dir)->info;
    my $list;
    $list = $dir->listdirs or $log->push, return undef;
    for my $d (@$list) {
	$d->rmdir or $log->push, return undef;
    }
    $list = $dir->listfiles or $log->push, return undef;
    for my $f (@$list) {
	$f->unlink or $log->push, return undef;
    }
    if (! $keep && ! rmdir($dir->getpath)) {
	my $errstr = "$!";
	if (! -e $dir->getpath) {
	    return 1;
	}
	$log->put("rmdir failed: $errstr")->push($dir);
	return undef;
    }
    return 1;
}

1;
# vim:set sw=4: