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