-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtc_test_listdirs.c
142 lines (129 loc) · 4.05 KB
/
tc_test_listdirs.c
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
/**
* Copyright (C) Stony Brook University 2016
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
/**
* This is an example showing how to list the content of multiple directories
* using TC API. It has the same effect as bash command
*
* ls -l /vfs0/dir{1,2,3}
*
* @file tc_test_listdirs.c
* @brief Test listing multiple directories.
*
*/
#include "config.h"
#include "nfs_init.h"
#include "fsal.h"
#include "log.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include "../nfs4/nfs4_util.h"
#include "common_types.h"
static char exe_path[PATH_MAX];
static char tc_config_path[PATH_MAX];
#define DEFAULT_LOG_FILE "/tmp/tc_test_listdirs.log"
static bool process_direntry(const struct tc_attrs *dentry, const char *dir,
void *cbarg)
{
fprintf(stderr, "listed '%s' from directory '%s'\n", dentry->file.path,
dir);
return true;
}
int main(int argc, char *argv[])
{
void *context = NULL;
const int NDIRS = 3;
const int NFILES = 5;
struct tc_attrs dirs[NDIRS];
char *path;
tc_res tcres;
int i, j;
int rc;
const char *DIR_PATHS[] = { "/vfs0/dir1", "/vfs0/dir2", "/vfs0/dir3" };
struct tc_attrs_masks masks = {0};
/* Locate and use the default config file in the repo. Before running
* this example, please update the config file to a correct NFS server.
*/
readlink("/proc/self/exe", exe_path, PATH_MAX);
snprintf(tc_config_path, PATH_MAX,
"%s/../../../config/tc.ganesha.conf", dirname(exe_path));
fprintf(stderr, "using config file: %s\n", tc_config_path);
/* Initialize TC services and daemons */
context = tc_init(tc_config_path, DEFAULT_LOG_FILE, 77);
if (context == NULL) {
NFS4_ERR("Error while initializing tc_client using config "
"file: %s; see log at %s",
tc_config_path, DEFAULT_LOG_FILE);
return EIO;
}
/* create common parent directory */
tcres = tc_ensure_dir("/vfs0", 0755, NULL);
if (!tc_okay(tcres)) {
NFS4_ERR("failed to create parent directory /vfs0");
goto exit;
}
/* create directories */
for (i = 0; i < NDIRS; ++i) {
tc_set_up_creation(&dirs[i], DIR_PATHS[i], 0755);
}
tcres = tc_mkdirv(dirs, NDIRS, false);
if (tc_okay(tcres)) {
fprintf(stderr, "successfully created %d directories\n", NDIRS);
} else {
fprintf(stderr, "failed to create directories\n");
goto exit;
}
/* create 5 files in each directories */
struct tc_attrs *files = alloca(NDIRS * NFILES * sizeof(*files));
memset(files, 0, NDIRS * NFILES * sizeof(*files));
for (i = 0; i < NDIRS; ++i) {
for (j = 0; j < NFILES; ++j) {
path = alloca(PATH_MAX);
snprintf(path, PATH_MAX, "%s/file%d", DIR_PATHS[i], j);
tc_set_up_creation(&files[i * NFILES + j], path, 0755);
fprintf(stderr, "set up %d-th file %s under %s\n",
i * NFILES + j, path, DIR_PATHS[i]);
}
}
/* FIXME: use tc_writev() to create files instead of directories */
tcres = tc_mkdirv(files, NDIRS * NFILES, false);
if (tc_okay(tcres)) {
fprintf(stderr, "successfully created %d files\n",
NDIRS * NFILES);
} else {
fprintf(stderr, "failed to create files\n");
goto exit;
}
tcres = tc_listdirv(DIR_PATHS, NDIRS, masks, 0, false, process_direntry,
NULL, false);
if (tc_okay(tcres)) {
fprintf(stderr, "successfully listed %d files\n",
NDIRS * NFILES);
} else {
fprintf(stderr, "failed to list files\n");
goto exit;
}
exit:
tc_deinit(context);
return tcres.err_no;
}