2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
20 #include <sys/types.h>
34 #include <atalk/adouble.h>
35 #include <atalk/cnid.h>
38 #define ADv2_DIRNAME ".AppleDouble"
40 #define DIR_DOT_OR_DOTDOT(a) \
41 ((strcmp(a, ".") == 0) || (strcmp(a, "..") == 0))
43 static const char *labels[] = {
55 static char *new_label;
56 static char *new_type;
57 static char *new_creator;
58 static char *new_flags;
59 static char *new_attributes;
61 static void usage_set(void)
64 "Usage: ad set [-t TYPE] [-c CREATOR] [-l label] [-f flags] [-a attributes] file|dir \n\n"
66 " none | grey | green | violet | blue | yellow | red | orange\n\n"
68 " d = On Desktop (f/d)\n"
69 " e = Hidden extension (f/d)\n"
70 " m = Shared (can run multiple times) (f)\n"
71 " n = No INIT resources (f)\n"
73 " c = Custom icon (f/d)\n"
74 " t = Stationery (f)\n"
75 " s = Name locked (f/d)\n"
77 " v = Invisible (f/d)\n"
78 " a = Alias file (f/d)\n\n"
82 " p = Needs backup (f/d)\n"
83 " r = No rename (f/d)\n"
84 " l = No delete (f/d)\n"
85 " o = No copy (f)\n\n"
86 " Uppercase letter sets the flag, lowercase removes the flag\n"
87 " f = valid for files\n"
88 " d = valid for directories\n"
93 static void change_type(char *path, afpvol_t *vol, const struct stat *st, struct adouble *ad, char *new_type)
97 if ((FinderInfo = ad_entry(ad, ADEID_FINDERI)))
98 memcpy(FinderInfo, new_type, 4);
101 static void change_creator(char *path, afpvol_t *vol, const struct stat *st, struct adouble *ad, char *new_creator)
105 if ((FinderInfo = ad_entry(ad, ADEID_FINDERI)))
106 memcpy(FinderInfo + 4, new_creator, 4);
110 static void change_label(char *path, afpvol_t *vol, const struct stat *st, struct adouble *ad, char *new_label)
113 const char **color = &labels[0];
114 uint16_t FinderFlags;
115 unsigned char color_count = 0;
117 if ((FinderInfo = ad_entry(ad, ADEID_FINDERI)) == NULL)
121 if (strcasecmp(*color, new_label) == 0) {
123 memcpy(&FinderFlags, FinderInfo + 8, 2);
124 FinderFlags = ntohs(FinderFlags);
127 FinderFlags &= ~FINDERINFO_COLOR;
128 FinderFlags |= color_count << 1;
131 FinderFlags = ntohs(FinderFlags);
132 memcpy(FinderInfo + 8, &FinderFlags, 2);
141 static void change_attributes(char *path, afpvol_t *vol, const struct stat *st, struct adouble *ad, char *new_attributes)
143 uint16_t AFPattributes;
145 ad_getattr(ad, &AFPattributes);
146 AFPattributes = ntohs(AFPattributes);
148 if (S_ISREG(st->st_mode)) {
149 if (strchr(new_attributes, 'W'))
150 AFPattributes |= ATTRBIT_NOWRITE;
151 if (strchr(new_attributes, 'w'))
152 AFPattributes &= ~ATTRBIT_NOWRITE;
154 if (strchr(new_attributes, 'O'))
155 AFPattributes |= ATTRBIT_NOCOPY;
156 if (strchr(new_attributes, 'o'))
157 AFPattributes &= ~ATTRBIT_NOCOPY;
160 if (strchr(new_attributes, 'Y'))
161 AFPattributes |= ATTRBIT_SYSTEM;
162 if (strchr(new_attributes, 'y'))
163 AFPattributes &= ~ATTRBIT_SYSTEM;
165 if (strchr(new_attributes, 'P'))
166 AFPattributes |= ATTRBIT_BACKUP;
167 if (strchr(new_attributes, 'p'))
168 AFPattributes &= ~ATTRBIT_BACKUP;
170 if (strchr(new_attributes, 'R'))
171 AFPattributes |= ATTRBIT_NORENAME;
172 if (strchr(new_attributes, 'r'))
173 AFPattributes &= ~ATTRBIT_NORENAME;
175 if (strchr(new_attributes, 'L'))
176 AFPattributes |= ATTRBIT_NODELETE;
177 if (strchr(new_attributes, 'l'))
178 AFPattributes &= ~ATTRBIT_NODELETE;
180 AFPattributes = ntohs(AFPattributes);
181 ad_setattr(ad, AFPattributes);
184 static void change_flags(char *path, afpvol_t *vol, const struct stat *st, struct adouble *ad, char *new_flags)
187 uint16_t FinderFlags;
189 if ((FinderInfo = ad_entry(ad, ADEID_FINDERI)) == NULL)
192 memcpy(&FinderFlags, FinderInfo + 8, 2);
193 FinderFlags = ntohs(FinderFlags);
195 if (S_ISREG(st->st_mode)) {
196 if (strchr(new_flags, 'M'))
197 FinderFlags |= FINDERINFO_ISHARED;
198 if (strchr(new_flags, 'm'))
199 FinderFlags &= ~FINDERINFO_ISHARED;
201 if (strchr(new_flags, 'N'))
202 FinderFlags |= FINDERINFO_HASNOINITS;
203 if (strchr(new_flags, 'n'))
204 FinderFlags &= ~FINDERINFO_HASNOINITS;
206 if (strchr(new_flags, 'T'))
207 FinderFlags |= FINDERINFO_ISSTATIONNERY;
208 if (strchr(new_flags, 't'))
209 FinderFlags &= ~FINDERINFO_ISSTATIONNERY;
212 if (strchr(new_flags, 'D'))
213 FinderFlags |= FINDERINFO_ISONDESK;
214 if (strchr(new_flags, 'd'))
215 FinderFlags &= ~FINDERINFO_ISONDESK;
217 if (strchr(new_flags, 'E'))
218 FinderFlags |= FINDERINFO_HIDEEXT;
219 if (strchr(new_flags, 'e'))
220 FinderFlags &= ~FINDERINFO_HIDEEXT;
222 if (strchr(new_flags, 'I'))
223 FinderFlags |= FINDERINFO_HASBEENINITED;
224 if (strchr(new_flags, 'i'))
225 FinderFlags &= ~FINDERINFO_HASBEENINITED;
227 if (strchr(new_flags, 'C'))
228 FinderFlags |= FINDERINFO_HASCUSTOMICON;
229 if (strchr(new_flags, 'c'))
230 FinderFlags &= ~FINDERINFO_HASCUSTOMICON;
232 if (strchr(new_flags, 'S'))
233 FinderFlags |= FINDERINFO_NAMELOCKED;
234 if (strchr(new_flags, 's'))
235 FinderFlags &= ~FINDERINFO_NAMELOCKED;
237 if (strchr(new_flags, 'B'))
238 FinderFlags |= FINDERINFO_HASBUNDLE;
239 if (strchr(new_flags, 'b'))
240 FinderFlags &= ~FINDERINFO_HASBUNDLE;
242 if (strchr(new_flags, 'V'))
243 FinderFlags |= FINDERINFO_INVISIBLE;
244 if (strchr(new_flags, 'v'))
245 FinderFlags &= ~FINDERINFO_INVISIBLE;
247 if (strchr(new_flags, 'A'))
248 FinderFlags |= FINDERINFO_ISALIAS;
249 if (strchr(new_flags, 'a'))
250 FinderFlags &= ~FINDERINFO_ISALIAS;
252 FinderFlags = ntohs(FinderFlags);
253 memcpy(FinderInfo + 8, &FinderFlags, 2);
256 int ad_set(int argc, char **argv, AFPObj *obj)
264 while ((c = getopt(argc, argv, ":l:t:c:f:a:")) != -1) {
267 new_label = strdup(optarg);
270 new_type = strdup(optarg);
273 new_creator = strdup(optarg);
276 new_flags = strdup(optarg);
279 new_attributes = strdup(optarg);
295 openvol(obj, argv[optind], &vol);
296 if (vol.vol->v_path == NULL)
299 if (stat(argv[optind], &st) != 0) {
304 if (S_ISDIR(st.st_mode))
305 adflags = ADFLAGS_DIR;
307 ad_init(&ad, vol.vol);
309 if (ad_open(&ad, argv[optind], adflags | ADFLAGS_HF | ADFLAGS_CREATE | ADFLAGS_RDWR, 0666) < 0)
313 change_label(argv[optind], &vol, &st, &ad, new_label);
315 change_type(argv[optind], &vol, &st, &ad, new_type);
317 change_creator(argv[optind], &vol, &st, &ad, new_creator);
319 change_flags(argv[optind], &vol, &st, &ad, new_flags);
321 change_attributes(argv[optind], &vol, &st, &ad, new_attributes);
324 ad_close(&ad, ADFLAGS_HF);