summaryrefslogtreecommitdiff
path: root/src/lib9/tempdir_plan9.c
blob: 80d7ddbe4bbe1b3afd127bead035a2c325cd22f5 (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
// Copyright 2013 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build plan9

#include <u.h>
#include <libc.h>

char*
mktempdir(void)
{
	char *p;
	int fd, i;
	
	p = smprint("/tmp/go-link-XXXXXX");
	for(i=0; i<1000; i++) {
		sprint(p, "/tmp/go-link-%06x", nrand((1<<24)-1));
		fd = create(p, OREAD|OEXCL, 0700|DMDIR);
		if(fd >= 0) {
			close(fd);
			return p;
		}
	}
	free(p);
	return nil;
}

void
removeall(char *p)
{
	int fd, n, i;
	Dir *d;
	char *q;
	
	if(remove(p) >= 0)
		return;
	if((d = dirstat(p)) == nil)
		return;
	if(!(d->mode & DMDIR)) {
		free(d);
		return;
	}
	free(d);
	
	if((fd = open(p, OREAD)) < 0)
		return;
	n = dirreadall(fd, &d);
	close(fd);
	for(i=0; i<n; i++) {
		q = smprint("%s/%s", p, d[i].name);
		removeall(q);
		free(q);
	}
	free(d);
}