diff: fix incorrectly displaying files as duplicates

When diff hits certain access errors, function diffreg() shows the error
message, and then returns to the calling function, which calls
print_status() with the return value.

However, in these cases, the return value isn't changed from the initial
default value of D_SAME.

Normally, print_status() with a value of D_SAME does nothing, so this
works out ok, however, if the "-s" flag is set, a message is displayed
showing identicality:

case D_SAME:
                if (sflag)
                        printf("Files %s%s and %s%s are identical\n",                                                                                                       path1, entry, path2, entry);
                break;

This then produces such results as:

% diff  -s /COPYRIGHT /var/run/rpcbind.sock
diff: /var/run/rpcbind.sock: Operation not supported
Files /COPYRIGHT and /var/run/rpcbind.sock are identical

% diff  -s /COPYRIGHT /etc/master.passwd
diff: /etc/master.passwd: Permission denied
Files /COPYRIGHT and /etc/master.passwd are identical

Create a D_ERROR status which is returned in such cases, and
print_status() then deals with that status seperately from D_SAME

PR:		252614
MFC after:	1 week
This commit is contained in:
Jamie Landeg-Jones 2021-01-25 18:42:26 +01:00 committed by Baptiste Daroussin
parent 13860e71eb
commit fefb3c46a8
3 changed files with 8 additions and 0 deletions

View File

@ -511,6 +511,8 @@ print_status(int val, char *path1, char *path2, const char *entry)
printf("File %s%s is not a regular file or directory and was skipped\n",
path2, entry);
break;
case D_ERROR:
break;
}
}

View File

@ -83,6 +83,7 @@
#define D_MISMATCH2 4 /* path1 was a file, path2 a dir */
#define D_SKIPPED1 5 /* path1 was a special file */
#define D_SKIPPED2 6 /* path2 was a special file */
#define D_ERROR 7 /* A file access error occurred */
struct excludes {
char *pattern;

View File

@ -299,6 +299,7 @@ diffreg(char *file1, char *file2, int flags, int capsicum)
if ((f1 = opentemp(file1)) == NULL ||
fstat(fileno(f1), &stb1) == -1) {
warn("%s", file1);
rval = D_ERROR;
status |= 2;
goto closem;
}
@ -309,6 +310,7 @@ diffreg(char *file1, char *file2, int flags, int capsicum)
}
if (f1 == NULL) {
warn("%s", file1);
rval = D_ERROR;
status |= 2;
goto closem;
}
@ -320,6 +322,7 @@ diffreg(char *file1, char *file2, int flags, int capsicum)
if ((f2 = opentemp(file2)) == NULL ||
fstat(fileno(f2), &stb2) == -1) {
warn("%s", file2);
rval = D_ERROR;
status |= 2;
goto closem;
}
@ -330,6 +333,7 @@ diffreg(char *file1, char *file2, int flags, int capsicum)
}
if (f2 == NULL) {
warn("%s", file2);
rval = D_ERROR;
status |= 2;
goto closem;
}
@ -365,6 +369,7 @@ diffreg(char *file1, char *file2, int flags, int capsicum)
break;
default:
/* error */
rval = D_ERROR;
status |= 2;
goto closem;
}