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

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

fprint(2, "create %s %s\n", dir, elem);
	if((fid = _fswalk(fs->root, dir)) == nil){
		if(p)
			*p = '/';
		return nil;
	}
	tx.type = Tcreate;
	tx.name = elem;
	tx.fid = fid->fid;
	tx.mode = mode;
	tx.perm = perm;
	if(_fsrpc(fs, &tx, &rx, 0) < 0){
		if(p)
			*p = '/';
		fsclose(fid);
		return nil;
	}
	if(p)
		*p = '/';
	fid->mode = mode;
	return fid;
}