jcs
/amend
/amendments
/80
repo: Limit filename length when creating temporary files
Files can be 31 characters long, so appending " (Amend tmp)" to a
long filename can put us over that limit. Work in a Str31 and put
our "[tmp] " and "[deleted] " labels at the beginning so we don't
end up overwriting the existing file if the filename is already 31
characters long.
Check error result of getpath calls as well.
jcs made amendment 80 over 2 years ago
--- diffreg.c Mon Jun 6 21:51:39 2022
+++ diffreg.c Wed Jun 15 22:07:22 2022
@@ -302,14 +302,14 @@ diffreg(char *file1, char *file2, int flags)
f1 = fopen(file1, "rb");
if (f1 == NULL) {
- warn("%s", file1);
+ warn("failed to fopen %s", file1);
status |= 2;
goto closem;
}
f2 = fopen(file2, "rb");
if (f2 == NULL) {
- warn("%s", file2);
+ warn("failed to fopen %s", file2);
status |= 2;
goto closem;
}
--- repo.c Wed Jun 15 10:45:40 2022
+++ repo.c Wed Jun 15 22:28:04 2022
@@ -714,8 +714,9 @@ repo_sort_commits(struct repo *repo)
short
repo_diff_file(struct repo *repo, struct repo_file *file)
{
- static Str255 fromfilename, tofilename, fromfilepath, tofilepath;
- static Str255 label0, label1;
+ Str31 fromfilename, tofilename;
+ Str255 fromfilepath, tofilepath;
+ Str255 label0, label1;
struct repo_file_attrs attrs;
size_t size;
char *text;
@@ -723,8 +724,8 @@ repo_diff_file(struct repo *repo, struct repo_file *fi
short error, ret, frefnum, tofile_empty = 0;
/* write out old file */
- snprintf((char *)fromfilename, sizeof(fromfilename),
- "%s (%s tmp)", file->filename, PROGRAM_NAME);
+ snprintf((char *)fromfilename, sizeof(fromfilename), "[tmp] %s",
+ file->filename);
CtoPstr(fromfilename);
error = Create(fromfilename, repo->bile->vrefnum, file->creator,
@@ -760,17 +761,22 @@ repo_diff_file(struct repo *repo, struct repo_file *fi
FSClose(frefnum);
- getpath(repo->bile->vrefnum, fromfilename, &fromfilepath, true);
+ if (getpath(repo->bile->vrefnum, fromfilename, &fromfilepath,
+ true) != 0)
+ panic("getpath(%d, %s) failed", repo->bile->vrefnum,
+ PtoCstr(fromfilename));
- memcpy((char *)tofilename, file->filename, sizeof(file->filename));
+ memcpy((char *)tofilename, file->filename, sizeof(tofilename));
CtoPstr(tofilename);
- getpath(repo->bile->vrefnum, tofilename, &tofilepath, true);
+ if (getpath(repo->bile->vrefnum, tofilename, &tofilepath, true) != 0)
+ panic("getpath(%d, %s) failed", repo->bile->vrefnum,
+ PtoCstr(tofilename));
error = repo_get_file_attrs(repo, tofilename, &attrs);
if (error == fnfErr) {
/* file no longer exists, create empty temp file */
snprintf((char *)tofilename, sizeof(tofilename),
- "%s (%s deleted)", file->filename, PROGRAM_NAME);
+ "[deleted] %s", file->filename);
CtoPstr(tofilename);
error = Create(tofilename, repo->bile->vrefnum, file->creator,
@@ -779,7 +785,10 @@ repo_diff_file(struct repo *repo, struct repo_file *fi
panic("Failed to create file %s: %d", PtoCstr(tofilename),
error);
- getpath(repo->bile->vrefnum, tofilename, &tofilepath, true);
+ if (getpath(repo->bile->vrefnum, tofilename, &tofilepath,
+ true) != 0)
+ panic("getpath(%d, %s) failed", repo->bile->vrefnum,
+ PtoCstr(tofilename));
tofile_empty = 1;
} else if (error)
panic("Failed to get info for %s", PtoCstr(tofilename));