File: copyfs-fversion

package info (click to toggle)
copyfs 1.0.1-5
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 272 kB
  • sloc: ansic: 1,766; perl: 276; makefile: 109; sh: 38
file content (416 lines) | stat: -rwxr-xr-x 8,412 bytes parent folder | download
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/perl -w

# copyfs - copy on write filesystem  http://n0x.org/copyfs/
# Copyright (C) 2004 Nicolas Vigier <boklm@mars-attacks.org>
#                    Thomas Joubert <widan@net-42.eu.org>
# This program can be distributed under the terms of the GNU GPL.
# See the file COPYING.

use strict;
use warnings;

use IPC::Open3;
use IO::Select;

use POSIX qw(strftime);
use Fcntl ':mode';
use File::Basename;
use File::stat;

use Errno;

use Getopt::Std;


#
# Run a command, and returns both output and error.
#
sub run_command($)
{
    my $command = shift;
    my ($output, $error, $msg);
    my $selector;
    my $pid;

    $output = "";
    $error = "";
    $pid = open3(*INPUT, *OUTPUT, *ERROR, $command);

    # No input
    close(INPUT);

    $selector = IO::Select->new();
    $selector->add(*ERROR, *OUTPUT);

    # Get what gets passed on the descriptors
    while (my @ready = $selector->can_read)
    {
	foreach my $fh (@ready)
	{
	    if (fileno($fh) == fileno(ERROR))
	    {
		$msg = scalar(<ERROR>);
		$error .= $msg if defined($msg);
	    }
	    else
	    {
		$msg = scalar(<OUTPUT>);
		$output .= $msg if defined($msg);
	    }
	    $selector->remove($fh) if eof($fh);
	}
    }

    close(OUTPUT);
    close(ERROR);

    # Kill the zombie
    waitpid($pid, 0);

    return ($output, $error);
}

#
# Get an extended attribute.
#
sub get_ea($$)
{
    my ($file, $name) = @_;
    my ($output, $error);
    my $cmd;

    $cmd = "getfattr --absolute-names -h -n '$name' --only-values -- '$file'";
    ($output, $error) = run_command($cmd);
    $error =~ s/^getfattr: // if $error;
    return ($output, $error);
}

#
# Set an extended attribute.
#
sub set_ea($$$)
{
    my ($file, $name, $value) = @_;
    my ($output, $error);
    my $cmd;

    $cmd = "setfattr -h -n '$name' -v '$value' -- '$file'";
    ($output, $error) = run_command($cmd);
    $error =~ s/^setfattr: // if $error;
    return ($output, $error);
}

#
# Get current version number.
#
sub get_current_version($)
{
    my $file = shift;
    my ($value, $error);

    ($value, $error) = get_ea($file, "rcs.locked_version");
    die "$0: $error" if $error;
    return split(/\./, $value);
}

#
# Set current version number.
#
sub set_current_version($$$)
{
    my ($file, $vid, $svid) = @_;

    my ($value, $error) = set_ea($file, "rcs.locked_version", "$vid.$svid");
    die "$0: $error" if $error;
}

#
# Dump a mode into an ls-like string.
#
sub get_perms($)
{
    my $mode = shift;
    my $result = "";

    # Type
    $result .= "s" if S_ISSOCK($mode);
    $result .= "l" if S_ISLNK($mode);
    $result .= "-" if S_ISREG($mode);
    $result .= "b" if S_ISBLK($mode);
    $result .= "d" if S_ISDIR($mode);
    $result .= "c" if S_ISCHR($mode);
    $result .= "p" if S_ISFIFO($mode);

    # Mode for owner
    $result .= (($mode & S_IRUSR) ? "r" : "-");
    $result .= (($mode & S_IWUSR) ? "w" : "-");
    $result .= (($mode & S_ISUID) ? (($mode & S_IXUSR) ? "s" : "S") :
		(($mode & S_IXUSR) ? "x" : "-"));

    # Mode for group
    $result .= (($mode & S_IRGRP) ? "r" : "-");
    $result .= (($mode & S_IWGRP) ? "w" : "-");
    $result .= (($mode & S_ISGID) ? (($mode & S_IXGRP) ? "s" : "S") :
		(($mode & S_IXGRP) ? "x" : "-"));

    # Mode for group
    $result .= (($mode & S_IROTH) ? "r" : "-");
    $result .= (($mode & S_IWOTH) ? "w" : "-");
    $result .= (($mode & S_ISVTX) ? (($mode & S_IXOTH) ? "t" : "T") :
		(($mode & S_IXGRP) ? "x" : "-"));

    return $result;
}

#
# Dump the owner in string form.
#
sub dump_owner($)
{
    my $uid = shift;
    my $value;

    $value = getpwuid($uid);
    return $value if $value;
    return "$uid";
}

#
# Dump the group in string form.
#
sub dump_group($)
{
    my $gid = shift;
    my $value;

    $value = getgrgid($gid);
    return $value if $value;
    return "$gid";
}

#
# Dump the version information for a file.
#
sub dump_versions($)
{
    my $file = shift;

    # Get the active revision
    my ($cvid, $csvid) = get_current_version($file);

    # Get the attribute
    my ($value, $error) = get_ea($file, "rcs.metadata_dump");
    die "$0: $error" if $error;

    printf("File %s (%s is active) :\n", $file,
	   ($cvid != -1) ? "'*'" : "last");

    # Explode the attributes
    my @versions = reverse(split(/\|/, $value));
    foreach my $vstring (@versions)
    {
	my ($vid, $svid, $mode, $uid, $gid, $size,
	    $mtime) = split(/:/, $vstring);

	# Display it
	printf("  v%-4.4s : %s  %-8.8s %-8.8s %10i %s", "$vid.$svid",
	       get_perms($mode), dump_owner($uid), dump_group($gid),
	       $size, strftime("%c", localtime($mtime)));

	printf(" [*]") if (($cvid == $vid) && ($csvid == $svid));
	printf("\n");
    }
}

#
# Generate the tag data for a directory
#
sub generate_tag_file_for_directory
{
    my ($fh, $directory) = @_;
    my @dirs = ();

    # Find files and directories
    opendir(DIR, $directory) or die "$0: Can't open directory !\n";
    while (my $item = readdir(DIR))
    {
	if (($item ne ".") && ($item ne ".."))
	{
	    my ($vid, $svid) = get_current_version("$directory/$item");
	    my $st = lstat("$directory/$item");

	    push @dirs, $item if (S_ISDIR($st->mode));
	    print $fh "$directory/$item|$vid.$svid\n";
	}
    }
    closedir(DIR);

    # Iterate over directories
    for my $item (@dirs)
    {
	generate_tag_file_for_directory($fh, "$directory/$item");
    }
}

#
# Generate the tag data
#
sub generate_tag_file($$)
{
    my ($root, $tagname) = @_;
    my $fh;

    open $fh, ">$tagname" or die "$0: Can't open tagfile !\n";
    generate_tag_file_for_directory($fh, $root);
    close $fh;
}

#
# Restore a tag file
#
sub restore_tag_file($$)
{
    my ($root, $tagname) = @_;

    # Create the root dir if it does no exist
    if (!lstat($root))
    {
	print "Creating root directory $root\n";
	mkdir($root);
    }

    open TAG, $tagname or die "$0: Can't open tagfile !\n";
    while (my $line = <TAG>)
    {
	if ($line =~ m/^([^\|]+)\|(\d+).(\d+)/)
	{
	    my ($file, $vid, $svid) = ($1, $2, $3);

	    if ($file !~ m/^\Q$root\E\//)
	    {
		print STDERR "$0: you are trying to put a tag in a different ";
		print STDERR "directory !\n";
		exit(1);
	    }

	    my $st = lstat($file);

	    if (!$st)
	    {
		my $done = 0;

		# It can take some time to change files back into directories
		# since fuse caches the lookup info for about one second, so
		# we retry here.
		do
		{
		    # Touch the file first, since it does not exist
		    if (!open(FILE, ">$file"))
		    {
			# "Not a directory" ignored
			print STDERR "$0: could not touch $file !\n"
			    if (!$!{ENOTDIR});
		    }
		    else
		    {
			$done = 1;
			close(FILE);
		    }
		}
		while (!$done && $!{ENOTDIR});
	    }

	    # Fix version
	    set_current_version($file, $vid, $svid);

	    printf("Restored $file to version $vid.$svid\n");
	}
    }

    close TAG;
}



# Strip the path
$0 = basename($0);

my %options = ();

# Parse command line
getopts("ghl:rst:u:", \%options);

if ($options{h})
{
    printf("Usage: $0 [-h] [-r] [-s] [-l version] [-g] file\n");
    printf("\n");
    printf("  -h           Show this help\n");

    # Version management
    printf("  -r           Release the version lock\n");
    printf("  -s           Show the versions for this file (default)\n");
    printf("  -g           Get the version number in use\n");
    printf("  -l version   Lock this version\n");

    # Tagging
    printf("  -t tagfile   Create a tag file\n");
    printf("  -u tagfile   Restore a tag file\n");
    exit(0);
}

# We need a file there
if (scalar(@ARGV) != 1)
{
    print STDERR "$0: You need to specify one file to operate on.\n";
    exit(1);
}

# Show by default
$options{s} = 1
    if (!$options{r} && !$options{g} && !$options{s} && !$options{l} &&
	!$options{t} && !$options{u});

if ($options{r})
{
    set_current_version($ARGV[0], -1, -1);
    exit(0);
}

if ($options{g})
{
    my ($vid, $svid) = get_current_version($ARGV[0]);
    print "$vid.$svid\n";
    exit(0);
}

if ($options{s})
{
    dump_versions($ARGV[0]);
    exit(0);
}

if ($options{l})
{
    if ($options{l} =~ m/^(\d+).(\d+)$/)
    {
	set_current_version($ARGV[0], $1, $2);
	exit(0);
    }
    else
    {
	print STDERR "$0: This version number is incorrect (format is x.y)\n";
	exit(1);
    }
}

if ($options{t})
{
    generate_tag_file($ARGV[0], $options{t});
    exit(0);
}

if ($options{u})
{
    restore_tag_file($ARGV[0], $options{u});
    exit(0);
}