summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ctm/mkCTM/mkCTM
blob: 64725802a3c1c20a0d44eccfca92eafac2360572 (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
#!/usr/local/bin/tclsh

set CTMignoreCVS 0
set CTMapply 1

source $argv

set tmp $CTMtmp
set dd  $CTMdest
set d1  $CTMcopy
set d2  $CTMref
set foo $CTMdate
set foo $CTMprefix
set foo $CTMname

exec rm -f $tmp.*

set f1 [open "| ./ctm_scan $d1"]
set f2 [open "| ./ctm_scan $d2"]

set fo_del   [open $tmp.del w]
set fo_rmdir [open $tmp.rmdir w]
set fo_mkdir [open $tmp.mkdir w]
set fo_files [open $tmp.files w]
set changes 0

####
# Find CTM#
for {set i 0} {1} {incr i} {
    if {[file exists [format "%s/$CTMname.%04d" $dd $i]]} continue
    if {[file exists [format "%s/$CTMname.%04d.gz" $dd $i]]} continue
    break
}
set CTMnbr $i

puts "Doing CTMname $CTMname CTMnbr $CTMnbr CTMdate $CTMdate"

#####
# Type Name Mode User Group Barf Size Hash

proc CTMadd {t n m u g b s h} {
    global fo_files fo_mkdir changes d2
    puts stderr "A $b  $t  $n"
    if {$t == "d"} {
	puts $fo_mkdir "CTMDM $n $u $g $m"
	incr changes
	return
    }
    puts $fo_files "CTMFM $n $u $g $m $h $s"
    flush $fo_files
    exec cat $d2/$n >@ $fo_files
    puts $fo_files ""
    incr changes
    return
}
proc CTMdel {t n m u g b s h} {
    global fo_del fo_rmdir changes
    puts stderr "D $b  $t  $n"
    if {$t == "d"} {
	puts $fo_rmdir "CTMDR $n"
	incr changes
	return
    }
    puts $fo_del "CTMFR $n $h"
    incr changes
    return
}
proc CTMchg {t1 n1 m1 u1 g1 b1 s1 h1 t2 n2 m2 u2 g2 b2 s2 h2} {
    global fo_files d2 d1 changes
    if {$t1 == "d" && $t2 == "d"} {
	return
    }
    if {$t1 == "d" || $t2 == "d"} {
	CTMdel $t1 $n1 $m1 $u1 $g1 $b1 $s1 $h1
	CTMadd $t2 $n2 $m2 $u2 $g2 $b2 $s2 $h2
	return
    }
    if {"x$h1" == "x$h2" && $s1 == $s2} {
	return
        puts stderr "M $b1$b2 $t1$t2 $n1"
	puts $fo_files "CTMFA $n2 $u2 $g2 $m2 $h2"
	incr changes
	return
    }
    if {$b1 == "0" || $b2 == "0"} {
	puts stderr "E $b1$b2 $t1$t2 $n1"
	set i [catch "exec diff -e $d1/$n1 $d2/$n2 > tmp" j]
	set s [file size tmp]
	if {$s < $s2} {
	    puts $fo_files "CTMFE $n1 $u2 $g2 $m2 $h1 $h2 $s"
	    flush $fo_files
	    exec cat tmp >@ $fo_files
	    puts $fo_files ""
	    incr changes
	    return
	}
    }
    puts stderr "R $b1$b2 $t1$t2 $n1"
    puts $fo_files "CTMFS $n2 $u2 $g2 $m2 $h1 $h2 $s2"
    flush $fo_files
    exec cat $d2/$n2 >@ $fo_files
    puts $fo_files ""
    incr changes
    return
}
#####
set l1 ""
set l2 ""

while 1 {

    if {$l1 == ""} {gets $f1 l1}

    if {$l2 == ""} {gets $f2 l2}
    
    if {$l1 == "" && $l2 == ""} break

    set n1 [lindex $l1 1]
    set n2 [lindex $l2 1]

    if {$l1 == $l2}  { set l1 "" ; set l2 "" ; continue }

    if {$CTMignoreCVS } {
	if {[regexp {/CVS/} $l1]} {set l1 ""; continue }
	if {[regexp {/CVS/} $l2]} {set l2 ""; continue }
    }

    if {$l1 == "" }   { eval CTMadd $l2 ; set l2 "" ; continue }

    if {$l2 == "" }   { eval CTMdel $l1 ; set l1 "" ; continue }

    if {$n1 < $n2 }   { eval CTMdel $l1 ; set l1 "" ; continue }
    
    if {$n1 > $n2 }   { eval CTMadd $l2 ; set l2 "" ; continue }

    if {$n1 == $n2}  { eval CTMchg $l1 $l2 ; set l1 "" ; set l2 "" ; continue }
}

close $fo_del
close $fo_rmdir
close $fo_mkdir
close $fo_files

exec echo CTM_BEGIN 2.0 $CTMname $CTMnbr $CTMdate $CTMprefix > $tmp.begin
exec echo -n "CTM_END " >> $tmp.end
set m [exec cat $tmp.begin $tmp.del $tmp.rmdir $tmp.mkdir $tmp.files $tmp.end | /sbin/md5]
exec echo "$m" >> $tmp.end

if {!$changes} {
    puts "no changes"
    exec sh -c "rm -f $tmp.*"
    exit 0
}

set  nm [format "%s/%s.%04d" $dd $CTMname $CTMnbr]
exec cat $tmp.begin $tmp.del $tmp.rmdir $tmp.mkdir $tmp.files $tmp.end | gzip -9 -v > ${nm}.gz 
exec sh -c "rm -f $tmp.*"
if {$CTMapply} {
	exec sh -e -x -c "cd $CTMcopy ; /root/CTM/ctm -v -v -v ${nm}.gz" >&@ stdout
}
OpenPOWER on IntegriCloud