xref: /minix3/minix/lib/libvboxfs/vboxfs.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc /* Part of libvboxfs - (c) 2012, D.C. van Moolenbroek */
2*433d6423SLionel Sambuc 
3*433d6423SLionel Sambuc #include "inc.h"
4*433d6423SLionel Sambuc 
5*433d6423SLionel Sambuc vbox_conn_t vboxfs_conn;
6*433d6423SLionel Sambuc vboxfs_root_t vboxfs_root;
7*433d6423SLionel Sambuc 
8*433d6423SLionel Sambuc static struct sffs_table vboxfs_table = {
9*433d6423SLionel Sambuc 	.t_open		= vboxfs_open,
10*433d6423SLionel Sambuc 	.t_read		= vboxfs_read,
11*433d6423SLionel Sambuc 	.t_write	= vboxfs_write,
12*433d6423SLionel Sambuc 	.t_close	= vboxfs_close,
13*433d6423SLionel Sambuc 
14*433d6423SLionel Sambuc 	.t_readbuf	= vboxfs_buffer,
15*433d6423SLionel Sambuc 	.t_writebuf	= vboxfs_buffer,
16*433d6423SLionel Sambuc 
17*433d6423SLionel Sambuc 	.t_opendir	= vboxfs_opendir,
18*433d6423SLionel Sambuc 	.t_readdir	= vboxfs_readdir,
19*433d6423SLionel Sambuc 	.t_closedir	= vboxfs_closedir,
20*433d6423SLionel Sambuc 
21*433d6423SLionel Sambuc 	.t_getattr	= vboxfs_getattr,
22*433d6423SLionel Sambuc 	.t_setattr	= vboxfs_setattr,
23*433d6423SLionel Sambuc 
24*433d6423SLionel Sambuc 	.t_mkdir	= vboxfs_mkdir,
25*433d6423SLionel Sambuc 	.t_unlink	= vboxfs_unlink,
26*433d6423SLionel Sambuc 	.t_rmdir	= vboxfs_rmdir,
27*433d6423SLionel Sambuc 	.t_rename	= vboxfs_rename,
28*433d6423SLionel Sambuc 
29*433d6423SLionel Sambuc 	.t_queryvol	= vboxfs_queryvol
30*433d6423SLionel Sambuc };
31*433d6423SLionel Sambuc 
32*433d6423SLionel Sambuc /*
33*433d6423SLionel Sambuc  * Initialize communication with the VBOX driver, and map the given share.
34*433d6423SLionel Sambuc  */
35*433d6423SLionel Sambuc int
vboxfs_init(char * share,const struct sffs_table ** tablep,int * case_insens,int * read_only)36*433d6423SLionel Sambuc vboxfs_init(char *share, const struct sffs_table **tablep, int *case_insens,
37*433d6423SLionel Sambuc 	int *read_only)
38*433d6423SLionel Sambuc {
39*433d6423SLionel Sambuc 	vbox_param_t param[4];
40*433d6423SLionel Sambuc 	vboxfs_path_t path;
41*433d6423SLionel Sambuc 	vboxfs_volinfo_t volinfo;
42*433d6423SLionel Sambuc 	int r;
43*433d6423SLionel Sambuc 
44*433d6423SLionel Sambuc 	if ((r = vboxfs_set_path(&path, share)) != OK)
45*433d6423SLionel Sambuc 		return r;
46*433d6423SLionel Sambuc 
47*433d6423SLionel Sambuc 	if ((r = vbox_init()) != OK)
48*433d6423SLionel Sambuc 		return r;
49*433d6423SLionel Sambuc 
50*433d6423SLionel Sambuc 	if ((vboxfs_conn = r = vbox_open("VBoxSharedFolders")) < 0)
51*433d6423SLionel Sambuc 		return r;
52*433d6423SLionel Sambuc 
53*433d6423SLionel Sambuc 	r = vbox_call(vboxfs_conn, VBOXFS_CALL_SET_UTF8, NULL, 0, NULL);
54*433d6423SLionel Sambuc 	if (r != OK) {
55*433d6423SLionel Sambuc 		vbox_close(vboxfs_conn);
56*433d6423SLionel Sambuc 
57*433d6423SLionel Sambuc 		return r;
58*433d6423SLionel Sambuc 	}
59*433d6423SLionel Sambuc 
60*433d6423SLionel Sambuc 	vbox_set_ptr(&param[0], &path, vboxfs_get_path_size(&path),
61*433d6423SLionel Sambuc 	    VBOX_DIR_OUT);
62*433d6423SLionel Sambuc 	vbox_set_u32(&param[1], 0);
63*433d6423SLionel Sambuc 	vbox_set_u32(&param[2], '/');	/* path separator */
64*433d6423SLionel Sambuc 	vbox_set_u32(&param[3], TRUE);	/* case sensitivity - no effect? */
65*433d6423SLionel Sambuc 
66*433d6423SLionel Sambuc 	r = vbox_call(vboxfs_conn, VBOXFS_CALL_MAP_FOLDER, param, 4, NULL);
67*433d6423SLionel Sambuc 	if (r != OK) {
68*433d6423SLionel Sambuc 		vbox_close(vboxfs_conn);
69*433d6423SLionel Sambuc 
70*433d6423SLionel Sambuc 		return r;
71*433d6423SLionel Sambuc 	}
72*433d6423SLionel Sambuc 
73*433d6423SLionel Sambuc 	vboxfs_root = vbox_get_u32(&param[1]);
74*433d6423SLionel Sambuc 
75*433d6423SLionel Sambuc 	/* Gather extra information about the mapped shared. */
76*433d6423SLionel Sambuc 	if (vboxfs_query_vol("", &volinfo) == OK) {
77*433d6423SLionel Sambuc 		*case_insens = !volinfo.props.casesens;
78*433d6423SLionel Sambuc 		*read_only = !!volinfo.props.readonly;
79*433d6423SLionel Sambuc 	}
80*433d6423SLionel Sambuc 
81*433d6423SLionel Sambuc 	*tablep = &vboxfs_table;
82*433d6423SLionel Sambuc 	return OK;
83*433d6423SLionel Sambuc }
84*433d6423SLionel Sambuc 
85*433d6423SLionel Sambuc /*
86*433d6423SLionel Sambuc  * Unmap the share, and disconnect from the VBOX driver.
87*433d6423SLionel Sambuc  */
88*433d6423SLionel Sambuc void
vboxfs_cleanup(void)89*433d6423SLionel Sambuc vboxfs_cleanup(void)
90*433d6423SLionel Sambuc {
91*433d6423SLionel Sambuc 	vbox_param_t param[1];
92*433d6423SLionel Sambuc 
93*433d6423SLionel Sambuc 	vbox_set_u32(&param[0], vboxfs_root);
94*433d6423SLionel Sambuc 
95*433d6423SLionel Sambuc 	vbox_call(vboxfs_conn, VBOXFS_CALL_UNMAP_FOLDER, param, 1, NULL);
96*433d6423SLionel Sambuc 
97*433d6423SLionel Sambuc 	vbox_close(vboxfs_conn);
98*433d6423SLionel Sambuc }
99