aboutsummaryrefslogtreecommitdiffstats
path: root/linux/use_dir.pl
blob: 21722705004737a6e38298c323b61889b88d33cd (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
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
#!/usr/bin/perl
use strict;
use File::Find;
use File::Path;
use File::Copy;
use Fcntl ':mode';
use Getopt::Long;
use Digest::SHA1;

my $silent = 0;
my $debug = 0;
my $recheck = 0;
my $get_patched = 0;
GetOptions( "--debug" => \$debug,
	    "--silent" => \$silent,
	    "--recheck" => \$recheck,
	    "--get_patched" => \$get_patched,
	  );

my $dir = shift;

my $ctlfile = ".linked_dir";
my $patchfile = ".patches_applied";

my $sync_patched = 0;
my %dirs;
my %files;
my $patches_applied;

#########################################
# Control info stored at the control file
my $path;
my %fhash;
my %fhash_patched;
#########################################

sub read_ctlfile()
{
	my $line;

	open IN, $ctlfile or return;
	while (<IN>) {
		next if (m/^\s*\#/);
		next if (m/^\n$/);
		if (m/^path:\s*([^\s]+)/) {
			$path = $1;
		} elsif (m/^hash\:\s*([^\s]+)\s*=\s*([^\s]+)/) {
			$fhash{$1} = $2;
		} elsif (m/^hash_patched\:\s*([^\s]+)\s*=\s*([^\s]+)/) {
			$fhash_patched{$1} = $2;
		} else {
			printf("Parse error on this line of $ctlfile:\n\t$_");
			die;
		}
	}
	close IN;
}

sub write_ctlfile()
{
	open OUT, ">$ctlfile" or print "Error: Can't write to $ctlfile\n";
	print OUT "path: $path\n";
	foreach my $file (keys %fhash) {
		printf OUT "hash: %s=%s\n", $file, $fhash{$file};
	}
	foreach my $file (keys %fhash_patched) {
		printf OUT "hash_patched: %s=%s\n", $file, $fhash_patched{$file};
	}
	close OUT;
}

sub add_dirs($)
{
	my $data = shift;
	my @dirs = split(' ', $data);

	foreach my $val (@dirs) {
		$dirs{$val} = 1;
	}
}

sub add_files($)
{
	my $data = shift;
	my @dirs = split(' ', $data);

	foreach my $val (@dirs) {
		$files{$val} = 1;
	}
}

sub get_file_dir_names()
{
	open IN, "Makefile" or die "Couldn't open Makefile";
	while (<IN>) {
		if (m/^\s*TARDIR\s*[\+\:]*=\s*([A-Za-z_].*)/) {
			add_dirs($1);
		} elsif (m/^\s*TARFILES\s*[\+\:]*=\s*([A-Za-z_].*)/) {
			add_files($1);
		}
	}
	close IN;
}


sub hash_calc($)
{
	my $file = shift;

	my $ctx = Digest::SHA1->new;

	my $rc = open INHASH, $file;
	if (!$rc) {
		print "Couldn't open file $file\n" if ($debug);
		return 0;
	}
	$ctx->addfile(*INHASH);
	my $digest = $ctx->hexdigest;
	close INHASH;

	return $digest;
}

sub sync_files($)
{
	my $file = shift;
	my $path = $file;
	my $check_hash;
	my $need_sync;
	my $filehash;
	my $cpfilehash;
	my $patched_file;

	$path =~ s,/[^/]+$,,;

	$filehash = hash_calc("$dir/$file");
	$need_sync = 1 if ($filehash ne $fhash{$file});

	if (!$need_sync && $recheck) {
		$cpfilehash = hash_calc("$file");
		if ($patches_applied && exists($fhash_patched{$file})) {
			$patched_file = 1;
			$need_sync = 1 if ($cpfilehash ne $fhash_patched{$file});
		} else {
			$need_sync = 1 if ($cpfilehash ne $fhash{$file});
		}
	}

	if ($need_sync) {
		printf "Sync'ing file $file (orig = %s, copy = %s, patched = %s)\n",
			$filehash, $cpfilehash, $fhash_patched{$file} if ($debug || $recheck);

		if (exists($fhash_patched{$file})) {
			$sync_patched = 1;
		} else {
			$fhash{$file} = $filehash;
			mkpath($path);
			copy("$dir/$file", $file);
		}
	} else {
		print "Skipping file $file, as is already synchronized\n" if ($debug);
	}
}

sub get_patched_files()
{
	my %files;

	open IN, $patchfile;
	while (<IN>) {
		next if (/^\s*#/);

		if (m/(.*)\n/) {
			print ("Backport ../backports/$1 touch(es) file(s):\n") if ($debug);
			open IN2, "lsdiff -h --strip 1 ../backports/$1 |";
			while (<IN2>) {
				my $f = $_;
				$f =~ s/\n//;
				$files{$f} = 1;
				print ("\t$f\n") if ($debug);
			}
			close IN2;
		}
	}
	close IN;

	return %files;
}

sub sync_patched_files()
{
	my %patches = get_patched_files();
	return if (!%patches);

	foreach my $file (keys %patches) {
		$fhash{$file} = hash_calc("$dir/$file");
		mkpath($path);
		copy("$dir/$file", $file);
	}
	close IN;
}

sub get_patched_hashes()
{
	my %patches = get_patched_files();
	return if (!%patches);

	foreach my $file (keys %patches) {
		$fhash_patched{$file} = hash_calc("$file");
		printf "Hash for patched file $file = %s\n", $fhash_patched{$file} if ($debug);
	}
	close IN;
}

sub remove_deleted()
{
	my $file = $File::Find::name;
	my $mode = (stat($file))[2];

	return if ($mode & S_IFDIR);

	return if ($file =~ /^\./);
	return if ($file =~ /\.mod\.c/);

	if ($file =~ /Makefile$/ || $file =~ /Kconfig$/ || $file =~ /\.[ch]$/ ) {
		if (! -e "$dir/$file") {
			printf "Removing file $file\n" if (!$silent);
			delete $fhash{$file} if exists($fhash{$file});
			delete $fhash_patched{$file} if exists($fhash_patched{$file});
			unlink $file;
			return;
		}
	}
}

sub parse_dir()
{
	my $file = $File::Find::name;
	my $mode = (stat($file))[2];

	return if ($mode & S_IFDIR);

	$file =~ s,^($dir/),,;

	return if ($file =~ /^\./);
	return if ($file =~ /\.mod\.c/);

	if ($file =~ /Makefile$/ || $file =~ /Kconfig$/ || $file =~ /\.[ch]$/ ) {
		sync_files $file;
		return;
	}

	printf "Skipping bogus file $file\n" if ($debug);
}

sub sync_dirs($)
{
	my $subdir = shift;

	print "sync dir: $subdir\n" if (!$silent);
	find({wanted => \&parse_dir, no_chdir => 1}, "$dir/$subdir");
	find({wanted => \&remove_deleted, no_chdir => 1}, "$subdir");
}

sub sync_all()
{
	foreach my $val (keys %files) {
		print "sync file: $val\n" if (!$silent);
		sync_files($val);
	}
	foreach my $val (keys %dirs) {
		sync_dirs($val);
	}
}

# Main

if (!$dir) {
	read_ctlfile();
	die "Please provide a directory to use" if !($path);
	$dir = $path;

	printf "Syncing with dir $dir\n";
} else {
	read_ctlfile();
}

if ($path ne $dir) {
	$path = $dir;
	%fhash = ();
}

$patches_applied = 1 if (-e $patchfile);

if ($get_patched && $patches_applied) {
	get_patched_hashes();
} else {
	get_file_dir_names();
	sync_all();

	if ($sync_patched) {
		sync_patched_files();
		unlink $patchfile;
	}
}

write_ctlfile();
system "git --git-dir $dir/.git log --pretty=oneline HEAD^1^1^1^1^1^1^1^1..HEAD >git_log"

Privacy Policy