forked from miekg/rdup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.c
70 lines (60 loc) · 1.28 KB
/
dir.c
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
68
69
70
/*
* Copyright (c) 2009 - 2011 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));
#ifdef DEBUG
msgd(__func__, __LINE__,_("Making directory writable `%s\'"), p);
#endif /* DEBUG */
if (stat(p, s) == -1)
return NULL;
/* make it writable, assume we are the OWNER */
if (chmod(p, s->st_mode | S_IWUSR) == -1) {
msgd(__func__, __LINE__,_("Failed to make directory writeable `%s\': %s"), p, strerror(errno));
}
return s;
}
void
dir_restore(gchar *p, struct stat *s)
{
if (!s || !p)
return;
/* restore perms - assumes *s has not be f*cked up */
if (chmod(p, s->st_mode & 07777) == -1) {
msgd(__func__, __LINE__,_("Failed to restore permissions `%s\': %s"), p, strerror(errno));
}
}
/**
* return parent dir string
* p MUST not end in a /
*/
gchar *
dir_parent(gchar *p)
{
gchar *p2;
gchar *n;
gchar *copy;
if (!p)
return NULL;
if (p[0] == '/' && p[1] == '\0')
return p;
copy = g_strdup(p);
n = strrchr(copy, '/');
if (n) {
*(n+1) = '\0';
p2 = g_strdup(copy);
g_free(copy);
*n= '/';
return p2;
}
return NULL;
}