-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersonalisation.c
130 lines (109 loc) · 2.94 KB
/
personalisation.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#define BUFSIZE 64
#define USER_CACHE_LAUNCH "/home/user/.cache/launch"
#define DEFAULT_THEME "/usr/share/themes/default"
#define ETC_HILDON_THEME "/etc/hildon/theme"
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
char *temp_file = NULL;
char *temp_file_usr_share = NULL;
char *theme_name;
DIR *cachedir = NULL;
struct dirent *dir;
if (argc != 2) {
fprintf(stderr, "usage: personalisation name_of_theme\n");
return ret;
}
// TODO: Sanitize theme name to a single word?
theme_name = argv[1];
temp_file = strdup("/tmp/themeXXXXXX");
if (mkstemp(temp_file) == -1) {
fprintf(stderr, "Could not mkstemp: %s\n", strerror(errno));
goto done;
}
if (unlink(temp_file)) {
fprintf(stderr,
"Could not remove just created temporary file\n");
goto done;
}
if ((mkdir("/etc/hildon", 0755) == -1) && (errno != EEXIST)) {
fprintf(stderr, "Could not create /etc/hildon\n");
goto done;
}
if (symlink(DEFAULT_THEME, temp_file)) {
fprintf(stderr, "Could not symlink temporary file: %s\n",
strerror(errno));
goto done;
}
if (rename(temp_file, "/etc/hildon/theme")) {
fprintf(stderr, "Could not rename temporary file: %s\n",
strerror(errno));
goto done;
}
temp_file_usr_share = strdup("/tmp/themeXXXXXX");
if (mkstemp(temp_file_usr_share) == -1) {
fprintf(stderr, "Could not mkstemp: %s\n", strerror(errno));
goto done;
}
if (unlink(temp_file_usr_share)) {
fprintf(stderr,
"Could not remove just created temporary file\n");
goto done;
}
if (symlink(theme_name, temp_file_usr_share)) {
fprintf(stderr, "Could not symlink temporary file: %s\n",
strerror(errno));
goto done;
}
if (rename(temp_file_usr_share, DEFAULT_THEME)) {
fprintf(stderr, "Could not rename temporary file: %s\n",
strerror(errno));
goto done;
}
// We're OK once we change the symlinks
ret = EXIT_SUCCESS;
cachedir = opendir(USER_CACHE_LAUNCH);
if (!cachedir) {
fprintf(stderr, "Could not open/clear cache dir\n");
goto done;
}
while ((dir = readdir(cachedir))) {
const char *name = dir->d_name;
if (strncmp(".", name, strlen(".")) == 0) {
// Skip anything starting with '.'
continue;
}
int chars_req = strlen(name) + strlen(USER_CACHE_LAUNCH) + 2;
char *cache_launch_dir_entry = malloc(sizeof(char) * chars_req);
if (!cache_launch_dir_entry) {
fprintf(stderr,
"Unable to allocate memory for path to remove call\n");
goto done2;
}
snprintf(cache_launch_dir_entry, chars_req, "%s/%s",
"/home/user/.cache/launch", name);
if (remove(cache_launch_dir_entry)) {
fprintf(stderr, "Cannot remove %s: %s\n",
cache_launch_dir_entry, strerror(errno));
}
free(cache_launch_dir_entry);
}
done2:
closedir(cachedir);
done:
if (temp_file) {
free(temp_file);
}
if (temp_file_usr_share) {
free(temp_file_usr_share);
}
return ret;
}