root/branches/Orange-Branch/test/io/trove/trove-rm.c @ 8872

Revision 8872, 5.3 KB (checked in by mtmoore, 2 years ago)

compiler warning cleanup

Line 
1/*
2 * (C) 2002 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7#include <unistd.h>
8#include <string.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <getopt.h>
12
13#include <trove.h>
14#include <trove-test.h>
15
16char storage_space[SSPACE_SIZE] = "/tmp/trove-test-space";
17char file_system[FS_SIZE] = "fs-foo";
18char path_to_file[PATH_SIZE] = "/bar";
19TROVE_handle requested_file_handle = 4095;
20
21int parse_args(int argc, char **argv);
22
23int main(int argc, char **argv)
24{
25    int ret, count, i;
26    char *file_name;
27    char path_name[PATH_SIZE];
28
29    TROVE_op_id op_id;
30    TROVE_coll_id coll_id;
31    TROVE_handle file_handle, parent_handle;
32    TROVE_ds_state state;
33    TROVE_keyval_s key, val;
34    TROVE_ds_attributes_s s_attr;
35    TROVE_context_id trove_context = -1;
36
37    ret = parse_args(argc, argv);
38    if (ret < 0) {
39        fprintf(stderr, "argument parsing failed.\n");
40        return -1;
41    }
42
43    ret = trove_initialize(
44        TROVE_METHOD_DBPF, NULL, storage_space, storage_space, 0);
45    if (ret < 0) {
46        fprintf(stderr, "initialize failed.\n");
47        return -1;
48    }
49
50    /* try to look up collection used to store file system */
51    ret = trove_collection_lookup(
52        TROVE_METHOD_DBPF, file_system, &coll_id, NULL, &op_id);
53    if (ret < 0) {
54        fprintf(stderr, "collection lookup failed.\n");
55        return -1;
56    }
57
58    ret = trove_open_context(coll_id, &trove_context);
59    if (ret < 0)
60    {
61        fprintf(stderr, "trove_open_context failed\n");
62        return -1;
63    }
64
65    /* find the parent directory name */
66    strcpy(path_name, path_to_file);
67    for (i=strlen(path_name); i >= 0; i--) {
68        if (path_name[i] != '/') path_name[i] = '\0';
69        else break;
70    }
71    file_name = path_to_file + strlen(path_name);
72#if 0
73    printf("path is %s\n", path_name);
74    printf("file is %s\n", file_name);
75#endif
76
77    /* find the parent directory handle */
78    ret = path_lookup(coll_id, path_name, &parent_handle);
79    if (ret < 0) {
80        return -1;
81    }
82
83    /* TODO: make a is_dir function... maybe make a full blown stat(2)? */
84   
85    /* look up the handle for the file */
86    memset(&key, 0, sizeof(key));
87    memset(&val, 0, sizeof(val));
88    key.buffer = file_name;
89    key.buffer_sz = strlen(file_name)+1;
90    val.buffer = &file_handle;
91    val.buffer_sz = sizeof(TROVE_handle);
92
93    /* it would be smart to verify that this is a directory first... */
94    ret = trove_keyval_read(coll_id, parent_handle, &key, &val,
95                            0, NULL, NULL, trove_context, &op_id,
96                            NULL);
97    while (ret == 0) ret = trove_dspace_test(
98        coll_id, op_id, trove_context, &count, NULL, NULL, &state,
99        TROVE_DEFAULT_TEST_TIMEOUT);
100    if ( ret < 0 || state == -1) {
101            fprintf(stderr, "read failed for key %s\n", file_name);
102            return -1;
103    }
104
105    ret = trove_dspace_getattr(coll_id,
106                               file_handle,
107                               &s_attr,
108                               0 /* flags */,
109                               NULL,
110                               trove_context,
111                               &op_id,
112                               NULL);
113    while (ret == 0) ret = trove_dspace_test(
114        coll_id, op_id, trove_context, &count, NULL, NULL, &state,
115        TROVE_DEFAULT_TEST_TIMEOUT);
116    if (ret < 0) return -1;
117
118    if ((int)s_attr.type != TROVE_TEST_FILE) {
119        fprintf(stderr, "%s is not a file.\n", file_name);
120        return -1;
121    }
122
123    /* 'handles are everything':  now that we've gotten a handle from the
124     * file_name, we can wipe the keyval (via name) and the dspace (via
125     * handle)*/
126
127    key.buffer = file_name;
128    key.buffer_sz = strlen(file_name)+1;
129
130    ret = trove_keyval_remove(coll_id, parent_handle, &key, NULL,
131                              0, NULL, NULL, trove_context, &op_id,
132                              NULL);
133    while (ret == 0) ret = trove_dspace_test(
134        coll_id, op_id, trove_context, &count, NULL, NULL, &state,
135        TROVE_DEFAULT_TEST_TIMEOUT);
136    if (ret < 0 ) {
137            fprintf(stderr, "removal failed for %s\n", file_name);
138            return -1;
139    }
140
141    /* the question: is it up to the caller to clean up the dspace if it removed the last entry?  no no no*/
142    /* gar gar being dense:  the dspace gets removed.  */
143    ret = trove_dspace_remove(coll_id,
144                              file_handle,
145                              TROVE_SYNC,
146                              NULL,
147                              trove_context,
148                              &op_id,
149                              NULL);
150    while (ret == 0) ret = trove_dspace_test(
151        coll_id, op_id, trove_context, &count, NULL, NULL, &state,
152        TROVE_DEFAULT_TEST_TIMEOUT);
153    if (ret < 0) {
154        fprintf(stderr, "dspace remove failed.\n");
155        return -1;
156    }
157
158    trove_close_context(coll_id, trove_context);
159    trove_finalize(TROVE_METHOD_DBPF);
160    printf("file %s removed (file handle = %d, parent handle = %d).\n",
161           file_name,
162           (int) file_handle,
163           (int) parent_handle);
164
165    return 0;
166}
167
168int parse_args(int argc, char **argv)
169{
170    int c;
171
172    while ((c = getopt(argc, argv, "s:c:p:h:")) != EOF) {
173        switch (c) {
174            case 's':
175                strncpy(storage_space, optarg, SSPACE_SIZE);
176                break;
177            case 'c': /* collection */
178                strncpy(file_system, optarg, FS_SIZE);
179                break;
180            case 'p':
181                strncpy(path_to_file, optarg, PATH_SIZE);
182            case 'h':
183                requested_file_handle = atoll(optarg);
184                break;
185            case '?':
186            default:
187                return -1;
188        }
189    }
190    return 0;
191}
192
193/*
194 * Local variables:
195 *  c-indent-level: 4
196 *  c-basic-offset: 4
197 * End:
198 *
199 * vim: ts=8 sts=4 sw=4 expandtab
200 */
Note: See TracBrowser for help on using the browser.