summaryrefslogtreecommitdiffstats
path: root/tests/bugs/gfapi/bug-1447266/1460514.c
blob: f04d2d1463fa20ac0356a189c292c42db8917c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <glusterfs/api/glfs.h>
#include <glusterfs/api/glfs-handles.h>

#define LOG_ERR(func, ret) do { \
        if (ret != 0) {            \
                fprintf (stderr, "%s : returned error %d (%s)\n", \
                         func, ret, strerror (errno)); \
                goto out; \
        } else { \
                fprintf (stderr, "%s : returned %d\n", func, ret); \
        } \
        } while (0)

int
main (int argc, char *argv[])
{
        int                     ret            = 0;
        glfs_t                  *fs            = NULL;
        struct glfs_object      *root = NULL, *dir = NULL, *subdir = NULL;
        struct stat             sb             = {0, };
        char                    *dirname       = "dir";
        char                    *subdirname    = "subdir";
        char                    *logfile       = NULL;
        char                    *volname       = NULL;
        char                    *hostname      = NULL;
        unsigned char subdir_handle[GFAPI_HANDLE_LENGTH] = {'\0'};

        if (argc != 4) {
                fprintf (stderr, "Invalid argument\n");
                exit(1);
        }

        hostname = argv[1];
        volname = argv[2];
        logfile = argv[3];

        fs = glfs_new (volname);
        if (!fs) {
                fprintf (stderr, "glfs_new: returned NULL\n");
                ret = -1;
                goto out;
        }

        ret = glfs_set_volfile_server (fs, "tcp", hostname, 24007);
        LOG_ERR("glfs_set_volfile_server", ret);

        ret = glfs_set_logging (fs, logfile, 7);
        LOG_ERR("glfs_set_logging", ret);

        ret = glfs_init (fs);
        LOG_ERR("first attempt glfs_init", ret);

        root = glfs_h_lookupat (fs, NULL, "/", &sb, 0);
        if (root == NULL) {
                fprintf (stderr, "glfs_h_lookupat: error on lookup of / ,%s\n",
                         strerror (errno));
                goto out;
        }
        dir = glfs_h_mkdir (fs, root, dirname, 0644, &sb);
        if (dir == NULL) {
                 fprintf (stderr, "glfs_h_mkdir: error on directory creation dir ,%s\n",
                         strerror (errno));
                goto out;
        }
        subdir = glfs_h_mkdir (fs, root, subdirname, 0644, &sb);
        if (subdir == NULL) {
                 fprintf (stderr, "glfs_h_mkdir: error on directory creation subdir ,%s\n",
                         strerror (errno));
                goto out;
        }
        ret = glfs_h_extract_handle (subdir, subdir_handle,
                                             GFAPI_HANDLE_LENGTH);
        if (ret < 0) {
                fprintf (stderr, "glfs_h_extract_handle: error extracting handle of %s: %s\n",
                         subdirname, strerror (errno));
                goto out;
        }

        glfs_h_close (subdir);
        subdir = NULL;
        glfs_h_close (dir);
        dir = NULL;

        if (fs) {
                ret = glfs_fini(fs);
                fprintf (stderr, "glfs_fini(fs) returned %d \n", ret);
        }

        fs = NULL;

        fs = glfs_new (volname);
        if (!fs) {
                fprintf (stderr, "glfs_new: returned NULL\n");
                ret = -1;
                goto out;
        }

        ret = glfs_set_volfile_server (fs, "tcp", hostname, 24007);
        LOG_ERR("glfs_set_volfile_server", ret);

        ret = glfs_set_logging (fs, logfile, 7);
        LOG_ERR("glfs_set_logging", ret);

        ret = glfs_init (fs);
        LOG_ERR("second attempt glfs_init", ret);

        subdir = glfs_h_create_from_handle (fs, subdir_handle, GFAPI_HANDLE_LENGTH,
                                          &sb);
        if (subdir == NULL) {
                fprintf (stderr, "glfs_h_create_from_handle: error on create of %s: from (%p),%s\n",
                         subdirname, subdir_handle, strerror (errno));
                goto out;
        }
        dir = glfs_h_lookupat (fs, subdir, "..", &sb, 0);
        if (dir == NULL) {
                fprintf (stderr, "glfs_h_lookupat: error on directory lookup dir using .. ,%s\n",
                         strerror (errno));
                goto out;
        }

out:
        if (subdir)
                glfs_h_close (subdir);
        if (dir)
                glfs_h_close (dir);

        if (fs) {
                ret = glfs_fini(fs);
                fprintf (stderr, "glfs_fini(fs) returned %d \n", ret);
        }

        if (ret)
                exit(1);
        exit(0);
}