xref: /minix3/minix/lib/libvboxfs/link.c (revision 94e65446c4f963450ea94b8becc02cec062675cd)
1433d6423SLionel Sambuc /* Part of libvboxfs - (c) 2012, D.C. van Moolenbroek */
2433d6423SLionel Sambuc 
3433d6423SLionel Sambuc #include "inc.h"
4433d6423SLionel Sambuc 
5433d6423SLionel Sambuc /*
6433d6423SLionel Sambuc  * Create a directory.
7433d6423SLionel Sambuc  */
8433d6423SLionel Sambuc int
vboxfs_mkdir(const char * path,int mode)9*94e65446SDavid van Moolenbroek vboxfs_mkdir(const char *path, int mode)
10433d6423SLionel Sambuc {
11433d6423SLionel Sambuc 	vboxfs_handle_t h;
12433d6423SLionel Sambuc 	int r;
13433d6423SLionel Sambuc 
14433d6423SLionel Sambuc 	assert(S_ISDIR(mode));
15433d6423SLionel Sambuc 
16433d6423SLionel Sambuc 	if ((r = vboxfs_open_file(path, O_CREAT | O_EXCL | O_WRONLY, mode, &h,
17433d6423SLionel Sambuc 	   NULL)) != OK)
18433d6423SLionel Sambuc 		return r;
19433d6423SLionel Sambuc 
20433d6423SLionel Sambuc 	vboxfs_close_file(h);
21433d6423SLionel Sambuc 
22433d6423SLionel Sambuc 	return r;
23433d6423SLionel Sambuc }
24433d6423SLionel Sambuc 
25433d6423SLionel Sambuc /*
26433d6423SLionel Sambuc  * Remove a file or directory.
27433d6423SLionel Sambuc  */
28433d6423SLionel Sambuc static int
remove_file(const char * path,int dir)29*94e65446SDavid van Moolenbroek remove_file(const char *path, int dir)
30433d6423SLionel Sambuc {
31433d6423SLionel Sambuc 	vbox_param_t param[3];
32433d6423SLionel Sambuc 	vboxfs_path_t pathbuf;
33433d6423SLionel Sambuc 	int r;
34433d6423SLionel Sambuc 
35433d6423SLionel Sambuc 	if ((r = vboxfs_set_path(&pathbuf, path)) != OK)
36433d6423SLionel Sambuc 		return r;
37433d6423SLionel Sambuc 
38433d6423SLionel Sambuc 	/* FIXME: symbolic links are not supported at all yet */
39433d6423SLionel Sambuc 	vbox_set_u32(&param[0], vboxfs_root);
40433d6423SLionel Sambuc 	vbox_set_ptr(&param[1], &pathbuf, vboxfs_get_path_size(&pathbuf),
41433d6423SLionel Sambuc 	    VBOX_DIR_OUT);
42433d6423SLionel Sambuc 	vbox_set_u32(&param[2], dir ? VBOXFS_REMOVE_DIR : VBOXFS_REMOVE_FILE);
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc 	return vbox_call(vboxfs_conn, VBOXFS_CALL_REMOVE, param, 3, NULL);
45433d6423SLionel Sambuc }
46433d6423SLionel Sambuc 
47433d6423SLionel Sambuc /*
48433d6423SLionel Sambuc  * Unlink a file.
49433d6423SLionel Sambuc  */
50433d6423SLionel Sambuc int
vboxfs_unlink(const char * path)51*94e65446SDavid van Moolenbroek vboxfs_unlink(const char *path)
52433d6423SLionel Sambuc {
53433d6423SLionel Sambuc 
54433d6423SLionel Sambuc 	return remove_file(path, FALSE /*dir*/);
55433d6423SLionel Sambuc }
56433d6423SLionel Sambuc 
57433d6423SLionel Sambuc /*
58433d6423SLionel Sambuc  * Remove a directory.
59433d6423SLionel Sambuc  */
60433d6423SLionel Sambuc int
vboxfs_rmdir(const char * path)61*94e65446SDavid van Moolenbroek vboxfs_rmdir(const char *path)
62433d6423SLionel Sambuc {
63433d6423SLionel Sambuc 
64433d6423SLionel Sambuc 	return remove_file(path, TRUE /*dir*/);
65433d6423SLionel Sambuc }
66433d6423SLionel Sambuc 
67433d6423SLionel Sambuc /*
68433d6423SLionel Sambuc  * Rename a file or directory.
69433d6423SLionel Sambuc  */
70433d6423SLionel Sambuc static int
rename_file(const char * opath,const char * npath,int dir)71*94e65446SDavid van Moolenbroek rename_file(const char *opath, const char *npath, int dir)
72433d6423SLionel Sambuc {
73433d6423SLionel Sambuc 	vbox_param_t param[4];
74433d6423SLionel Sambuc 	vboxfs_path_t opathbuf, npathbuf;
75433d6423SLionel Sambuc 	u32_t flags;
76433d6423SLionel Sambuc 	int r;
77433d6423SLionel Sambuc 
78433d6423SLionel Sambuc 	if ((r = vboxfs_set_path(&opathbuf, opath)) != OK)
79433d6423SLionel Sambuc 		return r;
80433d6423SLionel Sambuc 
81433d6423SLionel Sambuc 	if ((r = vboxfs_set_path(&npathbuf, npath)) != OK)
82433d6423SLionel Sambuc 		return r;
83433d6423SLionel Sambuc 
84433d6423SLionel Sambuc 	flags = dir ? VBOXFS_RENAME_DIR : VBOXFS_RENAME_FILE;
85433d6423SLionel Sambuc 	flags |= VBOXFS_RENAME_REPLACE;
86433d6423SLionel Sambuc 
87433d6423SLionel Sambuc 	vbox_set_u32(&param[0], vboxfs_root);
88433d6423SLionel Sambuc 	vbox_set_ptr(&param[1], &opathbuf, vboxfs_get_path_size(&opathbuf),
89433d6423SLionel Sambuc 	    VBOX_DIR_OUT);
90433d6423SLionel Sambuc 	vbox_set_ptr(&param[2], &npathbuf, vboxfs_get_path_size(&npathbuf),
91433d6423SLionel Sambuc 	    VBOX_DIR_OUT);
92433d6423SLionel Sambuc 	vbox_set_u32(&param[3], flags);
93433d6423SLionel Sambuc 
94433d6423SLionel Sambuc 	return vbox_call(vboxfs_conn, VBOXFS_CALL_RENAME, param, 4, NULL);
95433d6423SLionel Sambuc }
96433d6423SLionel Sambuc 
97433d6423SLionel Sambuc /*
98433d6423SLionel Sambuc  * Rename a file or directory.
99433d6423SLionel Sambuc  */
100433d6423SLionel Sambuc int
vboxfs_rename(const char * opath,const char * npath)101*94e65446SDavid van Moolenbroek vboxfs_rename(const char *opath, const char *npath)
102433d6423SLionel Sambuc {
103433d6423SLionel Sambuc 	int r;
104433d6423SLionel Sambuc 
105433d6423SLionel Sambuc 	if ((r = rename_file(opath, npath, FALSE /*dir*/)) != EISDIR)
106433d6423SLionel Sambuc 		return r;
107433d6423SLionel Sambuc 
108433d6423SLionel Sambuc 	return rename_file(opath, npath, TRUE /*dir*/);
109433d6423SLionel Sambuc }
110