summaryrefslogtreecommitdiff
path: root/rdiff-backup/rdiff_backup/increment.py
blob: 4ed6a39df09b38ec1b83d4a9fe826e6e014e7693 (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
execfile("destructive_stepping.py")

#######################################################################
#
# increment - Provides Inc class, which writes increment files
#
# This code is what writes files ending in .diff, .snapshot, etc.
#

class Inc:
	"""Class containing increment functions"""
	def Increment_action(new, mirror, incpref):
		"""Main file incrementing function, returns RobustAction

		new is the file on the active partition,
		mirror is the mirrored file from the last backup,
		incpref is the prefix of the increment file.

		This function basically moves mirror -> incpref.

		"""
		if not (new and new.lstat() or mirror.lstat()):
			return Robust.null_action # Files deleted in meantime, do nothing

		Log("Incrementing mirror file " + mirror.path, 5)
		if ((new and new.isdir()) or mirror.isdir()) and not incpref.isdir():
			incpref.mkdir()

		if not mirror.lstat(): return Inc.makemissing_action(incpref)
		elif mirror.isdir(): return Inc.makedir_action(mirror, incpref)
		elif new.isreg() and mirror.isreg():
			return Inc.makediff_action(new, mirror, incpref)
		else: return Inc.makesnapshot_action(mirror, incpref)

	def Increment(new, mirror, incpref):
		Inc.Increment_action(new, mirror, incpref).execute()

	def makemissing_action(incpref):
		"""Signify that mirror file was missing"""
		return RobustAction(lambda: None,
							Inc.get_inc_ext(incpref, "missing").touch,
							lambda exp: None)
		
	def makesnapshot_action(mirror, incpref):
		"""Copy mirror to incfile, since new is quite different"""
		snapshotrp = Inc.get_inc_ext(incpref, "snapshot")
		return Robust.copy_with_attribs_action(mirror, snapshotrp)

	def makediff_action(new, mirror, incpref):
		"""Make incfile which is a diff new -> mirror"""
		diff = Inc.get_inc_ext(incpref, "diff")
		return Robust.chain([Rdiff.write_delta_action(new, mirror, diff),
							 Robust.copy_attribs_action(mirror, diff)])

	def makedir_action(mirrordir, incpref):
		"""Make file indicating directory mirrordir has changed"""
		dirsign = Inc.get_inc_ext(incpref, "dir")
		def final():
			dirsign.touch()
			RPath.copy_attribs(mirrordir, dirsign)
		return RobustAction(lambda: None, final, dirsign.delete)

	def get_inc_ext(rp, typestr):
		"""Return RPath/DSRPath like rp but with inc/time extension

		If the file exists, then probably a previous backup has been
		aborted.  We then keep asking FindTime to get a time later
		than the one that already has an inc file.

		"""
		def get_newinc(timestr):
			"""Get new increment rp with given time suffix"""
			addtostr = lambda s: "%s.%s.%s" % (s, timestr, typestr)
			if rp.index:
				return rp.__class__(rp.conn, rp.base, rp.index[:-1] +
									(addtostr(rp.index[-1]),))
			else: return rp.__class__(rp.conn, addtostr(rp.base), rp.index)

		inctime = 0
		while 1:
			inctime = Resume.FindTime(rp.index, inctime)
			incrp = get_newinc(Time.timetostring(inctime))
			if not incrp.lstat(): return incrp

	def make_patch_increment_ITR(inc_rpath, initial_state = None):
		"""Return IterTreeReducer that patches and increments

		This has to be an ITR because directories that have files in
		them changed are flagged with an increment marker.  There are
		four possibilities as to the order:

		1.  Normal file -> Normal file:  right away
		2.  Directory -> Directory:  wait until files in the directory
		    are processed, as we won't know whether to add a marker
		    until the end.
		3.  Normal file -> Directory:  right away, so later files will
		    have a directory to go into.
		4.  Directory -> Normal file:  Wait until the end, so we can
		    process all the files in the directory.

		"""
		def base_init(indexed_tuple):
			"""Patch if appropriate, return (a,b) tuple

			a is true if found directory and thus didn't take action
			
			if a is false, b is true if some changes were made

			if a is true, b is the rp of a temporary file used to hold
			the diff_rorp's data (for dir -> normal file change), and
			false if none was necessary.

			"""
			diff_rorp, dsrp = indexed_tuple
			incpref = inc_rpath.new_index(indexed_tuple.index)
			if dsrp.isdir(): return init_dir(dsrp, diff_rorp, incpref)
			else: return init_non_dir(dsrp, diff_rorp, incpref)

		def init_dir(dsrp, diff_rorp, incpref):
			"""Initial processing of a directory

			Make the corresponding directory right away, but wait
			until the end to write the replacement.  However, if the
			diff_rorp contains data, we must write it locally before
			continuing, or else that data will be lost in the stream.

			"""
			if not (incpref.lstat() and incpref.isdir()): incpref.mkdir()
			if diff_rorp and diff_rorp.isreg() and diff_rorp.file:
				tf = TempFileManager.new(dsrp)
				RPathStatic.copy_with_attribs(diff_rorp, tf)
				tf.set_attached_filetype(diff_rorp.get_attached_filetype())
				return (1, tf)
			else: return (1, None)

		def init_non_dir(dsrp, diff_rorp, incpref):
			"""Initial processing of non-directory

			If a reverse diff is called for it is generated by apply
			the forwards diff first on a temporary file.

			"""
			if diff_rorp:
				if dsrp.isreg() and diff_rorp.isreg():
					tf = TempFileManager.new(dsrp)
					def init_thunk():
						Rdiff.patch_with_attribs_action(dsrp, diff_rorp,
														tf).execute()
						Inc.Increment_action(tf, dsrp, incpref).execute()
					Robust.make_tf_robustaction(init_thunk, (tf,),
												(dsrp,)).execute()
				else:
					Robust.chain([Inc.Increment_action(diff_rorp, dsrp,
													   incpref),
								  RORPIter.patchonce_action(
						             None, dsrp, diff_rorp)]).execute()
				return (None, 1)
			return (None, None)

		def base_final(base_tuple, base_init_tuple, changed):
			"""Patch directory if not done, return true iff made change"""
			if base_init_tuple[0]: # was directory
				diff_rorp, dsrp = base_tuple
				if changed or diff_rorp:
					if base_init_tuple[1]: diff_rorp = base_init_tuple[1]
					Inc.Increment(diff_rorp, dsrp,
								  inc_rpath.new_index(base_tuple.index))
					if diff_rorp:
						RORPIter.patchonce_action(None, dsrp,
												  diff_rorp).execute()
						if isinstance(diff_rorp, TempFile): diff_rorp.delete()
					return 1
				return None
			else: # changed iff base_init_tuple says it was
				return base_init_tuple[1]

		return IterTreeReducer(base_init, lambda x,y: x or y, None,
							   base_final, initial_state)

MakeStatic(Inc)