File: default-direct.lua

package info (click to toggle)
lsyncd 2.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 732 kB
  • ctags: 363
  • sloc: ansic: 2,549; sh: 1,134; makefile: 48
file content (207 lines) | stat: -rw-r--r-- 4,330 bytes parent folder | download | duplicates (2)
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
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- default-direct.lua
--
--    Keeps two directories with /bin/cp, /bin/rm and /bin/mv in sync.
--    Startup still uses rsync tough.
--
--    A (Layer 1) configuration.
--
-- Note:
--    this is infact just a configuration using Layer 1 configuration
--    like any other. It only gets compiled into the binary by default.
--
--    You can simply use a modified one, by copying everything into a
--    config file of yours and name it differently.
--
-- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <axkibe@gmail.com>
--
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if not default then
	error('default not loaded')
end

if not default.rsync then
	error('default-direct (currently) needs default.rsync loaded')
end

if default.direct then
	error('default-direct already loaded')
end

local direct = { }

default.direct = direct


--
-- known configuration parameters
--
direct.checkgauge = {
	--
	-- inherits rsync config params
	--
	default.rsync.checkgauge,

	rsyncExitCodes  =  true,
	onMove          =  true,
}


--
-- Spawns rsync for a list of events
--
direct.action = function(inlet)
	-- gets all events ready for syncing
	local event, event2 = inlet.getEvent()
	local config = inlet.getConfig()

	if event.etype == 'Create' then
		if event.isdir then
			spawn(
				event,
				'/bin/mkdir',
				event.targetPath
			)
		else
			-- 'cp -t', not supported on OSX
			spawn(
				event,
				'/bin/cp',
				event.sourcePath,
				event.targetPathdir
			)
		end
	elseif event.etype == 'Modify' then
		if event.isdir then
			error("Do not know how to handle 'Modify' on dirs")
		end
		spawn(event,
			'/bin/cp',
			event.sourcePath,
			event.targetPathdir
		)
	elseif event.etype == 'Delete' then

		if
			config.delete ~= true and
			config.delete ~= 'running'
		then
			inlet.discardEvent(event)
			return
		end

		local tp = event.targetPath

		-- extra security check
		if tp == '' or tp == '/' or not tp then
			error('Refusing to erase your harddisk!')
		end

		spawn(event, '/bin/rm', '-rf', tp)

	elseif event.etype == 'Move' then
		local tp = event.targetPath

		-- extra security check
		if tp == '' or tp == '/' or not tp then
			error('Refusing to erase your harddisk!')
		end

		local command = '/bin/mv $1 $2 || /bin/rm -rf $1'

		if
			config.delete ~= true and
			config.delete ~= 'running'
		then
			command = '/bin/mv $1 $2'
		end

		spawnShell(
			event,
			command,
			event.targetPath,
			event2.targetPath
		)

	else
		log('Warn', 'ignored an event of type "',event.etype, '"')
		inlet.discardEvent(event)
	end
end

--
-- Called when collecting a finished child process
--
direct.collect = function(agent, exitcode)

	local config = agent.config

	if not agent.isList and agent.etype == 'Init' then
		local rc = config.rsyncExitCodes[exitcode]
		if rc == 'ok' then
			log('Normal', 'Startup of "',agent.source,'" finished: ', exitcode)
		elseif rc == 'again' then
			if settings.insist then
				log('Normal', 'Retrying startup of "',agent.source,'": ', exitcode)
			else
				log('Error', 'Temporary or permanent failure on startup of "',
				agent.source, '". Terminating since "insist" is not set.');
				terminate(-1) -- ERRNO
			end
		elseif rc == 'die' then
			log('Error', 'Failure on startup of "',agent.source,'": ', exitcode)
		else
			log('Error', 'Unknown exitcode on startup of "', agent.source,': "',exitcode)
			rc = 'die'
		end
		return rc
	end

	-- everything else is just as it is,
	-- there is no network to retry something.
	return
end

--
-- Spawns the recursive startup sync
-- (currently) identical to default rsync.
--
direct.init = default.rsync.init

--
-- Checks the configuration.
--
direct.prepare = function( config, level )

	default.rsync.prepare( config, level + 1 )

end

--
-- Default delay is very short.
--
direct.delay = 1

--
-- Let the core not split move events.
--
direct.onMove = true

--
-- Rsync configuration for startup.
--
direct.rsync = default.rsync.rsync
direct.rsyncExitCodes = default.rsyncExitCodes

--
-- By default do deletes.
--
direct.delete = true

--
-- On many system multiple disk operations just rather slow down
-- than speed up.

direct.maxProcesses = 1