Skip to content

Commit 519a682

Browse files
committed
Add phase field to file_status JSON output with correct lifecycle emission
1 parent d80724b commit 519a682

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

src/borg/archive.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,13 @@ def process_pipe(self, *, path, cache, fd, mode, user=None, group=None):
14081408
item.uid = uid
14091409
if gid is not None:
14101410
item.gid = gid
1411-
self.process_file_chunks(item, cache, self.stats, self.show_progress, backup_io_iter(self.chunker.chunkify(fd)))
1411+
self.print_file_status(None, path, phase="start")
1412+
try:
1413+
self.process_file_chunks(
1414+
item, cache, self.stats, self.show_progress, backup_io_iter(self.chunker.chunkify(fd))
1415+
)
1416+
finally:
1417+
self.print_file_status(None, path, phase="end")
14121418
item.get_size(memorize=True)
14131419
self.stats.nfiles += 1
14141420
self.add_item(item, stats=self.stats)
@@ -1475,17 +1481,21 @@ def process_file(self, *, path, parent_fd, name, st, cache, flags=flags_normal,
14751481
# Only chunkify the file if needed
14761482
changed_while_backup = False
14771483
if "chunks" not in item:
1478-
start_reading = time.time_ns()
1479-
with backup_io("read"):
1480-
self.process_file_chunks(
1481-
item,
1482-
cache,
1483-
self.stats,
1484-
self.show_progress,
1485-
backup_io_iter(self.chunker.chunkify(None, fd)),
1486-
)
1487-
self.stats.chunking_time = self.chunker.chunking_time
1488-
end_reading = time.time_ns()
1484+
self.print_file_status(None, path, phase="start")
1485+
try:
1486+
start_reading = time.time_ns()
1487+
with backup_io("read"):
1488+
self.process_file_chunks(
1489+
item,
1490+
cache,
1491+
self.stats,
1492+
self.show_progress,
1493+
backup_io_iter(self.chunker.chunkify(None, fd)),
1494+
)
1495+
self.stats.chunking_time = self.chunker.chunking_time
1496+
end_reading = time.time_ns()
1497+
finally:
1498+
self.print_file_status(None, path, phase="end")
14891499
with backup_io("fstat2"):
14901500
st2 = os.fstat(fd)
14911501
if self.files_changed == "disabled" or is_special_file:

src/borg/archiver/__init__.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,33 +156,31 @@ def print_warning_instance(self, warning):
156156
self.print_warning(msg, *args, wc=wc, wt="curly", msgid=msgid)
157157

158158
def print_file_status(self, status, path, *, phase=None, error=None):
159-
# START lifecycle event (JSON only)
159+
# START lifecycle event
160160
if self.output_list and self.log_json and phase == "start" and status is None:
161161
json_data = {"type": "file_status", "phase": "start"}
162162
json_data |= text_to_json("path", path)
163163
if error is not None:
164164
json_data["error"] = error
165165
print(json.dumps(json_data), file=sys.stderr)
166166
return
167-
# END lifecycle event (JSON only)
167+
168+
# END lifecycle event
168169
if self.output_list and self.log_json and phase == "end" and status is None:
169170
json_data = {"type": "file_status", "phase": "end"}
170171
json_data |= text_to_json("path", path)
171172
if error is not None:
172173
json_data["error"] = error
173174
print(json.dumps(json_data), file=sys.stderr)
174175
return
175-
# regular status event (A, M, U, -, d, s, etc.)
176+
177+
# if we get called with status == None, the final file status was already printed
176178
if self.output_list and status is not None and (self.output_filter is None or status in self.output_filter):
177179
if self.log_json:
178180
json_data = {"type": "file_status", "status": status}
179181
json_data |= text_to_json("path", path)
180-
if phase is not None:
181-
json_data["phase"] = phase
182-
if error is not None:
183-
json_data["error"] = error
184182
print(json.dumps(json_data), file=sys.stderr)
185-
elif not self.log_json and status is not None:
183+
else:
186184
logging.getLogger("borg.output.list").info("%1s %s", status, remove_surrogates(path))
187185

188186
def preprocess_args(self, args):

0 commit comments

Comments
 (0)