AmendHub

Download (as of this amendment):

jcs

/

amend

/

amendments

/

189

repo: Fix error paths, handle not-deleted but zero-length files

bile can't store zero-length files so just delete the TEXT object
and treat it as deleted, but any future diff will just recreate the
file as zero-length. Probably not ideal but this seems like a rare
case (or someone adding a file that only has a resource fork which
we can't handle anyway).

jcs made amendment 189 18 days ago
--- repo.c Sat Oct 3 22:26:10 2026 +++ repo.c Sun Oct 4 10:52:45 2026 @@ -744,7 +744,8 @@ repo_checkout_file(struct repo *repo, struct repo_file error = Create(filename, vrefnum, file->creator, file->type); if (error) { warn("Failed to create file %s: %d", PtoCstr(filename), error); - return -1; + ret = -1; + goto checkout_done; } if (getpath(vrefnum, filename, &filepath, true) != 0) @@ -762,7 +763,6 @@ repo_checkout_file(struct repo *repo, struct repo_file error = Create(filename, vrefnum, file->creator, file->type); if (error && error != dupFNErr) { warn("Failed to create file %s: %d", PtoCstr(filename), error); - xfree(&textob); ret = -1; goto checkout_done; } @@ -1154,21 +1154,30 @@ repo_amend(struct repo *repo, struct diffed_file *diff panic("Failed to get size of file %s: %d", PtoCstr(tfilename), error); - tdata = xmalloc(fsize, 0); - error = FSRead(frefnum, &fsize, tdata); - if (error) - panic("Failed to read %lu of file %s: %d", fsize, - PtoCstr(tfilename), error); - + tdata = NULL; + if (fsize) { + tdata = xmalloc(fsize, 0); + error = FSRead(frefnum, &fsize, tdata); + if (error) + panic("Failed to read %lu of file %s: %d", + fsize, PtoCstr(tfilename), error); + } FSClose(frefnum); } - - size = bile_write(repo->bile, REPO_TEXT_RTYPE, - diffed_files[i].file->id, tdata, fsize); - if (size != fsize) - panic("Failed to write new text file at %s: %d", - PtoCstr(tfilename), bile_error(repo->bile)); - xfree(&tdata); + + if (fsize == 0) { + /* can't bile_write zero data, so consider deleted */ + bile_delete(repo->bile, REPO_TEXT_RTYPE, + diffed_files[i].file->id); + } else { + size = bile_write(repo->bile, REPO_TEXT_RTYPE, + diffed_files[i].file->id, tdata, fsize); + if (size != fsize) + panic("Failed to write new text file at %s: %d", + PtoCstr(tfilename), bile_error(repo->bile)); + } + if (tdata != NULL) + xfree(&tdata); } }