[go: up one dir, main page]

File: dir.c

package info (click to toggle)
rdup 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,996 kB
  • ctags: 185
  • sloc: ansic: 3,326; sh: 3,221; exp: 166; makefile: 64
file content (59 lines) | stat: -rw-r--r-- 930 bytes parent folder | download
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
/* 
 * Copyright (c) 2009 Miek Gieben
 * See LICENSE for the license
 * make a dir writable and later remove that
 * right again
 */

#include "rdup-up.h"

struct stat *
dir_write(gchar *p)
{
	 /* chmod +w . && rm $file && chmod -w # and hope for the best */
	if (!p)
		return NULL;

	struct stat *s = g_malloc(sizeof(struct stat));

	if (stat(p, s) == -1)
		return NULL;

	/* make it writable, assume we are the OWNER */
	chmod(p, s->st_mode | S_IWUSR);
	return s;
}

void
dir_restore(gchar *p, struct stat *s)
{
	if (!s || !p)
		return;
	/* restore perms - assumes *s has not be f*cked up */
	chmod(p, s->st_mode & ~S_IFMT);
}

/**
 * return parent dir string
 * p MUST not end in a /
 */
gchar *
dir_parent(gchar *p)
{
	gchar *p2;
	gchar *n;

	if (!p)
		return NULL;
	if (p[0] == '/' && p[1] == '\0')
		return p;

	n = strrchr(p, '/'); 
	if (n) {
		*(n+1) = '\0';
		p2 = g_strdup(p);
		*n= '/';
		return p2;
	}
	return NULL;
}