-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias2symlink.c
308 lines (241 loc) · 9.14 KB
/
alias2symlink.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// aliasToSymlink.c
/*
Add opts:
-c, --check-only Perform check if file is an alias. DONE
Print alias target and return 1 if true.
If false return 0 do not print anything.
-r, --recursive Recursively check for aliases in subfolders. DONE
Not active by default.
-d, --delete-alias Delete alias after symlink was succesfully created DONE
-n, --name Symlink name. %s stands for alias old name. DONE
"%s.symlink" Syntax of `sprintf` is used.
If symlink name is equal "%s" and `-d` flag is not set,
error will be shown.
--verbose Print all output DONE
--brief Print only important output, default behaviour
-x, --print-all Prints all aliases and symlinks in current folder
Do not create symlinks
-p, --print Print - lists all aliases in current folder DONE
Do not create symlinks
-s, --create-symlink Creates symlink with relative path from given alias.
alias
symlink
Workflow:
- if (-c) us set, getTrueName of the file and exit
- check all files in current folder, include subfolders if (-r) set
- get alias target
- convert absolute path to relative path
- create symlink
- delete alias (-d)
- rename symlink (-n)
*/
#include <Carbon/Carbon.h>
#include <dirent.h>
#include <getopt.h>
#define MAX_PATH_SIZE 1024
#define VERBOSE_PRINTF(a) if (opt_verbose_flag) {printf("<verbose> "); printf a;}
void listdir(char *name, int level, char * rootDirName, char * dirPath);
int getTrueName(UInt8 *fileName, UInt8 * targetPath);
int createSymlink(char * aliasName, UInt8 * targetPath, char * rootDirName, int level);
//flags
static int opt_verbose_flag;
static int opt_print_flag;
static int opt_recursive_flag;
static int opt_delete_flag;
static char opt_symlink_name[256] = "%s.symlink";
static char* opt_check_only_file;
static char* opt_work_folder;
int main ( int argc, char * argv[] )
{
char rootDirName[MAX_PATH_SIZE];
UInt8 targetPath[MAX_PATH_SIZE+1];
char dirPath[MAX_PATH_SIZE];
int wasAliased;
static struct option long_options[] =
{
{"verbose", no_argument, &opt_verbose_flag, 1},
{"brief", no_argument, &opt_verbose_flag, 0},
{"recursive", no_argument, 0, 'r'},
{"delete", no_argument, 0, 'd'},
{"print", no_argument, 0, 'p'},
{"check-only", required_argument, 0, 'c'},
{"name", required_argument, 0, 'n'},
{0, 0, 0, 0}
};
int c;
while (1)
{
int option_index = 0;
c = getopt_long(argc, argv, "rdpc:n:", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 'r':
opt_recursive_flag = 1;
break;
case 'd':
opt_delete_flag = 1;
break;
case 'c':
opt_check_only_file = optarg;
break;
case 'n':
strcpy(opt_symlink_name, optarg);
break;
case 'p':
opt_print_flag = 1;
break;
default:
abort();
}
}
if (opt_verbose_flag)
VERBOSE_PRINTF(("--verbose flag set\n"));
if (opt_recursive_flag)
VERBOSE_PRINTF(("--recursive flag set\n"));
if (opt_delete_flag)
VERBOSE_PRINTF(("--delete flag set\n"));
if (opt_check_only_file)
VERBOSE_PRINTF(("--check-only flag set\n"));
if (opt_print_flag)
VERBOSE_PRINTF(("--print flag set\n"));
VERBOSE_PRINTF(("--name=%s\n", opt_symlink_name));
if (strcmp(opt_symlink_name, "%s") == 0 && !opt_delete_flag) {
printf("Cannot rename symlink to alias not deleting alias\n");
printf("Please specify `-d` flag or change `-n`\n");
exit(-1);
}
if (opt_check_only_file) {
wasAliased = getTrueName((UInt8 *)opt_check_only_file, targetPath);
if (wasAliased) {
printf("%s\n", targetPath);
}
exit(wasAliased);
}
if (argc <= optind) {
printf("Please specify folder: ./aliasToSymlink --verboase -[rcd] [-n \"\"] [FOLDER]\n");
exit(255);
} else {
opt_work_folder = argv[optind];
VERBOSE_PRINTF(("opt_work_folder=%s\n\n", opt_work_folder));
}
realpath(opt_work_folder, rootDirName);
VERBOSE_PRINTF(("rootDirName=%s\n\n", rootDirName));
listdir(opt_work_folder, 0, rootDirName, dirPath);
exit(0);
}
int createSymlink(char * aliasName, UInt8 * targetPath, char * rootDirName, int level)
{
char targetRelativePath[MAX_PATH_SIZE];
char targetRelativePathUp[MAX_PATH_SIZE];
char symlinkName[MAX_PATH_SIZE];
char newSymlinkName[MAX_PATH_SIZE];
char buf[MAX_PATH_SIZE];
int rootDirNameLen;
int targetPathLen;
char commonDirPath[MAX_PATH_SIZE];
int commonPathLen;
sprintf(symlinkName, "%s.symlink", aliasName);
rootDirNameLen = strlen(rootDirName);
targetPathLen = strlen((const char *)targetPath);
VERBOSE_PRINTF(("rootDirName length=%d\n", rootDirNameLen));
VERBOSE_PRINTF(("targetPath length=%d\n", targetPathLen));
//targetPath + rootDirNameLen + 1 - to skip trailing slash "/"
targetRelativePathUp[0] = 0;
if (targetPathLen > rootDirNameLen) {
strncpy(targetRelativePath, (const char *)targetPath + rootDirNameLen + 1, targetPathLen - rootDirNameLen);
} else {
for (int i = 0;i < strlen((const char *)targetPath);i++) {
if (targetPath[i] != rootDirName[i]) {
level = level + 1;
break;
}
if (targetPath[i] == '/') {
level = level - 1;
commonPathLen = i;
}
}
for (int i = 0;i < strlen(rootDirName);i++) {
if (rootDirName[i] == '/') {
level = level + 1;
}
}
VERBOSE_PRINTF(("commotPathLen=%d\n", commonPathLen));
strncpy(targetRelativePath, (const char *)targetPath + commonPathLen + 1, targetPathLen - commonPathLen);
}
for (int i = 0;i < level; i++) {
strcat(targetRelativePathUp, "../");
}
strcat(targetRelativePathUp, targetRelativePath);
VERBOSE_PRINTF(("targetRelativePath=%s\n", targetRelativePath));
VERBOSE_PRINTF(("targetRelativePathUp=%s\n", targetRelativePathUp));
VERBOSE_PRINTF(("\n"));
printf("%s:\tCreating symlink to %s\n", symlinkName, targetRelativePathUp);
if (access(symlinkName, F_OK) != -1) {
printf("symlink `%s` already exists, skipping\n", symlinkName);
printf("\n");
return 0;
} else {
symlink(targetRelativePathUp, symlinkName);
printf("SYMLINK CREATED\n");
if (opt_delete_flag) {
VERBOSE_PRINTF(("deleting alias\n"));
remove(aliasName);
printf("%s: Deleted alias\n", aliasName);
}
if (strcmp(opt_symlink_name, "%s.symlink") != 0) {
sprintf(newSymlinkName, opt_symlink_name, aliasName);
VERBOSE_PRINTF(("renaming symlink to %s\n", newSymlinkName));
rename(symlinkName, newSymlinkName);
printf("%s: renamed symlink\n", newSymlinkName);
}
printf("\n");
}
return 1;
}
void listdir(char *name, int level, char * rootDirName, char * dirPath)
{
DIR *dir;
struct dirent *entry;
int wasAliased;
UInt8 targetPath[MAX_PATH_SIZE+1];
char fileName[MAX_PATH_SIZE];
//printf("dirPath: %s\n", dirPath);
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do {
//FIXME:
if (entry->d_type == DT_DIR && opt_recursive_flag) {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0 || entry->d_name[0] == '.')
continue;
//printf("%*s[%s]\n", level*2, "", entry->d_name);
strcat(dirPath, entry->d_name);
strcat(dirPath, "/");
listdir(path, level + 1, rootDirName, dirPath);
}
else {
strcpy(fileName, dirPath);
strcat(fileName, entry->d_name);
wasAliased = getTrueName((UInt8 *)fileName, targetPath);
if (!wasAliased) {
targetPath[0] = 0;
} else {
VERBOSE_PRINTF(("%*s- %s alias=%s | %s\n", level*2, "", entry->d_name, wasAliased==1 ? "true" : "false", targetPath));
if (!opt_print_flag) {
createSymlink(fileName, targetPath, rootDirName, level);
} else {
printf("alias: %s/%s => %s\n", rootDirName, fileName, targetPath);
}
}
}
} while ((entry = readdir(dir)) != NULL);
closedir(dir);
}