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