blob: 31510b3d4b141b320061d351afbfb7939eb0af20 [file] [log] [blame]
Jack Jansene9b2a052001-11-06 11:10:13 +00001/*
2 $Log$
3 Revision 1.1 2001/11/06 11:10:13 jackjansen
4 HFS+ API contributed by Nitin Ganatra. This checkin is identical to what he sent me, except for the namechange (fmgr->hfsplus).
5
6 Revision 1.8 2001/10/03 17:29:01 ganatra
7 add parent method to FSRef class
8
9 Revision 1.7 2001/04/13 20:54:19 ganatra
10 More standard format for MacOSError exceptions
11
12 Revision 1.6 2001/04/11 04:07:40 ganatra
13 Add permissions constants and log header..
14
15*/
16
17
18#include <CoreServices/CoreServices.h>
19#include "Python.h"
20
21static PyObject *
22dict_from_cataloginfo(FSCatalogInfoBitmap bitmap, const FSCatalogInfo *info, HFSUniStr255 *uni);
23static
24PyObject *macos_error_for_call(OSErr err, const char *name, const char *item);
25static
26int insert_long(FSCatalogInfoBitmap bitmap, UInt32 bit, PyObject *dict, const char *symbol, UInt32 value);
27static
28int insert_slong(FSCatalogInfoBitmap bitmap, UInt32 bit, PyObject *dict, const char *symbol, long value);
29static
30int insert_int(PyObject *d, const char *symbol, int value);
31static
32Boolean fsref_isdirectory(const FSRef *ref);
33static
34PyObject *obj_to_hfsunistr(PyObject *in, HFSUniStr255 *uni);
35
36static
37int cataloginfo_from_dict(FSCatalogInfoBitmap bitmap, FSCatalogInfo *info, const PyObject *dict);
38
39
40static PyObject *ErrorObject;
41
42//__________________________________________________________________________________________________
43//_______________________________________ FORKREF OBJECT ___________________________________________
44//__________________________________________________________________________________________________
45
46typedef struct {
47 PyObject_HEAD
48 PyObject *x_attr;
49 short forkref;
50} forkRefObject;
51
52staticforward PyTypeObject forkRefObject_Type;
53
54#define forkRefObject_Check(v) ((v)->ob_type == &forkRefObject_Type)
55
56//__________________________________________________________________________________________________
57
58static
59forkRefObject *newForkRefObject(PyObject *arg, FSRef *ref, Boolean resourceFork, SInt8 permissions)
60{
61 OSErr err;
62 HFSUniStr255 forkName;
63
64 forkRefObject *self = PyObject_New(forkRefObject, &forkRefObject_Type);
65 if (self == NULL)
66 return NULL;
67
68 if (resourceFork)
69 (void) FSGetResourceForkName(&forkName);
70 else
71 (void) FSGetDataForkName(&forkName);
72
73 Py_BEGIN_ALLOW_THREADS
74 err = FSOpenFork(ref, forkName.length, forkName.unicode, permissions, &self->forkref);
75 Py_END_ALLOW_THREADS
76 if (err != noErr) {
77 Py_DECREF(self);
78 return (forkRefObject *) macos_error_for_call(err, "FSOpenFork", NULL);
79 }
80
81 return self;
82}
83
84//__________________________________________________________________________________________________
85// forkRefObject methods
86//
87static
88void forkRefObject_dealloc(forkRefObject *self)
89{
90 Py_BEGIN_ALLOW_THREADS
91 if (self->forkref != -1)
92 FSClose(self->forkref);
93 Py_END_ALLOW_THREADS
94 PyObject_Del(self);
95}
96
97//__________________________________________________________________________________________________
98static char forkRefObject_read__doc__[] =
99"read([bytecount[, posmode[, offset]]]) -> String\n\n\
100Read bytes from a fork, optionally passing number of\n\
101bytes (default: 128 bytes), posmode (below) and offset:\n\
102 0: ignore offset; write at current mark (default)\n\
103 1: offset relative to the start of fork\n\
104 2: offset relative to the end of fork\n\
105 3: offset relative to current fork position\n\
106\n\
107Returns a string containing the contents of the buffer";
108
109static
110PyObject *forkRefObject_read(forkRefObject *self, PyObject *args)
111{
112 OSErr err;
113 ByteCount request = 128, actual;
114 unsigned posmode = fsAtMark;
115 long tempoffset = 0;
116 SInt64 posoffset;
117 PyObject *buffer;
118
119 if (!PyArg_ParseTuple(args, "|lil:read", &request, &posmode, &tempoffset))
120 return NULL;
121
122 posoffset = tempoffset;
123 buffer = PyString_FromStringAndSize((char *)NULL, request);
124 if (buffer == NULL)
125 return NULL;
126
127 Py_BEGIN_ALLOW_THREADS
128 err = FSReadFork(self->forkref, posmode, posoffset, request, PyString_AsString(buffer), &actual);
129 Py_END_ALLOW_THREADS
130 if ((err != noErr) && (err != eofErr)) {
131 Py_DECREF(buffer);
132 return macos_error_for_call(err, "FSReadFork", NULL);
133 }
134
135 if (actual != request)
136 _PyString_Resize(&buffer, actual);
137
138 Py_INCREF(buffer);
139 return buffer;
140}
141
142//__________________________________________________________________________________________________
143static char forkRefObject_write__doc__[] =
144"write(buffer [bytecount[, posmode[, offset]]]) -> None\n\n\
145Write buffer to fork, optionally passing number of bytes, posmode (below) and offset:\n\
146 0: ignore offset; write at current mark (default)\n\
147 1: offset relative to the start of fork\n\
148 2: offset relative to the end of fork\n\
149 3: offset relative to current fork position";
150
151static
152PyObject *forkRefObject_write(forkRefObject *self, PyObject *args)
153{
154 OSErr err;
155 ByteCount request = -1, actual;
156 int size;
157 char *buffer;
158 unsigned posmode = fsAtMark;
159 long tempoffset = 0;
160 SInt64 posoffset;
161
162 if (!PyArg_ParseTuple(args, "s#|lil:write", &buffer, &size, &request, &posmode, &tempoffset))
163 return NULL;
164
165 posoffset = tempoffset;
166 if (request == -1)
167 request = size;
168
169 Py_BEGIN_ALLOW_THREADS
170 err = FSWriteFork(self->forkref, posmode, posoffset, request, buffer, &actual);
171 Py_END_ALLOW_THREADS
172 if (err)
173 return macos_error_for_call(err, "FSWriteFork", NULL);
174
175 Py_INCREF(Py_None);
176 return Py_None;
177}
178
179//__________________________________________________________________________________________________
180static char forkRefObject_close__doc__[] =
181"close() -> None\n\n\
182Close a reference to an open fork.";
183
184static
185PyObject *forkRefObject_close(forkRefObject *self, PyObject *args)
186{
187 OSErr err;
188
189 Py_BEGIN_ALLOW_THREADS
190 err = FSClose(self->forkref);
191 Py_END_ALLOW_THREADS
192 if (err)
193 return macos_error_for_call(err, "FSClose", NULL);
194
195 self->forkref = -1;
196
197 Py_INCREF(Py_None);
198 return Py_None;
199}
200
201//__________________________________________________________________________________________________
202static char forkRefObject_seek__doc__[] =
203"seek(offset [,posmode]) -> None\n\n\
204Set the current position in the fork with an optional posmode:\n\
205 1: offset relative to the start of fork (default)\n\
206 2: offset relative to the end of fork\n\
207 3: offset relative to current fork position";
208
209static
210PyObject *forkRefObject_seek(forkRefObject *self, PyObject *args)
211{
212 OSErr err;
213 unsigned posmode = fsFromStart;
214 long tempoffset = 0;
215 SInt64 posoffset;
216
217 if (!PyArg_ParseTuple(args, "l|l:seek", &tempoffset, &posmode))
218 return NULL;
219
220 posoffset = tempoffset;
221 Py_BEGIN_ALLOW_THREADS
222 err = FSSetForkPosition(self->forkref, posmode, posoffset);
223 Py_END_ALLOW_THREADS
224
225 if (err)
226 return macos_error_for_call(err, "FSSetForkPosition", NULL);
227
228 Py_INCREF(Py_None);
229 return Py_None;
230}
231
232//__________________________________________________________________________________________________
233static char forkRefObject_resize__doc__[] =
234"resize(offset [,posmode]) -> None\n\n\
235Set the fork size with an optional posmode:\n\
236 1: offset relative to the start of fork (default)\n\
237 2: offset relative to the end of fork\n\
238 3: offset relative to current fork position";
239
240static
241PyObject *forkRefObject_resize(forkRefObject *self, PyObject *args)
242{
243 OSErr err;
244 unsigned posmode = fsFromStart;
245 long tempoffset = 0;
246 SInt64 posoffset;
247
248 if (!PyArg_ParseTuple(args, "l|l:resize", &tempoffset, &posmode))
249 return NULL;
250
251 posoffset = tempoffset;
252 Py_BEGIN_ALLOW_THREADS
253 err = FSSetForkSize(self->forkref, posmode, posoffset);
254 Py_END_ALLOW_THREADS
255
256 if (err)
257 return macos_error_for_call(err, "FSSetForkSize", NULL);
258
259 Py_INCREF(Py_None);
260 return Py_None;
261}
262
263//__________________________________________________________________________________________________
264static char forkRefObject_tell__doc__[] =
265"tell() -> current position (Long)\n\n\
266Return the current position in the fork.";
267
268static
269PyObject *forkRefObject_tell(forkRefObject *self, PyObject *args)
270{
271 OSErr err;
272 SInt64 position;
273
274 Py_BEGIN_ALLOW_THREADS
275 err = FSGetForkPosition(self->forkref, &position);
276 Py_END_ALLOW_THREADS
277 if (err)
278 return macos_error_for_call(err, "FSGetForkPosition", NULL);
279
280 return PyLong_FromLongLong(position);
281}
282
283//__________________________________________________________________________________________________
284static char forkRefObject_length__doc__[] =
285"length() -> fork length (Long)\n\n\
286Return the logical length of the fork.";
287
288static
289PyObject *forkRefObject_length(forkRefObject *self, PyObject *args)
290{
291 OSErr err;
292 SInt64 size;
293
294 Py_BEGIN_ALLOW_THREADS
295 err = FSGetForkSize(self->forkref, &size);
296 Py_END_ALLOW_THREADS
297 if (err)
298 return macos_error_for_call(err, "FSGetForkSize", NULL);
299
300 return PyLong_FromLongLong(size);
301}
302
303//__________________________________________________________________________________________________
304
305static PyMethodDef forkRefObject_methods[] = {
306 {"read", (PyCFunction)forkRefObject_read,METH_VARARGS, forkRefObject_read__doc__},
307 {"write", (PyCFunction)forkRefObject_write,METH_VARARGS, forkRefObject_write__doc__},
308 {"close", (PyCFunction)forkRefObject_close,METH_VARARGS, forkRefObject_close__doc__},
309 {"seek", (PyCFunction)forkRefObject_seek,METH_VARARGS, forkRefObject_seek__doc__},
310 {"tell", (PyCFunction)forkRefObject_tell,METH_VARARGS, forkRefObject_tell__doc__},
311 {"length", (PyCFunction)forkRefObject_length,METH_VARARGS, forkRefObject_length__doc__},
312 {"resize", (PyCFunction)forkRefObject_resize,METH_VARARGS, forkRefObject_resize__doc__},
313 {NULL, NULL}
314};
315
316//__________________________________________________________________________________________________
317
318static
319PyObject *forkRefObject_getattr(forkRefObject *self, char *name)
320{
321 return Py_FindMethod(forkRefObject_methods, (PyObject *)self, name);
322}
323
324//__________________________________________________________________________________________________
325
326static int
327forkRefObject_print(forkRefObject *self, FILE *fp, int flags)
328{
329 fprintf(fp, "%d", self->forkref);
330 return 0;
331}
332
333//__________________________________________________________________________________________________
334
335statichere PyTypeObject forkRefObject_Type = {
336 /* The ob_type field must be initialized in the module init function
337 * to be portable to Windows without using C++. */
338 PyObject_HEAD_INIT(NULL)
339 0, /*ob_size*/
340 "openfile", /*tp_name*/
341 sizeof(forkRefObject), /*tp_basicsize*/
342 0, /*tp_itemsize*/
343 /* methods */
344 (destructor)forkRefObject_dealloc, /*tp_dealloc*/
345 (printfunc)forkRefObject_print, /*tp_print*/
346 (getattrfunc)forkRefObject_getattr, /*tp_getattr*/
347 0, /*tp_setattr*/
348 0, /*tp_compare*/
349 0, /*tp_repr*/
350 0, /*tp_as_number*/
351 0, /*tp_as_sequence*/
352 0, /*tp_as_mapping*/
353 0, /*tp_hash*/
354};
355
356
357
358//__________________________________________________________________________________________________
359//_______________________________________ ITERATOR OBJECT __________________________________________
360//__________________________________________________________________________________________________
361
362typedef struct {
363 PyObject_HEAD
364 PyObject *x_attr;
365 FSIterator iterator;
366} iteratorObject;
367
368staticforward PyTypeObject iteratorObject_Type;
369
370#define iteratorObject_Check(v) ((v)->ob_type == &iteratorObject_Type)
371
372static
373iteratorObject *newIteratorObject(PyObject *arg, FSRef *ref)
374{
375 OSErr err;
376 iteratorObject *self = PyObject_New(iteratorObject, &iteratorObject_Type);
377 if (self == NULL)
378 return NULL;
379
380 self->x_attr = NULL;
381 Py_BEGIN_ALLOW_THREADS
382 err = FSOpenIterator(ref, kFSIterateFlat, &self->iterator);
383 Py_END_ALLOW_THREADS
384 if (err != noErr) {
385 Py_DECREF(self);
386 return NULL;
387 }
388
389 return self;
390}
391
392//__________________________________________________________________________________________________
393// iteratorObject methods
394//
395static
396void iteratorObject_dealloc(iteratorObject *self)
397{
398 Py_XDECREF(self->x_attr);
399 FSCloseIterator(self->iterator);
400 PyObject_Del(self);
401}
402
403//__________________________________________________________________________________________________
404static char iteratorObject_listdir__doc__[] =
405"listdir([itemcount [, bitmap]]) -> Dict\n\n\
406Returns a dictionary of items and their attributes\n\
407for the given iterator and an optional bitmap describing\n\
408the attributes to be fetched (see CarbonCore/Files.h for\n\
409details of the bit definitions and key definitions).";
410
411static
412PyObject *iteratorObject_listdir(iteratorObject *self, PyObject *args)
413{
414 OSErr err;
415 int count = 500;
416 UInt32 actual;
417 FSCatalogInfoPtr infos;
418 HFSUniStr255 *unis;
419 PyObject *items;
420 unsigned i;
421 FSCatalogInfoBitmap bitmap = kFSCatInfoGettableInfo;
422
423 if (!PyArg_ParseTuple(args, "|il:listdir", &count, &bitmap))
424 return NULL;
425
426 items = PyList_New(0);
427 if (items == NULL)
428 return NULL;
429
430 infos = malloc(sizeof(FSCatalogInfo) * count);
431 if (infos == NULL)
432 return PyErr_NoMemory();
433
434 unis = malloc(sizeof(HFSUniStr255) * count);
435 if (unis == NULL) {
436 free(infos);
437 return PyErr_NoMemory();
438 }
439
440 err = FSGetCatalogInfoBulk(self->iterator, count, &actual, NULL, bitmap, infos, NULL, NULL, unis);
441
442 if (err == noErr || err == errFSNoMoreItems) {
443 for (i = 0; i < actual; i ++) {
444 PyObject *item;
445
446 item = dict_from_cataloginfo(bitmap, &infos[i], &unis[i]);
447 if (item == NULL)
448 continue;
449
450 if (PyList_Append(items, item) != 0) {
451 printf("ack! (PyList_Append)\n");
452 continue;
453 }
454 Py_DECREF(item);
455 }
456 }
457
458 free(infos);
459 free(unis);
460
461 Py_INCREF(items);
462 return items;
463}
464
465//__________________________________________________________________________________________________
466
467static PyMethodDef iteratorObject_methods[] = {
468 {"listdir", (PyCFunction)iteratorObject_listdir,METH_VARARGS, iteratorObject_listdir__doc__},
469 {NULL, NULL}
470};
471
472//__________________________________________________________________________________________________
473
474static
475PyObject *iteratorObject_getattr(iteratorObject *self, char *name)
476{
477 if (self->x_attr != NULL) {
478 PyObject *v = PyDict_GetItemString(self->x_attr, name);
479 if (v != NULL) {
480 Py_INCREF(v);
481 return v;
482 }
483 }
484 return Py_FindMethod(iteratorObject_methods, (PyObject *)self, name);
485}
486
487//__________________________________________________________________________________________________
488
489static
490int iteratorObject_setattr(iteratorObject *self, char *name, PyObject *v)
491{
492 if (self->x_attr == NULL) {
493 self->x_attr = PyDict_New();
494 if (self->x_attr == NULL)
495 return -1;
496 }
497 if (v == NULL) {
498 int rv = PyDict_DelItemString(self->x_attr, name);
499 if (rv < 0)
500 PyErr_SetString(PyExc_AttributeError, "delete non-existing iteratorObject attribute");
501 return rv;
502 }
503 else
504 return PyDict_SetItemString(self->x_attr, name, v);
505}
506
507//__________________________________________________________________________________________________
508
509statichere PyTypeObject iteratorObject_Type = {
510 /* The ob_type field must be initialized in the module init function
511 * to be portable to Windows without using C++. */
512 PyObject_HEAD_INIT(NULL)
513 0, /*ob_size*/
514 "iterator", /*tp_name*/
515 sizeof(iteratorObject), /*tp_basicsize*/
516 0, /*tp_itemsize*/
517 /* methods */
518 (destructor)iteratorObject_dealloc, /*tp_dealloc*/
519 0, /*tp_print*/
520 (getattrfunc)iteratorObject_getattr, /*tp_getattr*/
521 (setattrfunc)iteratorObject_setattr, /*tp_setattr*/
522 0, /*tp_compare*/
523 0, /*tp_repr*/
524 0, /*tp_as_number*/
525 0, /*tp_as_sequence*/
526 0, /*tp_as_mapping*/
527 0, /*tp_hash*/
528};
529
530
531//__________________________________________________________________________________________________
532//_________________________________________ FSREF OBJECT ___________________________________________
533//__________________________________________________________________________________________________
534
535typedef struct {
536 PyObject_HEAD
537 PyObject *x_attr;
538 FSRef ref;
539 int typeknown;
540 Boolean directory;
541} fsRefObject;
542
543staticforward PyTypeObject fsRefObject_Type;
544
545#define fsRefObject_Check(v) ((v)->ob_type == &fsRefObject_Type)
546
547static
548fsRefObject *newFSRefObject(PyObject *arg, FSRef *ref, Boolean typeknown, Boolean directory)
549{
550 fsRefObject *self = PyObject_New(fsRefObject, &fsRefObject_Type);
551 if (self == NULL)
552 return NULL;
553
554 self->x_attr = PyDict_New();
555 if (self->x_attr == NULL) {
556 Py_DECREF(self);
557 return NULL;
558 }
559
560 if (PyDict_SetItemString(self->x_attr, "directory", directory ? Py_True : Py_False) < 0) {
561 Py_XDECREF(self->x_attr);
562 Py_DECREF(self);
563 return NULL;
564 }
565
566 self->ref = *ref;
567 self->typeknown = typeknown;
568 self->directory = directory;
569 return self;
570}
571
572//__________________________________________________________________________________________________
573
574Boolean fsRefObject_isdir(fsRefObject *self)
575{
576 if (self->typeknown == false) {
577 self->directory = fsref_isdirectory(&self->ref);
578 self->typeknown = true;
579 }
580 return self->directory;
581}
582
583//__________________________________________________________________________________________________
584// fsRefObject methods
585//
586static
587void fsRefObject_dealloc(fsRefObject *self)
588{
589 Py_XDECREF(self->x_attr);
590 PyObject_Del(self);
591}
592
593//__________________________________________________________________________________________________
594static char fsRefObject_opendir__doc__[] =
595"opendir() -> iterator\n\n\
596Return an iterator";
597
598static
599PyObject *fsRefObject_opendir(fsRefObject *self, PyObject *args)
600{
601 iteratorObject *obj;
602
603 obj = newIteratorObject(args, &self->ref);
604 if (obj == NULL)
605 return NULL;
606 return (PyObject *) obj;
607}
608
609//__________________________________________________________________________________________________
610static char fsRefObject_namedfsref__doc__[] =
611"namedfsref(String) -> FSref\n\n\
612Return an FSRef for the named item in the container FSRef";
613
614static
615PyObject *fsRefObject_namedfsref(fsRefObject *self, PyObject *args)
616{
617 OSErr err;
618 fsRefObject *newObject;
619 FSRef newref;
620 HFSUniStr255 uni;
621 PyObject *namearg, *nameobj;
622
623 if (!PyArg_ParseTuple(args, "O|l:namedfsref", &namearg))
624 return NULL;
625
626 nameobj = obj_to_hfsunistr(namearg, &uni);
627 if (nameobj == NULL)
628 return NULL;
629 Py_DECREF(nameobj);
630
631 Py_BEGIN_ALLOW_THREADS
632 err = FSMakeFSRefUnicode(&self->ref, uni.length, uni.unicode, kTextEncodingUnknown, &newref);
633 Py_END_ALLOW_THREADS
634 if (err != noErr)
635 return macos_error_for_call(err, "FSMakeFSRefUnicode", NULL);
636
637 newObject = newFSRefObject(args, &newref, false, false);
638
639 Py_INCREF(newObject);
640 return (PyObject *) newObject;
641}
642
643//__________________________________________________________________________________________________
644static char fsRefObject_parent__doc__[] =
645"parent() -> FSref\n\n\
646Return an FSRef for the parent of this FSRef, or NULL (if this \n\
647is the root.)";
648
649static
650PyObject *fsRefObject_parent(fsRefObject *self, PyObject *args)
651{
652 OSErr err;
653 fsRefObject *newObject;
654 FSRef parentref;
655 FSCatalogInfo info;
656
657 Py_BEGIN_ALLOW_THREADS
658 err = FSGetCatalogInfo(&self->ref, kFSCatInfoParentDirID, &info, NULL, NULL, &parentref);
659 Py_END_ALLOW_THREADS
660 if (err != noErr)
661 return macos_error_for_call(err, "FSGetCatalogInfo", NULL);
662
663 if (info.parentDirID == fsRtParID)
664 return NULL;
665
666 newObject = newFSRefObject(args, &parentref, false, false);
667 Py_INCREF(newObject);
668 return (PyObject *) newObject;
669}
670
671//__________________________________________________________________________________________________
672static char fsRefObject_openfork__doc__[] =
673"openfork([resourcefork [,perms]]) -> forkRef\n\n\
674Return a forkRef object for reading/writing/etc. Optionally,\n\
675pass 1 for the resourcefork param to open the resource fork,\n\
676and permissions to open read-write or something else:\n\
6770: fsCurPerm\n\
6781: fsRdPerm (default)\n\
6792: fsWrPerm\n\
6803: fsRdWrPerm\n\
6814: fsRdWrShPerm";
682
683static
684PyObject *fsRefObject_openfork(fsRefObject *self, PyObject *args)
685{
686 forkRefObject *obj;
687 int resfork = 0, perms = fsRdPerm;
688
689 if (!PyArg_ParseTuple(args, "|ii:openfork", &resfork, &perms))
690 return NULL;
691
692 obj = newForkRefObject(args, &self->ref, resfork, perms);
693 if (obj == NULL)
694 return NULL;
695 return (PyObject *) obj;
696}
697
698//__________________________________________________________________________________________________
699
700static
701PyObject *fsRefObject_createcommon(Boolean createdir, fsRefObject *self, PyObject *args)
702{
703 OSErr err;
704 PyObject *nameobj, *namearg, *attrs = NULL;
705 FSRef newref;
706 fsRefObject *newObject;
707 HFSUniStr255 uni;
708 FSCatalogInfoBitmap bitmap = kFSCatInfoSettableInfo;
709 FSCatalogInfo info;
710
711 if (!PyArg_ParseTuple(args, "O|Ol", &namearg, &attrs, &bitmap))
712 return NULL;
713
714 nameobj = obj_to_hfsunistr(namearg, &uni);
715 if (nameobj == NULL)
716 return NULL;
717 Py_DECREF(nameobj);
718
719 if (attrs) {
720 if (!cataloginfo_from_dict(bitmap, &info, attrs))
721 return NULL;
722 } else {
723 bitmap = 0L;
724 }
725
726 if (createdir) {
727 Py_BEGIN_ALLOW_THREADS
728 err = FSCreateDirectoryUnicode(&self->ref, uni.length, uni.unicode, bitmap, &info, &newref, NULL, NULL);
729 Py_END_ALLOW_THREADS
730 if (err != noErr)
731 return macos_error_for_call(err, "FSCreateDirectoryUnicode", NULL);
732 } else {
733 Py_BEGIN_ALLOW_THREADS
734 err = FSCreateFileUnicode(&self->ref, uni.length, uni.unicode, bitmap, &info, &newref, NULL);
735 Py_END_ALLOW_THREADS
736 if (err != noErr)
737 return macos_error_for_call(err, "FSCreateFileUnicode", NULL);
738 }
739
740 newObject = newFSRefObject(args, &newref, true, createdir);
741 if (newObject == NULL)
742 return NULL;
743
744 Py_INCREF(newObject);
745 return (PyObject *) newObject;
746}
747
748//__________________________________________________________________________________________________
749static char fsRefObject_create__doc__[] =
750"create(name [,dict]) -> FSRef\n\n\
751Create a file in the specified directory with the given name\n\
752and return an FSRef object of the newly created item";
753
754static
755PyObject *fsRefObject_create(fsRefObject *self, PyObject *args)
756{
757 return fsRefObject_createcommon(false, self, args);
758}
759
760//__________________________________________________________________________________________________
761static char fsRefObject_mkdir__doc__[] =
762"mkdir(name [,dict]) -> FSRef\n\n\
763Create a directory in the specified directory with the given name\n\
764and return an FSRef object of the newly created item";
765
766static
767PyObject *fsRefObject_mkdir(fsRefObject *self, PyObject *args)
768{
769 return fsRefObject_createcommon(true, self, args);
770}
771
772//__________________________________________________________________________________________________
773static char fsRefObject_getcatinfo__doc__[] =
774"getcatinfo([bitmap]) -> Dict\n\n\
775Returns a dictionary of attributes for the given item\n\
776and an optional bitmap describing the attributes to be\n\
777fetched (see CarbonCore/Files.h for details of the bit\n\
778definitions and key definitions).";
779
780static
781PyObject *fsRefObject_getcatinfo(fsRefObject *self, PyObject *args)
782{
783 PyObject *dict;
784 OSErr err;
785 FSCatalogInfo info = {0};
786 HFSUniStr255 uni;
787 FSCatalogInfoBitmap bitmap = kFSCatInfoGettableInfo;
788
789 if (!PyArg_ParseTuple(args, "|l:getcatinfo", &bitmap))
790 return NULL;
791
792 Py_BEGIN_ALLOW_THREADS
793 err = FSGetCatalogInfo(&self->ref, bitmap, &info, &uni, NULL, NULL);
794 Py_END_ALLOW_THREADS
795 if (err != noErr)
796 return macos_error_for_call(err, "FSGetCatalogInfo", NULL);
797
798 dict = dict_from_cataloginfo(bitmap, &info, &uni);
799 if (dict == NULL)
800 return NULL;
801
802 Py_INCREF(dict);
803 return dict;
804}
805
806//__________________________________________________________________________________________________
807static char fsRefObject_setcatinfo__doc__[] =
808"setcatinfo(Dict [,bitmap]) -> None\n\n\
809Sets attributes for the given item. An optional\n\
810bitmap describing the attributes to be set can be\n\
811given as well (see CarbonCore/Files.h for details\n\
812of the bit definitions and key definitions).";
813
814static
815PyObject *fsRefObject_setcatinfo(fsRefObject *self, PyObject *args)
816{
817 PyObject *dict;
818 OSErr err;
819 FSCatalogInfo info = {0};
820 FSCatalogInfoBitmap bitmap = kFSCatInfoSettableInfo;
821
822 if (!PyArg_ParseTuple(args, "O|l:setcatinfo", &dict, &bitmap))
823 return NULL;
824
825 if (!cataloginfo_from_dict(bitmap, &info, dict)) return NULL;
826
827 Py_BEGIN_ALLOW_THREADS
828 err = FSSetCatalogInfo(&self->ref, bitmap, &info);
829 Py_END_ALLOW_THREADS
830 if (err != noErr)
831 return macos_error_for_call(err, "FSSetCatalogInfo", NULL);
832
833 Py_INCREF(Py_None);
834 return Py_None;
835}
836
837//__________________________________________________________________________________________________
838static char fsRefObject_delete__doc__[] =
839"delete() -> None\n\n\
840Delete the item";
841
842static
843PyObject *fsRefObject_delete(fsRefObject *self, PyObject *args)
844{
845 OSErr err;
846
847 Py_BEGIN_ALLOW_THREADS
848 err = FSDeleteObject(&self->ref);
849 Py_END_ALLOW_THREADS
850 if (err != noErr)
851 return macos_error_for_call(err, "FSDeleteObject", NULL);
852
853 Py_INCREF(Py_None);
854 return Py_None;
855}
856
857//__________________________________________________________________________________________________
858static char fsRefObject_exchange__doc__[] =
859"exchange(FSRef) -> None\n\n\
860Exchange this file with another.";
861
862static
863PyObject *fsRefObject_exchange(fsRefObject *self, PyObject *args)
864{
865 OSErr err;
866 fsRefObject *fsrefobj;
867
868 if (!PyArg_ParseTuple(args, "O|:exchange", &fsrefobj))
869 return NULL;
870
871 if (fsRefObject_Check(fsrefobj) == false)
872 return NULL;
873
874 Py_BEGIN_ALLOW_THREADS
875 err = FSExchangeObjects(&self->ref, &fsrefobj->ref);
876 Py_END_ALLOW_THREADS
877 if (err != noErr)
878 return macos_error_for_call(err, "FSExchangeObjects", NULL);
879
880 Py_INCREF(Py_None);
881 return Py_None;
882}
883
884//__________________________________________________________________________________________________
885static char fsRefObject_move__doc__[] =
886"move(FSRef) -> FSRef\n\n\
887Move the item to the container and return an FSRef to\n\
888the new item.";
889
890static
891PyObject *fsRefObject_move(fsRefObject *self, PyObject *args)
892{
893 OSErr err;
894 fsRefObject *newObject;
895 FSRef newref;
896 fsRefObject *fsrefobj;
897
898 if (!PyArg_ParseTuple(args, "O|:move", &fsrefobj))
899 return NULL;
900
901 if (fsRefObject_Check(fsrefobj) == false)
902 return NULL;
903
904 Py_BEGIN_ALLOW_THREADS
905 err = FSMoveObject(&self->ref, &fsrefobj->ref, &newref);
906 Py_END_ALLOW_THREADS
907 if (err != noErr)
908 return macos_error_for_call(err, "FSMoveObject", NULL);
909
910 newObject = newFSRefObject(args, &newref, false, false);
911 if (newObject == NULL)
912 return NULL;
913
914 Py_INCREF(newObject);
915 return (PyObject *) newObject;
916}
917
918//__________________________________________________________________________________________________
919static char fsRefObject_rename__doc__[] =
920"rename(String [,encoding]) -> FSRef\n\n\
921Rename the item to the new name and return an FSRef to\n\
922the new item.";
923
924static
925PyObject *fsRefObject_rename(fsRefObject *self, PyObject *args)
926{
927 OSErr err;
928 fsRefObject *newObject;
929 FSRef newref;
930 TextEncoding hint = kTextEncodingUnknown;
931 HFSUniStr255 uni;
932 PyObject *namearg, *nameobj;
933
934 if (!PyArg_ParseTuple(args, "O|l:rename", &namearg, &hint))
935 return NULL;
936
937 nameobj = obj_to_hfsunistr(namearg, &uni);
938 if (nameobj == NULL)
939 return NULL;
940 Py_DECREF(nameobj);
941
942 Py_BEGIN_ALLOW_THREADS
943 err = FSRenameUnicode(&self->ref, uni.length, uni.unicode, hint, &newref);
944 Py_END_ALLOW_THREADS
945 if (err != noErr)
946 return macos_error_for_call(err, "FSRenameUnicode", NULL);
947
948 newObject = newFSRefObject(args, &newref, false, false);
949 if (newObject == NULL)
950 return NULL;
951
952 Py_INCREF(newObject);
953 return (PyObject *) newObject;
954}
955
956
957//__________________________________________________________________________________________________
958
959static PyMethodDef fsRefObject_methods[] = {
960 {"getcatinfo", (PyCFunction)fsRefObject_getcatinfo,METH_VARARGS, fsRefObject_getcatinfo__doc__},
961 {"setcatinfo", (PyCFunction)fsRefObject_setcatinfo,METH_VARARGS, fsRefObject_setcatinfo__doc__},
962 {"create", (PyCFunction)fsRefObject_create, METH_VARARGS, fsRefObject_create__doc__},
963 {"mkdir", (PyCFunction)fsRefObject_mkdir, METH_VARARGS, fsRefObject_mkdir__doc__},
964 {"namedfsref", (PyCFunction)fsRefObject_namedfsref,METH_VARARGS, fsRefObject_namedfsref__doc__},
965 {"parent", (PyCFunction)fsRefObject_parent, METH_VARARGS, fsRefObject_parent__doc__},
966 {"delete", (PyCFunction)fsRefObject_delete, METH_VARARGS, fsRefObject_delete__doc__},
967 {"rename", (PyCFunction)fsRefObject_rename, METH_VARARGS, fsRefObject_rename__doc__},
968 {"move", (PyCFunction)fsRefObject_move, METH_VARARGS, fsRefObject_move__doc__},
969 {"exchange", (PyCFunction)fsRefObject_exchange, METH_VARARGS, fsRefObject_exchange__doc__},
970
971 {"opendir", (PyCFunction)fsRefObject_opendir, METH_VARARGS, fsRefObject_opendir__doc__},
972 {"openfork", (PyCFunction)fsRefObject_openfork, METH_VARARGS, fsRefObject_openfork__doc__},
973
974 {NULL, NULL}
975};
976
977//__________________________________________________________________________________________________
978
979static
980PyObject *fsRefObject_getattr(fsRefObject *self, char *name)
981{
982 if (self->x_attr != NULL) {
983 PyObject *v = PyDict_GetItemString(self->x_attr, name);
984 if (v != NULL) {
985 Py_INCREF(v);
986 return v;
987 }
988 }
989 return Py_FindMethod(fsRefObject_methods, (PyObject *)self, name);
990}
991
992//__________________________________________________________________________________________________
993
994static
995int fsRefObject_setattr(fsRefObject *self, char *name, PyObject *v)
996{
997 if (self->x_attr == NULL) {
998 self->x_attr = PyDict_New();
999 if (self->x_attr == NULL)
1000 return -1;
1001 }
1002 if (v == NULL) {
1003 int rv = PyDict_DelItemString(self->x_attr, name);
1004 if (rv < 0)
1005 PyErr_SetString(PyExc_AttributeError, "delete non-existing fsRefObject attribute");
1006 return rv;
1007 }
1008 else
1009 return PyDict_SetItemString(self->x_attr, name, v);
1010}
1011
1012//__________________________________________________________________________________________________
1013
1014statichere PyTypeObject fsRefObject_Type = {
1015 /* The ob_type field must be initialized in the module init function
1016 * to be portable to Windows without using C++. */
1017 PyObject_HEAD_INIT(NULL)
1018 0, /*ob_size*/
1019 "fsref", /*tp_name*/
1020 sizeof(fsRefObject), /*tp_basicsize*/
1021 0, /*tp_itemsize*/
1022 /* methods */
1023 (destructor)fsRefObject_dealloc, /*tp_dealloc*/
1024 0, /*tp_print*/
1025 (getattrfunc)fsRefObject_getattr, /*tp_getattr*/
1026 (setattrfunc)fsRefObject_setattr, /*tp_setattr*/
1027 0, /*tp_compare*/
1028 0, /*tp_repr*/
1029 0, /*tp_as_number*/
1030 0, /*tp_as_sequence*/
1031 0, /*tp_as_mapping*/
1032 0, /*tp_hash*/
1033};
1034
1035
1036
1037//__________________________________________________________________________________________________
1038//____________________________________ MODULE FUNCTIONS ____________________________________________
1039//__________________________________________________________________________________________________
1040static char fmgrmodule_getcatinfo__doc__[] =
1041"getcatinfo(path[,bitmap]) -> Dict\n\n\
1042Returns a dictionary of attributes for the given item\n\
1043and an optional bitmap describing the attributes to be\n\
1044fetched (see CarbonCore/Files.h for details of the bit\n\
1045definitions and key definitions).";
1046
1047static
1048PyObject *fmgrmodule_getcatinfo(PyObject *self, PyObject *args)
1049{
1050 char *path;
1051 PyObject *dict;
1052 FSRef ref;
1053 OSErr err;
1054 FSCatalogInfo info = {0};
1055 HFSUniStr255 uni;
1056 FSCatalogInfoBitmap bitmap = kFSCatInfoGettableInfo;
1057
1058 if (!PyArg_ParseTuple(args, "s|l", &path, &bitmap))
1059 return NULL;
1060
1061 Py_BEGIN_ALLOW_THREADS
1062 err = FSPathMakeRef(path, &ref, NULL);
1063 Py_END_ALLOW_THREADS
1064 if (err != noErr)
1065 return macos_error_for_call(err, "FSPathMakeRef", path);
1066
1067 Py_BEGIN_ALLOW_THREADS
1068 err = FSGetCatalogInfo(&ref, bitmap, &info, &uni, NULL, NULL);
1069 Py_END_ALLOW_THREADS
1070 if (err != noErr)
1071 return macos_error_for_call(err, "FSGetCatalogInfo", path);
1072
1073 dict = dict_from_cataloginfo(bitmap, &info, &uni);
1074 if (dict == NULL)
1075 return NULL;
1076
1077 Py_INCREF(dict);
1078 return dict;
1079}
1080
1081//__________________________________________________________________________________________________
1082static char fmgrmodule_opendir__doc__[] =
1083"opendir(path) -> iterator\n\n\
1084Return an iterator for listdir.";
1085
1086static
1087PyObject *fmgrmodule_opendir(PyObject *self, PyObject *args)
1088{
1089 char *path;
1090 iteratorObject *rv;
1091 FSRef ref;
1092 OSErr err;
1093 Boolean isdir;
1094
1095 if (!PyArg_ParseTuple(args, "s", &path))
1096 return NULL;
1097
1098 Py_BEGIN_ALLOW_THREADS
1099 err = FSPathMakeRef(path, &ref, &isdir);
1100 Py_END_ALLOW_THREADS
1101
1102 if (err != noErr)
1103 return macos_error_for_call(err, "FSPathMakeRef", path);
1104 else if (isdir == false)
1105 return PyErr_Format(PyExc_SyntaxError, "requires a directory");
1106
1107 rv = newIteratorObject(args, &ref);
1108 if (rv == NULL)
1109 return NULL;
1110 return (PyObject *) rv;
1111}
1112
1113//__________________________________________________________________________________________________
1114static char fmgrmodule_fsref__doc__[] =
1115"fsref(path) -> FSRef\n\n\
1116Return an FSRef object.";
1117
1118static
1119PyObject *fmgrmodule_fsref(PyObject *self, PyObject *args)
1120{
1121 char *path;
1122 fsRefObject *obj;
1123 FSRef ref;
1124 OSErr err;
1125 Boolean isdir;
1126
1127 if (!PyArg_ParseTuple(args, "s", &path))
1128 return NULL;
1129
1130 Py_BEGIN_ALLOW_THREADS
1131 err = FSPathMakeRef(path, &ref, &isdir);
1132 Py_END_ALLOW_THREADS
1133
1134 if (err != noErr)
1135 return macos_error_for_call(err, "FSPathMakeRef", path);
1136
1137 obj = newFSRefObject(args, &ref, true, isdir);
1138 if (obj == NULL)
1139 return NULL;
1140 return (PyObject *) obj;
1141}
1142
1143//__________________________________________________________________________________________________
1144static char fmgrmodule_openfork__doc__[] =
1145"openfork(path[,resourcefork[,perms]]) -> forkRef\n\n\
1146Return a forkRef object for reading/writing/etc. Optionally,\n\
1147pass 1 for the resourcefork param to open the resource fork,\n\
1148and permissions to open read-write or something else:\n\
11490: fsCurPerm\n\
11501: fsRdPerm\n\
11512: fsWrPerm\n\
11523: fsRdWrPerm\n\
11534: fsRdWrShPerm";
1154
1155static
1156PyObject *fmgrmodule_openfork(PyObject *self, PyObject *args)
1157{
1158 char *path;
1159 forkRefObject *rv;
1160 FSRef ref;
1161 OSErr err;
1162 Boolean isdir;
1163 int resfork = 0, perms = fsRdPerm;
1164
1165 if (!PyArg_ParseTuple(args, "s|ii", &path, &resfork, &perms))
1166 return NULL;
1167
1168 Py_BEGIN_ALLOW_THREADS
1169 err = FSPathMakeRef(path, &ref, &isdir);
1170 Py_END_ALLOW_THREADS
1171
1172 if (err != noErr) {
1173 return macos_error_for_call(err, "FSPathMakeRef", path);
1174 } else if (isdir == true) {
1175 return PyErr_Format(PyExc_SyntaxError, "requires a file");
1176 }
1177
1178 rv = newForkRefObject(args, &ref, resfork, perms);
1179 if (rv == NULL)
1180 return NULL;
1181 return (PyObject *) rv;
1182}
1183
1184//__________________________________________________________________________________________________
1185// List of functions defined in the module
1186//
1187static PyMethodDef fmgrmodule_methods[] = {
1188 {"getcatinfo", fmgrmodule_getcatinfo, METH_VARARGS, fmgrmodule_getcatinfo__doc__},
1189 {"opendir", fmgrmodule_opendir, METH_VARARGS, fmgrmodule_opendir__doc__},
1190 {"openfork", fmgrmodule_openfork, METH_VARARGS, fmgrmodule_openfork__doc__},
1191 {"fsref", fmgrmodule_fsref, METH_VARARGS, fmgrmodule_fsref__doc__},
1192 {NULL, NULL}
1193};
1194
1195//__________________________________________________________________________________________________
1196// Initialization function for the module (*must* be called initfmgr)
1197//
1198DL_EXPORT(void)
1199initfmgr(void)
1200{
1201 PyObject *m, *d;
1202
1203 /* Initialize the type of the new type object here; doing it here
1204 * is required for portability to Windows without requiring C++. */
1205 iteratorObject_Type.ob_type = &PyType_Type;
1206 forkRefObject_Type.ob_type = &PyType_Type;
1207 fsRefObject_Type.ob_type = &PyType_Type;
1208
1209 /* Create the module and add the functions */
1210 m = Py_InitModule("fmgr", fmgrmodule_methods);
1211
1212 /* Add some symbolic constants to the module */
1213 d = PyModule_GetDict(m);
1214 insert_int(d, "fsCurPerm", fsCurPerm);
1215 insert_int(d, "fsRdPerm", fsRdPerm);
1216 insert_int(d, "fsWrPerm", fsWrPerm);
1217 insert_int(d, "fsRdWrPerm", fsRdWrPerm);
1218 insert_int(d, "fsRdWrShPerm", fsRdWrShPerm);
1219 insert_int(d, "fsAtMark", fsAtMark);
1220 insert_int(d, "fsFromStart", fsFromStart);
1221 insert_int(d, "fsFromLEOF", fsFromLEOF);
1222 insert_int(d, "fsFromMark", fsFromMark);
1223 insert_int(d, "noCacheMask", noCacheMask);
1224 ErrorObject = PyErr_NewException("fmgr.error", NULL, NULL);
1225 PyDict_SetItemString(d, "error", ErrorObject);
1226}
1227
1228//__________________________________________________________________________________________________
1229//_________________________________________ UTILITIES ______________________________________________
1230//__________________________________________________________________________________________________
1231
1232static const char *_kFSCatInfoPrintableName = "pname";
1233static const char *_kFSCatInfoUnicodeName = "uname";
1234
1235static const char *_kFSCatInfoVolume = "volume";
1236static const char *_kFSCatInfoNodeID = "nodeid";
1237static const char *_kFSCatInfoParentDirID = "parid";
1238static const char *_kFSCatInfoTextEncoding = "encoding";
1239static const char *_kFSCatInfoNodeFlags = "flags";
1240
1241static const char *_kFSCatInfoDataLogical = "datalogicalsize";
1242static const char *_kFSCatInfoRsrcLogical = "rsrclogicalsize";
1243static const char *_kFSCatInfoDataPhysical = "dataphysicalsize";
1244static const char *_kFSCatInfoRsrcPhysical = "rsrcphysicalsize";
1245
1246static const char *_kFSCatInfoUserID = "uid";
1247static const char *_kFSCatInfoGroupID = "gid";
1248static const char *_kFSCatInfoUserAccess = "useraccess";
1249static const char *_kFSCatInfoMode = "mode";
1250
1251static const char *_kFSCatInfoFinderInfo = "finfo";
1252static const char *_kFSCatInfoFinderXInfo = "fxinfo";
1253
1254static const char *_kFSCatInfoCreateDate = "crdate";
1255static const char *_kFSCatInfoContentMod = "mddate";
1256static const char *_kFSCatInfoAttrMod = "amdate";
1257static const char *_kFSCatInfoAccessDate = "acdate";
1258static const char *_kFSCatInfoBackupDate = "bkdate";
1259
1260//__________________________________________________________________________________________________
1261
1262static
1263PyObject *macos_error_for_call(OSErr err, const char *name, const char *item)
1264{
1265 PyObject *v;
1266 char buffer[1024];
1267
1268 if (item)
1269 sprintf(buffer, "mac error calling %s on %s", name, item);
1270 else
1271 sprintf(buffer, "mac error calling %s", name);
1272
1273 v = Py_BuildValue("(is)", err, buffer);
1274 if (v != NULL) {
1275 PyErr_SetObject(PyExc_OSError, v);
1276 Py_DECREF(v);
1277 }
1278 return NULL;
1279}
1280//__________________________________________________________________________________________________
1281
1282static
1283int insert_slong(FSCatalogInfoBitmap bitmap, UInt32 bit, PyObject *d, const char *symbol, long value)
1284{
1285 if (bitmap & bit) {
1286 PyObject* v = PyLong_FromLong(value);
1287 if (!v || PyDict_SetItemString(d, (char *) symbol, v) < 0)
1288 return -1;
1289 Py_DECREF(v);
1290 }
1291 return 0;
1292}
1293
1294//__________________________________________________________________________________________________
1295
1296static
1297int insert_long(FSCatalogInfoBitmap bitmap, UInt32 bit, PyObject *d, const char *symbol, UInt32 value)
1298{
1299 if (bitmap & bit) {
1300 PyObject* v = PyLong_FromUnsignedLong(value);
1301 if (!v || PyDict_SetItemString(d, (char *) symbol, v) < 0)
1302 return -1;
1303 Py_DECREF(v);
1304 }
1305 return 0;
1306}
1307
1308//__________________________________________________________________________________________________
1309
1310static
1311int insert_int(PyObject *d, const char *symbol, int value)
1312{
1313 PyObject* v = PyInt_FromLong(value);
1314 if (!v || PyDict_SetItemString(d, (char *) symbol, v) < 0)
1315 return -1;
1316
1317 Py_DECREF(v);
1318 return 0;
1319}
1320
1321//__________________________________________________________________________________________________
1322
1323static
1324int insert_longlong(FSCatalogInfoBitmap bitmap, UInt32 bit, PyObject *d, const char *symbol, UInt64 value)
1325{
1326 if (bitmap & bit) {
1327 PyObject* v = PyLong_FromLongLong(value);
1328 if (!v || PyDict_SetItemString(d, (char *) symbol, v) < 0)
1329 return -1;
1330 Py_DECREF(v);
1331 }
1332 return 0;
1333}
1334
1335//__________________________________________________________________________________________________
1336
1337static
1338int insert_utcdatetime(FSCatalogInfoBitmap bitmap, UInt32 bit, PyObject *d, const char *symbol, const UTCDateTime *utc)
1339{
1340 if (bitmap & bit) {
1341 PyObject* tuple = Py_BuildValue("ili", utc->highSeconds, utc->lowSeconds, utc->fraction);
1342 if (!tuple || PyDict_SetItemString(d, (char *) symbol, tuple) < 0)
1343 return -1;
1344 Py_DECREF(tuple);
1345 }
1346 return 0;
1347}
1348
1349//__________________________________________________________________________________________________
1350
1351static
1352int fetch_long(FSCatalogInfoBitmap bitmap, UInt32 bit, const PyObject *d, const char *symbol, UInt32 *value)
1353{
1354 if (bitmap & bit) {
1355 PyObject* v = PyDict_GetItemString((PyObject *) d, (char *) symbol);
1356 if (v == NULL)
1357 return -1;
1358
1359 *value = PyLong_AsUnsignedLong(v);
1360 }
1361 return 0;
1362}
1363
1364//__________________________________________________________________________________________________
1365
1366static
1367int fetch_utcdatetime(FSCatalogInfoBitmap bitmap, UInt32 bit, const PyObject *d, const char *symbol, UTCDateTime *utc)
1368{
1369 if (bitmap & bit) {
1370 PyObject* tuple = PyDict_GetItemString((PyObject *) d, (char *) symbol);
1371 if (tuple == NULL)
1372 return -1;
1373
1374 if (!PyArg_ParseTuple(tuple, "ili", &utc->highSeconds, &utc->lowSeconds, &utc->fraction))
1375 return -1;
1376 }
1377
1378 return 0;
1379}
1380
1381//__________________________________________________________________________________________________
1382
1383static
1384void printableUniStr(const HFSUniStr255 *uni, char *buffer)
1385{
1386 int i;
1387 char localbuf[32];
1388
1389 buffer[0] = 0;
1390 for (i = 0; i < uni->length; i ++) {
1391 UniChar uch = uni->unicode[i];
1392
1393 if ((uch & 0x7f) == uch) {
1394 sprintf(localbuf, "%c", uch);
1395 } else {
1396 sprintf(localbuf, "\\u%04x", uch);
1397 }
1398 strcat(buffer, localbuf);
1399 }
1400}
1401
1402//__________________________________________________________________________________________________
1403
1404static
1405int cataloginfo_from_dict(FSCatalogInfoBitmap bitmap, FSCatalogInfo *info, const PyObject *dict)
1406{
1407 UInt32 storage;
1408 FSPermissionInfo *permissions;
1409
1410 // Dates
1411 if (fetch_utcdatetime(bitmap, kFSCatInfoCreateDate, dict, _kFSCatInfoCreateDate, &info->createDate)) return NULL;
1412 if (fetch_utcdatetime(bitmap, kFSCatInfoContentMod, dict, _kFSCatInfoContentMod, &info->contentModDate)) return NULL;
1413 if (fetch_utcdatetime(bitmap, kFSCatInfoAttrMod, dict, _kFSCatInfoAttrMod, &info->attributeModDate)) return NULL;
1414 if (fetch_utcdatetime(bitmap, kFSCatInfoAccessDate, dict, _kFSCatInfoAccessDate, &info->accessDate)) return NULL;
1415 if (fetch_utcdatetime(bitmap, kFSCatInfoBackupDate, dict, _kFSCatInfoBackupDate, &info->backupDate)) return NULL;
1416
1417 // Permissions
1418 permissions = (FSPermissionInfo *) info->permissions;
1419 if (fetch_long(bitmap, kFSCatInfoPermissions, dict, _kFSCatInfoUserID, &permissions->userID)) return NULL;
1420 if (fetch_long(bitmap, kFSCatInfoPermissions, dict, _kFSCatInfoGroupID, &permissions->groupID)) return NULL;
1421 if (fetch_long(bitmap, kFSCatInfoPermissions, dict, _kFSCatInfoMode, &storage)) return NULL;
1422 permissions->mode = (UInt16) storage;
1423 if (fetch_long(bitmap, kFSCatInfoPermissions, dict, _kFSCatInfoUserAccess, &storage)) return NULL;
1424 permissions->userAccess = (UInt8) storage;
1425
1426 // IDs
1427 if (fetch_long(bitmap, kFSCatInfoTextEncoding, dict, _kFSCatInfoTextEncoding, &info->textEncodingHint)) return NULL;
1428 if (fetch_long(bitmap, kFSCatInfoNodeFlags, dict, _kFSCatInfoNodeFlags, &storage)) return NULL;
1429 info->nodeFlags = (UInt16) storage;
1430
1431 // FinderInfo
1432 if (bitmap & kFSCatInfoFinderInfo) {
1433 PyObject *obj = PyDict_GetItemString((PyObject *) dict, (char *) _kFSCatInfoFinderInfo);
1434 if (obj == NULL)
1435 return NULL;
1436 BlockMoveData(PyString_AsString(obj), info->finderInfo, sizeof(FInfo));
1437 }
1438
1439 if (bitmap & kFSCatInfoFinderXInfo) {
1440 PyObject *obj = PyDict_GetItemString((PyObject *) dict, (char *) _kFSCatInfoFinderXInfo);
1441 if (obj == NULL)
1442 return NULL;
1443 BlockMoveData(PyString_AsString(obj), info->extFinderInfo, sizeof(FXInfo));
1444 }
1445
1446 return 1;
1447}
1448
1449//__________________________________________________________________________________________________
1450
1451static
1452PyObject *dict_from_cataloginfo(FSCatalogInfoBitmap bitmap, const FSCatalogInfo *info, HFSUniStr255 *uni)
1453{
1454 PyObject *dict;
1455 PyObject *id;
1456 FSPermissionInfo *permissions;
1457 char buffer[1024];
1458
1459 dict = PyDict_New();
1460 if (dict == NULL)
1461 return NULL;
1462
1463 // Name
1464 if (uni) {
1465 id = PyUnicode_FromUnicode(uni->unicode, uni->length);
1466 PyDict_SetItemString(dict, (char*) _kFSCatInfoUnicodeName, id); Py_XDECREF(id);
1467
1468 printableUniStr(uni, buffer);
1469 id = PyString_FromString(buffer);
1470 PyDict_SetItemString(dict, (char*) _kFSCatInfoPrintableName, id); Py_XDECREF(id);
1471 }
1472
1473 // IDs
1474 if (insert_slong(bitmap, kFSCatInfoVolume, dict, _kFSCatInfoVolume, info->volume)) return NULL;
1475 if (insert_long(bitmap, kFSCatInfoNodeID, dict, _kFSCatInfoNodeID, info->nodeID)) return NULL;
1476 if (insert_long(bitmap, kFSCatInfoParentDirID, dict, _kFSCatInfoParentDirID, info->parentDirID)) return NULL;
1477 if (insert_long(bitmap, kFSCatInfoTextEncoding, dict, _kFSCatInfoTextEncoding, info->textEncodingHint)) return NULL;
1478 if (insert_long(bitmap, kFSCatInfoNodeFlags, dict, _kFSCatInfoNodeFlags, info->nodeFlags)) return NULL;
1479
1480 // Sizes
1481 if (insert_longlong(bitmap, kFSCatInfoDataSizes, dict, _kFSCatInfoDataLogical, info->dataLogicalSize)) return NULL;
1482 if (insert_longlong(bitmap, kFSCatInfoDataSizes, dict, _kFSCatInfoDataPhysical, info->dataPhysicalSize)) return NULL;
1483 if (insert_longlong(bitmap, kFSCatInfoRsrcSizes, dict, _kFSCatInfoRsrcLogical, info->rsrcLogicalSize)) return NULL;
1484 if (insert_longlong(bitmap, kFSCatInfoRsrcSizes, dict, _kFSCatInfoRsrcPhysical, info->rsrcPhysicalSize)) return NULL;
1485
1486 // Permissions
1487 permissions = (FSPermissionInfo *) info->permissions;
1488 if (insert_long(bitmap, kFSCatInfoPermissions, dict, _kFSCatInfoUserID, permissions->userID)) return NULL;
1489 if (insert_long(bitmap, kFSCatInfoPermissions, dict, _kFSCatInfoGroupID, permissions->groupID)) return NULL;
1490 if (insert_long(bitmap, kFSCatInfoPermissions, dict, _kFSCatInfoUserAccess, permissions->userAccess)) return NULL;
1491 if (insert_long(bitmap, kFSCatInfoPermissions, dict, _kFSCatInfoMode, permissions->mode)) return NULL;
1492
1493 // Dates
1494 if (insert_utcdatetime(bitmap, kFSCatInfoCreateDate, dict, _kFSCatInfoCreateDate, &info->createDate)) return NULL;
1495 if (insert_utcdatetime(bitmap, kFSCatInfoContentMod, dict, _kFSCatInfoContentMod, &info->contentModDate)) return NULL;
1496 if (insert_utcdatetime(bitmap, kFSCatInfoAttrMod, dict, _kFSCatInfoAttrMod, &info->attributeModDate)) return NULL;
1497 if (insert_utcdatetime(bitmap, kFSCatInfoAccessDate, dict, _kFSCatInfoAccessDate, &info->accessDate)) return NULL;
1498 if (insert_utcdatetime(bitmap, kFSCatInfoBackupDate, dict, _kFSCatInfoBackupDate, &info->backupDate)) return NULL;
1499
1500 // FinderInfo
1501 if (bitmap & kFSCatInfoFinderInfo) {
1502 id = Py_BuildValue("s#", (char *) info->finderInfo, sizeof(FInfo));
1503 PyDict_SetItemString(dict, (char*) _kFSCatInfoFinderInfo, id); Py_XDECREF(id);
1504 }
1505 if (bitmap & kFSCatInfoFinderXInfo) {
1506 id = Py_BuildValue("s#", (char *) info->extFinderInfo, sizeof(FXInfo));
1507 PyDict_SetItemString(dict, (char*) _kFSCatInfoFinderXInfo, id); Py_XDECREF(id);
1508 }
1509
1510 return dict;
1511}
1512
1513//__________________________________________________________________________________________________
1514
1515static
1516Boolean fsref_isdirectory(const FSRef *ref)
1517{
1518 Boolean isdir = false;
1519 OSErr err;
1520 FSCatalogInfo info;
1521
1522 err = FSGetCatalogInfo(ref, kFSCatInfoNodeFlags, &info, NULL, NULL, NULL);
1523
1524 if (err == noErr)
1525 isdir = (info.nodeFlags & kioFlAttribDirMask);
1526
1527 return isdir;
1528}
1529
1530//__________________________________________________________________________________________________
1531
1532static
1533PyObject *obj_to_hfsunistr(PyObject *in, HFSUniStr255 *uni)
1534{
1535 PyObject *out;
1536
1537 out = PyUnicode_FromObject(in);
1538 if (out == NULL)
1539 return NULL;
1540
1541 BlockMoveData(PyUnicode_AS_UNICODE(out), uni->unicode, PyUnicode_GET_DATA_SIZE(out));
1542 uni->length = PyUnicode_GET_SIZE(out);
1543
1544 return out;
1545}
1546
1547