File: filter-rsync.lua

package info (click to toggle)
lsyncd 2.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 484 kB
  • sloc: ansic: 2,640; sh: 81; makefile: 11
file content (146 lines) | stat: -rw-r--r-- 2,728 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
require( 'posix' )
dofile( 'tests/testlib.lua' )

cwriteln( '****************************************************************' )
cwriteln( ' Testing filters (rsync)' )
cwriteln( '****************************************************************' )

local tdir, srcdir, trgdir = mktemps( )
local logfile = tdir .. "log"
local cfgfile = tdir .. "config.lua"
local range = 5
local log = {"-log", "all"}

writefile(cfgfile, [[
settings {
	logfile = ']]..logfile..[[',
	nodaemon = true,
}

sync {
	default.rsync,
	source = ']]..srcdir..[[',
	target = ']]..trgdir..[[',
	delay = 3,
	filter = {
		'- /ab**',
		'+ /a**',
		'- /**',
	},
}]])

-- writes all files
local function writefiles
( )
	writefile( srcdir .. 'abc', 'abc' )
	writefile( srcdir .. 'acc', 'acc' )
	writefile( srcdir .. 'baa', 'baa' )
	posix.mkdir( srcdir .. 'abx' )
	writefile( srcdir .. 'abx/a', 'abxa' )
	posix.mkdir( srcdir .. 'acx' )
	writefile( srcdir .. 'acx/x', 'acxx' )
end

--
-- Tests if the filename exists
-- fails if this is different to expect.
--
local function testfile
(
	filename,
	expect
)
	local stat, err = posix.stat( filename )

	if stat and not expect
	then
		cwriteln( 'failure: ', filename, ' should be filtered')

		os.exit( 1 )
	end

	if not stat and expect
	then
		cwriteln( 'failure: ', filename, ' should not be filtered' )
		os.exit( 1 )
	end
end

-- test all files
local function testfiles
( )
	testfile( trgdir .. 'abc', false )
	testfile( trgdir .. 'acc', true )
	testfile( trgdir .. 'baa', false )
	testfile( trgdir .. 'abx/a', false )
	testfile( trgdir .. 'acx/x', true )
end


cwriteln( 'testing startup filters' )

writefiles( )

cwriteln( 'starting Lsyncd' )

local pid = spawn( './lsyncd', cfgfile, '-log', 'all' )

cwriteln( 'waiting for Lsyncd to start' )

posix.sleep( 3 )

cwriteln( 'testing filters after startup' )

testfiles( )

cwriteln( 'ok, removing sources' )

if srcdir:sub( 1,4 ) ~= '/tmp'
then
	-- just to make sure before rm -rf
	cwriteln( 'exit before drama, srcdir is "', srcdir, '"' )

	os.exit( 1 )
end

os.execute( 'rm -rf '..srcdir..'/*' )

cwriteln( 'waiting for Lsyncd to remove destination' )

posix.sleep( 5 )

_, result, code = os.execute( 'diff -urN ' .. srcdir .. ' ' .. trgdir )

if result ~= 'exit' or code ~= 0
then
	cwriteln( 'fail, target directory not empty!' )

	os.exit( 1 )
end

cwriteln( 'writing files after startup' )

writefiles( )

cwriteln( 'waiting for Lsyncd to transmit changes' )

posix.sleep( 5 )

testfiles( )

cwriteln( 'killing started Lsyncd' )

posix.kill( pid )
local _, exitmsg, exitcode = posix.wait( lpid )

cwriteln( 'Exitcode of Lsyncd = ', exitmsg, ' ', exitcode );

if exitcode == 143
then
	cwriteln( 'OK' )
	os.exit( 0 )
else
	os.exit( 1 )
end

-- TODO remove temp