aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

rm.c (1091B)


      1 /* see LICENSE file for copyright and license details */
      2 #include <sys/stat.h>
      3 
      4 #include <errno.h>
      5 #include <fcntl.h>
      6 #include <stdio.h>
      7 #include <unistd.h>
      8 
      9 #include "../fs.h"
     10 #include "../util.h"
     11 
     12 int rm_status = 0;
     13 
     14 void
     15 rm(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
     16 {
     17   int quiet, ask, write, flags, ignore;
     18 
     19   (void)data;
     20 
     21   ignore = r->flags & IGNORE;
     22   quiet  = r->flags & SILENT;
     23   ask    = r->flags & CONFIRM;
     24   write  = S_ISLNK(st->st_mode) || faccessat(dirfd, name, W_OK, 0) == 0;
     25   flags  = 0;
     26 
     27   if (S_ISDIR(st->st_mode) && r->maxdepth) {
     28     errno = EISDIR;
     29     goto err;
     30   }
     31 
     32   if (!quiet && !(r->flags & QUIET) && ((!write && isatty(0)) || ask)) {
     33     if (!confirm("remove file '%s'? ", r->path))
     34       return;
     35   }
     36 
     37   if (S_ISDIR(st->st_mode)) {
     38     flags = AT_REMOVEDIR;
     39     recurse(dirfd, name, NULL, r);
     40   }
     41 
     42   if (unlinkat(dirfd, name, flags) < 0)
     43     goto err;
     44   return;
     45 
     46 err:
     47   if (!ignore) {
     48     if (!r->err)
     49       r->err = errno;
     50     if (!(r->flags & QUIET))
     51       weprintf("cannot remove '%s':", r->path);
     52     rm_status = 1;
     53   }
     54 }