Rather more natural (if much more complex) Mac Filename implementation.
[sgt/putty] / mac / macstore.c
1 /* $Id: macstore.c,v 1.15 2003/02/01 21:44:05 ben Exp $ */
2
3 /*
4 * macstore.c: Macintosh-specific impementation of the interface
5 * defined in storage.h
6 */
7
8 #include <MacTypes.h>
9 #include <Folders.h>
10 #include <Memory.h>
11 #include <Resources.h>
12 #include <TextUtils.h>
13
14 #include <stdio.h>
15 #include <string.h>
16
17 #include "putty.h"
18 #include "storage.h"
19 #include "mac.h"
20 #include "macresid.h"
21
22
23 OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit);
24
25 /*
26 * We store each session as a file in the "PuTTY" sub-directory of the
27 * preferences folder. Each (key,value) pair is stored as a resource.
28 */
29
30 OSErr get_putty_dir(Boolean makeit, short *pVRefNum, long *pDirID)
31 {
32 OSErr error = noErr;
33 short prefVRefNum;
34 FSSpec puttydir;
35 long prefDirID, puttyDirID;
36
37 error = FindFolder(kOnSystemDisk, kPreferencesFolderType, makeit,
38 &prefVRefNum, &prefDirID);
39 if (error != noErr) goto out;
40
41 error = FSMakeFSSpec(prefVRefNum, prefDirID, "\pPuTTY", &puttydir);
42 if (error != noErr && error != fnfErr) goto out;
43 error = FSpGetDirID(&puttydir, &puttyDirID, makeit);
44 if (error != noErr) goto out;
45
46 *pVRefNum = prefVRefNum;
47 *pDirID = puttyDirID;
48
49 out:
50 return error;
51 }
52
53 OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID) {
54 OSErr error = noErr;
55 short puttyVRefNum;
56 FSSpec sessdir;
57 long puttyDirID, sessDirID;
58
59 error = get_putty_dir(makeit, &puttyVRefNum, &puttyDirID);
60 if (error != noErr) goto out;
61 error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pSaved Sessions",
62 &sessdir);
63 if (error != noErr && error != fnfErr) goto out;
64 error = FSpGetDirID(&sessdir, &sessDirID, makeit);
65 if (error != noErr) goto out;
66
67 *pVRefNum = puttyVRefNum;
68 *pDirID = sessDirID;
69
70 out:
71 return error;
72 }
73
74 OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit) {
75 CInfoPBRec pb;
76 OSErr error = noErr;
77
78 pb.dirInfo.ioNamePtr = f->name;
79 pb.dirInfo.ioVRefNum = f->vRefNum;
80 pb.dirInfo.ioDrDirID = f->parID;
81 pb.dirInfo.ioFDirIndex = 0;
82 error = PBGetCatInfoSync(&pb);
83 if (error == fnfErr && makeit)
84 return FSpDirCreate(f, smSystemScript, idp);
85 if (error != noErr) goto out;
86 if ((pb.dirInfo.ioFlAttrib & ioDirMask) == 0) {
87 error = dirNFErr;
88 goto out;
89 }
90 *idp = pb.dirInfo.ioDrDirID;
91
92 out:
93 return error;
94 }
95
96 /* Copy a resource into the current resource file */
97 static OSErr copy_resource(ResType restype, short resid)
98 {
99 Handle h;
100 Str255 resname;
101
102 h = GetResource(restype, resid);
103 if (h != NULL) {
104 GetResInfo(h, &resid, &restype, resname);
105 DetachResource(h);
106 AddResource(h, restype, resid, resname);
107 if (ResError() == noErr)
108 WriteResource(h);
109 }
110 return ResError();
111 }
112
113 struct write_settings {
114 int fd;
115 FSSpec tmpfile;
116 FSSpec dstfile;
117 };
118
119 void *open_settings_w(char const *sessionname) {
120 short sessVRefNum;
121 long sessDirID;
122 OSErr error;
123 Str255 psessionname;
124 FSSpec dstfile;
125
126 error = get_session_dir(kCreateFolder, &sessVRefNum, &sessDirID);
127 if (error != noErr) return NULL;
128
129 c2pstrcpy(psessionname, sessionname);
130 error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &dstfile);
131 if (error == fnfErr) {
132 FSpCreateResFile(&dstfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript);
133 if ((error = ResError()) != noErr) return NULL;
134 } else if (error != noErr) return NULL;
135
136 return open_settings_w_fsp(&dstfile);
137 }
138
139 /*
140 * NB: Destination file must exist.
141 */
142 void *open_settings_w_fsp(FSSpec *dstfile)
143 {
144 short tmpVRefNum;
145 long tmpDirID;
146 struct write_settings *ws;
147 OSErr error;
148 Str255 tmpname;
149
150 ws = smalloc(sizeof *ws);
151 ws->dstfile = *dstfile;
152
153 /* Create a temporary file to save to first. */
154 error = FindFolder(ws->dstfile.vRefNum, kTemporaryFolderType,
155 kCreateFolder, &tmpVRefNum, &tmpDirID);
156 if (error != noErr) goto out;
157 c2pstrcpy(tmpname, tmpnam(NULL));
158 error = FSMakeFSSpec(tmpVRefNum, tmpDirID, tmpname, &ws->tmpfile);
159 if (error != noErr && error != fnfErr) goto out;
160 if (error == noErr) {
161 error = FSpDelete(&ws->tmpfile);
162 if (error != noErr) goto out;
163 }
164 FSpCreateResFile(&ws->tmpfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript);
165 if ((error = ResError()) != noErr) goto out;
166
167 ws->fd = FSpOpenResFile(&ws->tmpfile, fsWrPerm);
168 if (ws->fd == -1) {error = ResError(); goto out;}
169
170 /* Set up standard resources. Doesn't matter if these fail. */
171 copy_resource('STR ', -16396);
172 copy_resource('TMPL', TMPL_Int);
173
174 return ws;
175
176 out:
177 safefree(ws);
178 fatalbox("Failed to open session for write (%d)", error);
179 }
180
181 void write_setting_s(void *handle, char const *key, char const *value) {
182 int fd = *(int *)handle;
183 Handle h;
184 int id;
185 OSErr error;
186
187 UseResFile(fd);
188 if (ResError() != noErr)
189 fatalbox("Failed to open saved session (%d)", ResError());
190
191 error = PtrToHand(value, &h, strlen(value));
192 if (error != noErr)
193 fatalbox("Failed to allocate memory");
194 /* Put the data in a resource. */
195 id = Unique1ID(FOUR_CHAR_CODE('TEXT'));
196 if (ResError() != noErr)
197 fatalbox("Failed to get ID for resource %s (%d)", key, ResError());
198 addresource(h, FOUR_CHAR_CODE('TEXT'), id, key);
199 if (ResError() != noErr)
200 fatalbox("Failed to add resource %s (%d)", key, ResError());
201 }
202
203 void write_setting_i(void *handle, char const *key, int value) {
204 int fd = *(int *)handle;
205 Handle h;
206 int id;
207 OSErr error;
208
209 UseResFile(fd);
210 if (ResError() != noErr)
211 fatalbox("Failed to open saved session (%d)", ResError());
212
213 /* XXX assume all systems have the same "int" format */
214 error = PtrToHand(&value, &h, sizeof(int));
215 if (error != noErr)
216 fatalbox("Failed to allocate memory (%d)", error);
217
218 /* Put the data in a resource. */
219 id = Unique1ID(FOUR_CHAR_CODE('Int '));
220 if (ResError() != noErr)
221 fatalbox("Failed to get ID for resource %s (%d)", key, ResError());
222 addresource(h, FOUR_CHAR_CODE('Int '), id, key);
223 if (ResError() != noErr)
224 fatalbox("Failed to add resource %s (%d)", key, ResError());
225 }
226
227 void close_settings_w(void *handle) {
228 struct write_settings *ws = handle;
229 OSErr error;
230
231 CloseResFile(ws->fd);
232 if ((error = ResError()) != noErr)
233 goto out;
234 error = FSpExchangeFiles(&ws->tmpfile, &ws->dstfile);
235 if (error != noErr) goto out;
236 error = FSpDelete(&ws->tmpfile);
237 if (error != noErr) goto out;
238 return;
239
240 out:
241 fatalbox("Close of saved session failed (%d)", error);
242 safefree(handle);
243 }
244
245 void *open_settings_r(char const *sessionname)
246 {
247 short sessVRefNum;
248 long sessDirID;
249 FSSpec sessfile;
250 OSErr error;
251 Str255 psessionname;
252
253 error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID);
254
255 c2pstrcpy(psessionname, sessionname);
256 error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile);
257 if (error != noErr) goto out;
258 return open_settings_r_fsp(&sessfile);
259
260 out:
261 return NULL;
262 }
263
264 void *open_settings_r_fsp(FSSpec *sessfile)
265 {
266 OSErr error;
267 int fd;
268 int *handle;
269
270 fd = FSpOpenResFile(sessfile, fsRdPerm);
271 if (fd == 0) {error = ResError(); goto out;}
272
273 handle = safemalloc(sizeof *handle);
274 *handle = fd;
275 return handle;
276
277 out:
278 return NULL;
279 }
280
281 char *read_setting_s(void *handle, char const *key, char *buffer, int buflen) {
282 int fd;
283 Handle h;
284 size_t len;
285
286 if (handle == NULL) goto out;
287 fd = *(int *)handle;
288 UseResFile(fd);
289 if (ResError() != noErr) goto out;
290 h = get1namedresource(FOUR_CHAR_CODE('TEXT'), key);
291 if (h == NULL) goto out;
292
293 len = GetHandleSize(h);
294 if (len + 1 > buflen) goto out;
295 memcpy(buffer, *h, len);
296 buffer[len] = '\0';
297
298 ReleaseResource(h);
299 if (ResError() != noErr) goto out;
300 return buffer;
301
302 out:
303 return NULL;
304 }
305
306 int read_setting_i(void *handle, char const *key, int defvalue) {
307 int fd;
308 Handle h;
309 int value;
310
311 if (handle == NULL) goto out;
312 fd = *(int *)handle;
313 UseResFile(fd);
314 if (ResError() != noErr) goto out;
315 h = get1namedresource(FOUR_CHAR_CODE('Int '), key);
316 if (h == NULL) goto out;
317 value = *(int *)*h;
318 ReleaseResource(h);
319 if (ResError() != noErr) goto out;
320 return value;
321
322 out:
323 return defvalue;
324 }
325
326 int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
327 {
328 char *settingname;
329 FontSpec ret;
330 char tmp[256];
331
332 if (!read_setting_s(handle, name, tmp, sizeof(tmp)))
333 return 0;
334 c2pstrcpy(ret.name, tmp);
335 settingname = dupcat(name, "Face", NULL);
336 ret.face = read_setting_i(handle, settingname, 0);
337 sfree(settingname);
338 settingname = dupcat(name, "Height", NULL);
339 ret.size = read_setting_i(handle, settingname, 0);
340 sfree(settingname);
341 if (ret.size == 0) return 0;
342 *result = ret;
343 return 1;
344 }
345
346 void write_setting_fontspec(void *handle, const char *name, FontSpec font)
347 {
348 char *settingname;
349 char tmp[256];
350
351 p2cstrcpy(tmp, font.name);
352 write_setting_s(handle, name, tmp);
353 settingname = dupcat(name, "Face", NULL);
354 write_setting_i(handle, settingname, font.face);
355 sfree(settingname);
356 settingname = dupcat(name, "Size", NULL);
357 write_setting_i(handle, settingname, font.size);
358 sfree(settingname);
359 }
360
361 int read_setting_filename(void *handle, const char *name, Filename *result)
362 {
363 int fd;
364 AliasHandle h;
365 Boolean changed;
366 OSErr err;
367
368 if (handle == NULL) goto out;
369 fd = *(int *)handle;
370 UseResFile(fd);
371 if (ResError() != noErr) goto out;
372 h = (AliasHandle)get1namedresource(rAliasType, name);
373 if (h == NULL) goto out;
374 if ((*h)->userType == 'pTTY' && (*h)->aliasSize == sizeof(**h))
375 memset(result, 0, sizeof(*result));
376 else {
377 err = ResolveAlias(NULL, h, &result->fss, &changed);
378 if (err != noErr && err != fnfErr) goto out;
379 if ((*h)->userType == 'pTTY') {
380 long dirid;
381 StrFileName fname;
382
383 /* Tail of record is pascal string contaning leafname */
384 if (FSpGetDirID(&result->fss, &dirid, FALSE) != noErr) goto out;
385 memcpy(fname, (char *)*h + (*h)->aliasSize,
386 GetHandleSize((Handle)h) - (*h)->aliasSize);
387 err = FSMakeFSSpec(result->fss.vRefNum, dirid, fname,
388 &result->fss);
389 if (err != noErr && err != fnfErr) goto out;
390 }
391 }
392 ReleaseResource((Handle)h);
393 if (ResError() != noErr) goto out;
394 return 1;
395
396 out:
397 return 0;
398 }
399
400 void write_setting_filename(void *handle, const char *name, Filename fn)
401 {
402 int fd = *(int *)handle;
403 AliasHandle h;
404 int id;
405 OSErr error;
406
407 UseResFile(fd);
408 if (ResError() != noErr)
409 fatalbox("Failed to open saved session (%d)", ResError());
410
411 if (filename_is_null(fn)) {
412 /* Generate a special "null" alias */
413 h = (AliasHandle)NewHandle(sizeof(**h));
414 if (h == NULL)
415 fatalbox("Failed to create fake alias");
416 (*h)->userType = 'pTTY';
417 (*h)->aliasSize = sizeof(**h);
418 } else {
419 error = NewAlias(NULL, &fn.fss, &h);
420 if (error == fnfErr) {
421 /*
422 * NewAlias can't create an alias for a nonexistent file.
423 * Create an alias for the directory, and record the
424 * filename as well.
425 */
426 FSSpec tmpfss;
427
428 FSMakeFSSpec(fn.fss.vRefNum, fn.fss.parID, NULL, &tmpfss);
429 error = NewAlias(NULL, &tmpfss, &h);
430 if (error != noErr)
431 fatalbox("Failed to create alias");
432 (*h)->userType = 'pTTY';
433 SetHandleSize((Handle)h, (*h)->aliasSize + fn.fss.name[0] + 1);
434 if (MemError() != noErr)
435 fatalbox("Failed to create alias");
436 memcpy((char *)*h + (*h)->aliasSize, fn.fss.name,
437 fn.fss.name[0] + 1);
438 }
439 if (error != noErr)
440 fatalbox("Failed to create alias");
441 }
442 /* Put the data in a resource. */
443 id = Unique1ID(rAliasType);
444 if (ResError() != noErr)
445 fatalbox("Failed to get ID for resource %s (%d)", name, ResError());
446 addresource((Handle)h, rAliasType, id, name);
447 if (ResError() != noErr)
448 fatalbox("Failed to add resource %s (%d)", name, ResError());
449 }
450
451 void close_settings_r(void *handle) {
452 int fd;
453
454 if (handle == NULL) return;
455 fd = *(int *)handle;
456 CloseResFile(fd);
457 if (ResError() != noErr)
458 fatalbox("Close of saved session failed (%d)", ResError());
459 safefree(handle);
460 }
461
462 void del_settings(char const *sessionname) {
463 OSErr error;
464 FSSpec sessfile;
465 short sessVRefNum;
466 long sessDirID;
467 Str255 psessionname;
468
469 error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID);
470
471 c2pstrcpy(psessionname, sessionname);
472 error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile);
473 if (error != noErr) goto out;
474
475 error = FSpDelete(&sessfile);
476 return;
477 out:
478 fatalbox("Delete session failed (%d)", error);
479 }
480
481 struct enum_settings_state {
482 short vRefNum;
483 long dirID;
484 int index;
485 };
486
487 void *enum_settings_start(void) {
488 OSErr error;
489 struct enum_settings_state *state;
490
491 state = safemalloc(sizeof(*state));
492 error = get_session_dir(kDontCreateFolder, &state->vRefNum, &state->dirID);
493 if (error != noErr) {
494 safefree(state);
495 return NULL;
496 }
497 state->index = 1;
498 return state;
499 }
500
501 char *enum_settings_next(void *handle, char *buffer, int buflen) {
502 struct enum_settings_state *e = handle;
503 CInfoPBRec pb;
504 OSErr error = noErr;
505 Str255 name;
506
507 if (e == NULL) return NULL;
508 do {
509 pb.hFileInfo.ioNamePtr = name;
510 pb.hFileInfo.ioVRefNum = e->vRefNum;
511 pb.hFileInfo.ioDirID = e->dirID;
512 pb.hFileInfo.ioFDirIndex = e->index++;
513 error = PBGetCatInfoSync(&pb);
514 if (error != noErr) return NULL;
515 } while (!((pb.hFileInfo.ioFlAttrib & ioDirMask) == 0 &&
516 pb.hFileInfo.ioFlFndrInfo.fdCreator == PUTTY_CREATOR &&
517 pb.hFileInfo.ioFlFndrInfo.fdType == SESS_TYPE &&
518 name[0] < buflen));
519
520 p2cstrcpy(buffer, name);
521 return buffer;
522 }
523
524 void enum_settings_finish(void *handle) {
525
526 safefree(handle);
527 }
528
529 #define SEED_SIZE 512
530
531 void read_random_seed(noise_consumer_t consumer)
532 {
533 short puttyVRefNum;
534 long puttyDirID;
535 OSErr error;
536 char buf[SEED_SIZE];
537 short refnum;
538 long count = SEED_SIZE;
539
540 if (get_putty_dir(kDontCreateFolder, &puttyVRefNum, &puttyDirID) != noErr)
541 return;
542 if (HOpenDF(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed", fsRdPerm,
543 &refnum) != noErr)
544 return;
545 error = FSRead(refnum, &count, buf);
546 if (error != noErr && error != eofErr)
547 return;
548 (*consumer)(buf, count);
549 FSClose(refnum);
550 }
551
552 /*
553 * We don't bother with the usual FSpExchangeFiles dance here because
554 * it doesn't really matter if the old random seed gets lost.
555 */
556 void write_random_seed(void *data, int len)
557 {
558 short puttyVRefNum;
559 long puttyDirID;
560 OSErr error;
561 FSSpec dstfile;
562 short refnum;
563 long count = len;
564
565 if (get_putty_dir(kCreateFolder, &puttyVRefNum, &puttyDirID) != noErr)
566 return;
567
568 error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed",
569 &dstfile);
570 if (error == fnfErr) {
571 /* Set up standard resources */
572 FSpCreateResFile(&dstfile, INTERNAL_CREATOR, SEED_TYPE, smRoman);
573 refnum = FSpOpenResFile(&dstfile, fsWrPerm);
574 if (ResError() == noErr) {
575 copy_resource('STR ', -16397);
576 CloseResFile(refnum);
577 }
578 } else if (error != noErr) return;
579
580 if (FSpOpenDF(&dstfile, fsWrPerm, &refnum) != noErr) return;
581 FSWrite(refnum, &count, data);
582 FSClose(refnum);
583
584 return;
585 }
586
587 /*
588 * Emacs magic:
589 * Local Variables:
590 * c-file-style: "simon"
591 * End:
592 */