aboutsummaryrefslogtreecommitdiff
path: root/src/lib9pclient/create.c
blob: 62e26b2293455200a8bddd08b19d65b8d7901b57 (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
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <9pclient.h>
#include "fsimpl.h"

int
fsfcreate(CFid *fid, char *name, int mode, ulong perm)
{
	Fcall tx, rx;

	tx.type = Tcreate;
	tx.name = name;
	tx.fid = fid->fid;
	tx.mode = mode;
	tx.perm = perm;
	tx.extension = nil;
	if(_fsrpc(fid->fs, &tx, &rx, 0) < 0)
		return -1;
	fid->mode = mode;
	return 0;
}

CFid*
fscreate(CFsys *fs, char *name, int mode, ulong perm)
{
	CFid *fid;
	char *p, *dir, *elem;
	
	p = strrchr(name, '/');
	if(p == nil){
		dir = "";
		elem = name;
	}else{
		dir = name;
		*p = 0;
		elem = p+1;
	}

	if((fid = fswalk(fs->root, dir)) == nil){
		if(p)
			*p = '/';
		return nil;
	}
	if(p)
		*p = '/';
	if(fsfcreate(fid, elem, mode, perm) < 0){
		fsclose(fid);
		return nil;
	}
	return fid;
}