Project

General

Profile

Bug #12906 » rename-w.c

Mark Harris, 2020-06-28 12:39 AM

 
1
#include <sys/types.h>
2
#include <errno.h>
3
#include <fcntl.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <unistd.h>
7

    
8
int
9
main(int argc, char **argv)
10
{
11
    /* look for w subdirectory and move it to cwd continually */
12
    int i_fd = AT_FDCWD;
13
    for (;;)
14
    {
15
        int itmp_fd = openat(i_fd, "i", O_RDONLY | O_DIRECTORY);
16
        if (itmp_fd >= 0)
17
        {
18
            if (i_fd != AT_FDCWD) close(i_fd);
19
            i_fd = itmp_fd;
20
        }
21
        else if (errno != ENOENT)
22
        {
23
            perror("Failed to open intermediate directory");
24
            return 1;
25
        }
26
        else
27
        {
28
            if (i_fd != AT_FDCWD)
29
            {
30
                int tries = 1;
31
                for (;;)
32
                {
33
                    if (renameat(i_fd, "w", AT_FDCWD, "w") == -1)
34
                    {
35
                        if (errno != ENOENT)
36
                        {
37
                            perror("Failed to rename w");
38
                            return 1;
39
                        }
40
                        if (--tries == 0)
41
                            break;
42
                    }
43
                    else
44
                        tries = 10000;
45
                }
46
            }
47
            sleep(1);
48
        }
49
    }
50
}
(2-2/2)