aboutsummaryrefslogtreecommitdiff
path: root/src/lib9pclient/ns.c
blob: 65225ecbc19d4b0e917fa0ef6aeedf14ba4fa1e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <9pclient.h>
#include <ctype.h>

CFsys*
nsinit(char *name)
{
	char *addr, *ns;
	int fd;

	ns = getns();
	if(ns == nil){
		werrstr("no name space");
		return nil;
	}

	addr = smprint("unix!%s/%s", ns, name);
	free(ns);
	if(addr == nil){
		werrstr("smprint: %r");
		return nil;
	}

	fd = dial(addr, 0, 0, 0);
	if(fd < 0){
		werrstr("dial %s: %r", addr);
		free(addr);
		return nil;
	}
	free(addr);

	fcntl(fd, F_SETFD, FD_CLOEXEC);
	return fsinit(fd);
}

CFsys*
nsmount(char *name, char *aname)
{
	CFsys *fs;
	CFid *fid;

	fs = nsinit(name);
	if(fs == nil)
		return nil;
	if((fid = fsattach(fs, nil, getuser(), aname)) == nil){
		_fsunmount(fs);
		return nil;
	}
	fssetroot(fs, fid);
	return fs;
}

CFid*
nsopen(char *name, char *aname, char *fname, int mode)
{
	CFsys *fs;
	CFid *fid;

	fs = nsmount(name, aname);
	if(fs == nil)
		return nil;
	fid = fsopen(fs, fname, mode);
	fsunmount(fs);
	return fid;
}