boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

012-unlink.c (1161B)


      1 /* unlink -> boot2-syscall.c::unlink -> P1pp sys_unlink (= unlinkat
      2  * with AT_FDCWD). Drives the syscall by creating a file, removing it,
      3  * then re-opening it for read and asserting fopen now returns NULL.
      4  * The first fopen("w") must succeed, the second fopen("r") after
      5  * unlink must fail — anything else means the path either never
      6  * existed or the unlink no-oped. */
      7 typedef long FILE;
      8 typedef long ssize_t;
      9 typedef unsigned long size_t;
     10 
     11 extern FILE   *fopen  (char const *path, char const *mode);
     12 extern int     fclose (FILE *stream);
     13 extern int     fputs  (char const *s, FILE *stream);
     14 extern int     unlink (char const *path);
     15 extern ssize_t write  (int fd, void const *buf, size_t n);
     16 
     17 int
     18 main (void)
     19 {
     20   char const *path = "/tmp/boot2-cclibc-12";
     21 
     22   FILE *w = fopen (path, "w");
     23   if (!w)
     24     {
     25       write (1, "fopen-w-fail\n", 13);
     26       return 1;
     27     }
     28   fputs ("x\n", w);
     29   fclose (w);
     30 
     31   if (unlink (path))
     32     {
     33       write (1, "unlink-fail\n", 12);
     34       return 2;
     35     }
     36 
     37   FILE *r = fopen (path, "r");
     38   if (r)
     39     {
     40       write (1, "still-there\n", 12);
     41       fclose (r);
     42       return 3;
     43     }
     44   write (1, "gone\n", 5);
     45   return 0;
     46 }