Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrub link and remove fix: Scrub and remove both link and target. #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/scrub.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ static int scrub_object(char *filename, const struct opt_struct *opt,
{
bool havesig = false;
int errcount = 0;
char *absolute_path; /* store absolute path if symlink */

absolute_path = is_symlink(filename); /* get absolute path. If NULL not a symlink */
switch (filetype(filename)) {
case FILE_NOEXIST:
fprintf(stderr, "%s: %s does not exist\n", prog, filename);
Expand Down Expand Up @@ -320,7 +322,7 @@ static int scrub_object(char *filename, const struct opt_struct *opt,
fprintf(stderr, "%s: %s already scrubbed? (-f to force)\n",
prog, filename);
errcount++;
} else if (is_symlink(filename) && opt->nofollow) {
} else if (absolute_path && opt->nofollow) {
fprintf(stderr, "%s: skipping symlink %s because --no-link (-L) option was set\n",
prog, filename);
errcount++;
Expand All @@ -334,7 +336,7 @@ static int scrub_object(char *filename, const struct opt_struct *opt,
}
break;
case FILE_REGULAR:
if (is_symlink(filename) && opt->nofollow) {
if (absolute_path && opt->nofollow) { /* symlink, don't remove target */
if (opt->remove && !noexec) {
if (dryrun) {
printf("%s: (dryrun) unlink %s\n", prog, filename);
Expand Down Expand Up @@ -381,23 +383,43 @@ static int scrub_object(char *filename, const struct opt_struct *opt,
#endif
if (opt->dirent) {
if (dryrun) {
printf("%s: (dryrun) scrub dirent %s\n",
prog, filename);
if (absolute_path) {
printf("%s: (dryrun) scrub dirent target %s\n",
prog, absolute_path);
} else {
printf("%s: (dryrun) scrub dirent %s\n",
prog, filename);
}
} else {
scrub_dirent(filename, opt);
if (absolute_path) {
scrub_dirent(absolute_path, opt);
} else {
scrub_dirent(filename, opt);
}
}
}
if (opt->remove) {
char *rmfile = opt->dirent ? opt->dirent : filename;
if (dryrun) {
printf("%s: (dryrun) unlink %s\n", prog, rmfile);
if (absolute_path) { /* link target */
printf("%s: (dryrun) unlink target %s\n", prog, absolute_path);
}
} else {
printf("%s: unlinking %s\n", prog, rmfile);
if (unlink(rmfile) != 0) {
fprintf(stderr, "%s: unlink %s: %s\n", prog, rmfile,
strerror(errno));
exit(1);
}
if (absolute_path) { /* link target */
if (unlink(filename) != 0) {
printf("%s: unlinking target %s\n", prog, absolute_path);
fprintf(stderr, "%s: unlink %s: %s\n", prog, absolute_path,
strerror(errno));
exit(1);
}
}
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,23 @@ write_all(int fd, const unsigned char *buf, int count)
}

/* Indicates whether the file represented by 'path' is a symlink.
* and if so, return a pointer to the realpath.
*/
int
char *
is_symlink(char *path)
{
struct stat sb;
return lstat(path, &sb) == 0 && S_ISLNK(sb.st_mode);
int error;
char *resolved_path;

if ((error = lstat(path, &sb)) != 0)
return NULL; /* not a link */
if ((resolved_path=realpath(path,NULL)) == NULL)
return NULL; /* some error in determining absolute path */
if (!S_ISLNK(sb.st_mode)) /* if it's not a link */
return NULL;

return resolved_path;
}

/* Return the type of file represented by 'path'.
Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ typedef enum { UP, DOWN } round_t;

int read_all(int fd, unsigned char *buf, int count);
int write_all(int fd, const unsigned char *buf, int count);
int is_symlink(char *path);
char * is_symlink(char *path);
filetype_t filetype(char *path);
off_t blkalign(off_t offset, int blocksize, round_t rtype);
void * alloc_buffer(int bufsize);
Expand Down