29e9f365ee3e249e4054b380e041b569007a5bdb
[u/mdw/putty] / mac / macstore.c
1 /* $Id: macstore.c,v 1.12 2003/01/21 00:27:24 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 void close_settings_r(void *handle) {
327 int fd;
328
329 if (handle == NULL) return;
330 fd = *(int *)handle;
331 CloseResFile(fd);
332 if (ResError() != noErr)
333 fatalbox("Close of saved session failed (%d)", ResError());
334 safefree(handle);
335 }
336
337 void del_settings(char const *sessionname) {
338 OSErr error;
339 FSSpec sessfile;
340 short sessVRefNum;
341 long sessDirID;
342 Str255 psessionname;
343
344 error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID);
345
346 c2pstrcpy(psessionname, sessionname);
347 error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile);
348 if (error != noErr) goto out;
349
350 error = FSpDelete(&sessfile);
351 return;
352 out:
353 fatalbox("Delete session failed (%d)", error);
354 }
355
356 struct enum_settings_state {
357 short vRefNum;
358 long dirID;
359 int index;
360 };
361
362 void *enum_settings_start(void) {
363 OSErr error;
364 struct enum_settings_state *state;
365
366 state = safemalloc(sizeof(*state));
367 error = get_session_dir(kDontCreateFolder, &state->vRefNum, &state->dirID);
368 if (error != noErr) {
369 safefree(state);
370 return NULL;
371 }
372 state->index = 1;
373 return state;
374 }
375
376 char *enum_settings_next(void *handle, char *buffer, int buflen) {
377 struct enum_settings_state *e = handle;
378 CInfoPBRec pb;
379 OSErr error = noErr;
380 Str255 name;
381
382 if (e == NULL) return NULL;
383 do {
384 pb.hFileInfo.ioNamePtr = name;
385 pb.hFileInfo.ioVRefNum = e->vRefNum;
386 pb.hFileInfo.ioDirID = e->dirID;
387 pb.hFileInfo.ioFDirIndex = e->index++;
388 error = PBGetCatInfoSync(&pb);
389 if (error != noErr) return NULL;
390 } while (!((pb.hFileInfo.ioFlAttrib & ioDirMask) == 0 &&
391 pb.hFileInfo.ioFlFndrInfo.fdCreator == PUTTY_CREATOR &&
392 pb.hFileInfo.ioFlFndrInfo.fdType == SESS_TYPE &&
393 name[0] < buflen));
394
395 p2cstrcpy(buffer, name);
396 return buffer;
397 }
398
399 void enum_settings_finish(void *handle) {
400
401 safefree(handle);
402 }
403
404 #define SEED_SIZE 512
405
406 void read_random_seed(noise_consumer_t consumer)
407 {
408 short puttyVRefNum;
409 long puttyDirID;
410 OSErr error;
411 char buf[SEED_SIZE];
412 short refnum;
413 long count = SEED_SIZE;
414
415 if (get_putty_dir(kDontCreateFolder, &puttyVRefNum, &puttyDirID) != noErr)
416 return;
417 if (HOpenDF(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed", fsRdPerm,
418 &refnum) != noErr)
419 return;
420 error = FSRead(refnum, &count, buf);
421 if (error != noErr && error != eofErr)
422 return;
423 (*consumer)(buf, count);
424 FSClose(refnum);
425 }
426
427 /*
428 * We don't bother with the usual FSpExchangeFiles dance here because
429 * it doesn't really matter if the old random seed gets lost.
430 */
431 void write_random_seed(void *data, int len)
432 {
433 short puttyVRefNum;
434 long puttyDirID;
435 OSErr error;
436 FSSpec dstfile;
437 short refnum;
438 long count = len;
439
440 if (get_putty_dir(kCreateFolder, &puttyVRefNum, &puttyDirID) != noErr)
441 return;
442
443 error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed",
444 &dstfile);
445 if (error == fnfErr) {
446 /* Set up standard resources */
447 FSpCreateResFile(&dstfile, INTERNAL_CREATOR, SEED_TYPE, smRoman);
448 refnum = FSpOpenResFile(&dstfile, fsWrPerm);
449 if (ResError() == noErr) {
450 copy_resource('STR ', -16397);
451 CloseResFile(refnum);
452 }
453 } else if (error != noErr) return;
454
455 if (FSpOpenDF(&dstfile, fsWrPerm, &refnum) != noErr) return;
456 FSWrite(refnum, &count, data);
457 FSClose(refnum);
458
459 return;
460 }
461
462 /*
463 * Emacs magic:
464 * Local Variables:
465 * c-file-style: "simon"
466 * End:
467 */