Imported Upstream version 2.6.0p2
[debian/amanda] / device-src / tests / vfs_test.c
1 /*
2  * Copyright (c) 2005 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1 as
6  * published by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
11  * License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16  *
17  * Contact information: Zmanda Inc., 505 N Mathlida Ave, Suite 120
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 #include <device.h>
22 #include <amanda.h>
23 #include "util.h"
24
25 /* global so the 'atexit' handler can access it */
26
27 static void
28 cleanup_vtape_dir(char *device_path)
29 {
30     char *quoted = g_shell_quote(device_path);
31     char *cmd = vstralloc("rm -rf ", quoted, NULL);
32
33     /* would you rather write 'rm -rf' here? */
34     if (system(cmd) == -1) {
35         exit(1);
36     }
37
38     amfree(cmd);
39     amfree(quoted);
40 }
41
42 static char *
43 setup_vtape_dir(void)
44 {
45     char *cwd = g_get_current_dir();
46     char *device_path = NULL;
47     char *data_dir = NULL;
48
49     device_path = vstralloc(cwd, "/vfs-test-XXXXXX", NULL);
50     amfree(cwd);
51
52     if (mkdtemp(device_path) == NULL) {
53         fprintf(stderr, "Could not create temporary directory in %s\n", cwd);
54         return NULL;
55     }
56
57     /* append "/data/" to that for the VFS device*/
58     data_dir = vstralloc(device_path, "/data/", NULL);
59     if (mkdir(data_dir, 0777) == -1) {
60         fprintf(stderr, "Could not create %s: %s\n", cwd, strerror(errno));
61         amfree(data_dir);
62         return NULL;
63     }
64
65     amfree(data_dir);
66     return device_path;
67 }
68
69 static Device *
70 setup_device(char *device_path)
71 {
72     Device *device;
73     char *device_name = NULL;
74
75     device_name = vstralloc("file:", device_path, NULL);
76     device = device_open(device_name);
77     if (!device) {
78         fprintf(stderr, "Could not open device %s\n", device_name);
79     }
80
81     amfree(device_name);
82     return device;
83 }
84
85 static gboolean
86 check_free_space(Device *device)
87 {
88     GValue value;
89     QualifiedSize qsize;
90
91     bzero(&value, sizeof(value));
92     if (!device_property_get(device, PROPERTY_FREE_SPACE, &value)) {
93         fprintf(stderr, "Could not get property_free_space\n");
94         return FALSE;
95     }
96
97     qsize = *(QualifiedSize*)g_value_get_boxed(&value);
98     g_value_unset(&value);
99
100     if (qsize.accuracy != SIZE_ACCURACY_REAL) {
101         fprintf(stderr, "property_free_space accuracy is not SIZE_ACCURACY_REAL\n");
102         return FALSE;
103     }
104
105     if (qsize.bytes == 0) {
106         fprintf(stderr, "property_free_space returned bytes=0\n");
107         return FALSE;
108     }
109
110     return TRUE;
111 }
112
113 int
114 main(int argc G_GNUC_UNUSED, char **argv G_GNUC_UNUSED)
115 {
116     Device *device = NULL;
117     gboolean ok = TRUE;
118     char *device_path = NULL;
119     pid_t pid;
120     amwait_t status;
121
122     amanda_thread_init();
123
124     device_path = setup_vtape_dir();
125
126     /* run the tests in a subprocess so we can clean up even if they fail */
127     switch (pid = fork()) {
128         case -1: /* error */
129             perror("fork");
130             g_assert_not_reached();
131
132         case 0: /* child */
133             device_api_init();
134
135             device = setup_device(device_path);
136             if (!device)
137                 return 1;
138
139             ok = ok && check_free_space(device);
140
141             g_object_unref(device);
142
143             if (!ok) exit(1);
144             exit(0);
145             g_assert_not_reached();
146
147         default: /* parent */
148             if (waitpid(pid, &status, 0) == -1)
149                 perror("waitpid");
150
151             /* cleanup */
152             cleanup_vtape_dir(device_path);
153             amfree(device_path);
154
155             /* figure our own return status */
156             if (WIFEXITED(status))
157                 return WEXITSTATUS(status);
158             else if (WIFSIGNALED(status)) {
159                 fprintf(stderr, "Test failed with signal %d\n", (int)WTERMSIG(status));
160                 return 1;
161             } else {
162                 /* weird.. */
163                 return 1;
164             }
165             g_assert_not_reached();
166     }
167 }