1 /* Part of libvboxfs - (c) 2012, D.C. van Moolenbroek */
2
3 #include "inc.h"
4
5 /*
6 * Create a directory.
7 */
8 int
vboxfs_mkdir(const char * path,int mode)9 vboxfs_mkdir(const char *path, int mode)
10 {
11 vboxfs_handle_t h;
12 int r;
13
14 assert(S_ISDIR(mode));
15
16 if ((r = vboxfs_open_file(path, O_CREAT | O_EXCL | O_WRONLY, mode, &h,
17 NULL)) != OK)
18 return r;
19
20 vboxfs_close_file(h);
21
22 return r;
23 }
24
25 /*
26 * Remove a file or directory.
27 */
28 static int
remove_file(const char * path,int dir)29 remove_file(const char *path, int dir)
30 {
31 vbox_param_t param[3];
32 vboxfs_path_t pathbuf;
33 int r;
34
35 if ((r = vboxfs_set_path(&pathbuf, path)) != OK)
36 return r;
37
38 /* FIXME: symbolic links are not supported at all yet */
39 vbox_set_u32(¶m[0], vboxfs_root);
40 vbox_set_ptr(¶m[1], &pathbuf, vboxfs_get_path_size(&pathbuf),
41 VBOX_DIR_OUT);
42 vbox_set_u32(¶m[2], dir ? VBOXFS_REMOVE_DIR : VBOXFS_REMOVE_FILE);
43
44 return vbox_call(vboxfs_conn, VBOXFS_CALL_REMOVE, param, 3, NULL);
45 }
46
47 /*
48 * Unlink a file.
49 */
50 int
vboxfs_unlink(const char * path)51 vboxfs_unlink(const char *path)
52 {
53
54 return remove_file(path, FALSE /*dir*/);
55 }
56
57 /*
58 * Remove a directory.
59 */
60 int
vboxfs_rmdir(const char * path)61 vboxfs_rmdir(const char *path)
62 {
63
64 return remove_file(path, TRUE /*dir*/);
65 }
66
67 /*
68 * Rename a file or directory.
69 */
70 static int
rename_file(const char * opath,const char * npath,int dir)71 rename_file(const char *opath, const char *npath, int dir)
72 {
73 vbox_param_t param[4];
74 vboxfs_path_t opathbuf, npathbuf;
75 u32_t flags;
76 int r;
77
78 if ((r = vboxfs_set_path(&opathbuf, opath)) != OK)
79 return r;
80
81 if ((r = vboxfs_set_path(&npathbuf, npath)) != OK)
82 return r;
83
84 flags = dir ? VBOXFS_RENAME_DIR : VBOXFS_RENAME_FILE;
85 flags |= VBOXFS_RENAME_REPLACE;
86
87 vbox_set_u32(¶m[0], vboxfs_root);
88 vbox_set_ptr(¶m[1], &opathbuf, vboxfs_get_path_size(&opathbuf),
89 VBOX_DIR_OUT);
90 vbox_set_ptr(¶m[2], &npathbuf, vboxfs_get_path_size(&npathbuf),
91 VBOX_DIR_OUT);
92 vbox_set_u32(¶m[3], flags);
93
94 return vbox_call(vboxfs_conn, VBOXFS_CALL_RENAME, param, 4, NULL);
95 }
96
97 /*
98 * Rename a file or directory.
99 */
100 int
vboxfs_rename(const char * opath,const char * npath)101 vboxfs_rename(const char *opath, const char *npath)
102 {
103 int r;
104
105 if ((r = rename_file(opath, npath, FALSE /*dir*/)) != EISDIR)
106 return r;
107
108 return rename_file(opath, npath, TRUE /*dir*/);
109 }
110