Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1 | /* |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 2 | ** Interface to hfs+ API. |
| 3 | ** Contributed by Nitin Ganatra. |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 7 | #include "Python.h" |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 8 | #include "pymactoolbox.h" |
Jack Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 9 | #ifdef WITHOUT_FRAMEWORKS |
| 10 | #include <Files.h> |
| 11 | #else |
| 12 | #include <CoreServices/CoreServices.h> |
| 13 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 14 | |
| 15 | static PyObject * |
| 16 | dict_from_cataloginfo(FSCatalogInfoBitmap bitmap, const FSCatalogInfo *info, HFSUniStr255 *uni); |
| 17 | static |
| 18 | PyObject *macos_error_for_call(OSErr err, const char *name, const char *item); |
| 19 | static |
| 20 | int insert_long(FSCatalogInfoBitmap bitmap, UInt32 bit, PyObject *dict, const char *symbol, UInt32 value); |
| 21 | static |
| 22 | int insert_slong(FSCatalogInfoBitmap bitmap, UInt32 bit, PyObject *dict, const char *symbol, long value); |
| 23 | static |
| 24 | int insert_int(PyObject *d, const char *symbol, int value); |
| 25 | static |
| 26 | Boolean fsref_isdirectory(const FSRef *ref); |
| 27 | static |
| 28 | PyObject *obj_to_hfsunistr(PyObject *in, HFSUniStr255 *uni); |
| 29 | |
| 30 | static |
| 31 | int cataloginfo_from_dict(FSCatalogInfoBitmap bitmap, FSCatalogInfo *info, const PyObject *dict); |
| 32 | |
| 33 | |
| 34 | static PyObject *ErrorObject; |
| 35 | |
| 36 | //__________________________________________________________________________________________________ |
| 37 | //_______________________________________ FORKREF OBJECT ___________________________________________ |
| 38 | //__________________________________________________________________________________________________ |
| 39 | |
| 40 | typedef struct { |
| 41 | PyObject_HEAD |
| 42 | PyObject *x_attr; |
| 43 | short forkref; |
| 44 | } forkRefObject; |
| 45 | |
| 46 | staticforward PyTypeObject forkRefObject_Type; |
| 47 | |
| 48 | #define forkRefObject_Check(v) ((v)->ob_type == &forkRefObject_Type) |
| 49 | |
| 50 | //__________________________________________________________________________________________________ |
| 51 | |
| 52 | static |
| 53 | forkRefObject *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 | // |
| 81 | static |
| 82 | void 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 | //__________________________________________________________________________________________________ |
| 92 | static char forkRefObject_read__doc__[] = |
| 93 | "read([bytecount[, posmode[, offset]]]) -> String\n\n\ |
| 94 | Read bytes from a fork, optionally passing number of\n\ |
| 95 | bytes (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\ |
| 101 | Returns a string containing the contents of the buffer"; |
| 102 | |
| 103 | static |
| 104 | PyObject *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 | //__________________________________________________________________________________________________ |
| 137 | static char forkRefObject_write__doc__[] = |
| 138 | "write(buffer [bytecount[, posmode[, offset]]]) -> None\n\n\ |
| 139 | Write 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 | |
| 145 | static |
| 146 | PyObject *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 | //__________________________________________________________________________________________________ |
| 174 | static char forkRefObject_close__doc__[] = |
| 175 | "close() -> None\n\n\ |
| 176 | Close a reference to an open fork."; |
| 177 | |
| 178 | static |
| 179 | PyObject *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 | //__________________________________________________________________________________________________ |
| 196 | static char forkRefObject_seek__doc__[] = |
| 197 | "seek(offset [,posmode]) -> None\n\n\ |
| 198 | Set 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 | |
| 203 | static |
| 204 | PyObject *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 | //__________________________________________________________________________________________________ |
| 227 | static char forkRefObject_resize__doc__[] = |
| 228 | "resize(offset [,posmode]) -> None\n\n\ |
| 229 | Set 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 | |
| 234 | static |
| 235 | PyObject *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 | //__________________________________________________________________________________________________ |
| 258 | static char forkRefObject_tell__doc__[] = |
| 259 | "tell() -> current position (Long)\n\n\ |
| 260 | Return the current position in the fork."; |
| 261 | |
| 262 | static |
| 263 | PyObject *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 | //__________________________________________________________________________________________________ |
| 278 | static char forkRefObject_length__doc__[] = |
| 279 | "length() -> fork length (Long)\n\n\ |
| 280 | Return the logical length of the fork."; |
| 281 | |
| 282 | static |
| 283 | PyObject *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 | |
| 299 | static 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 | |
| 312 | static |
| 313 | PyObject *forkRefObject_getattr(forkRefObject *self, char *name) |
| 314 | { |
| 315 | return Py_FindMethod(forkRefObject_methods, (PyObject *)self, name); |
| 316 | } |
| 317 | |
| 318 | //__________________________________________________________________________________________________ |
| 319 | |
| 320 | static int |
| 321 | forkRefObject_print(forkRefObject *self, FILE *fp, int flags) |
| 322 | { |
| 323 | fprintf(fp, "%d", self->forkref); |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | //__________________________________________________________________________________________________ |
| 328 | |
| 329 | statichere 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 Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 334 | "hfsplus.openfile", /*tp_name*/ |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 335 | 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 | |
| 356 | typedef struct { |
| 357 | PyObject_HEAD |
| 358 | PyObject *x_attr; |
| 359 | FSIterator iterator; |
| 360 | } iteratorObject; |
| 361 | |
| 362 | staticforward PyTypeObject iteratorObject_Type; |
| 363 | |
| 364 | #define iteratorObject_Check(v) ((v)->ob_type == &iteratorObject_Type) |
| 365 | |
| 366 | static |
| 367 | iteratorObject *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 | // |
| 389 | static |
| 390 | void iteratorObject_dealloc(iteratorObject *self) |
| 391 | { |
| 392 | Py_XDECREF(self->x_attr); |
| 393 | FSCloseIterator(self->iterator); |
| 394 | PyObject_Del(self); |
| 395 | } |
| 396 | |
| 397 | //__________________________________________________________________________________________________ |
| 398 | static char iteratorObject_listdir__doc__[] = |
| 399 | "listdir([itemcount [, bitmap]]) -> Dict\n\n\ |
| 400 | Returns a dictionary of items and their attributes\n\ |
| 401 | for the given iterator and an optional bitmap describing\n\ |
| 402 | the attributes to be fetched (see CarbonCore/Files.h for\n\ |
| 403 | details of the bit definitions and key definitions)."; |
| 404 | |
| 405 | static |
| 406 | PyObject *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 | |
| 461 | static PyMethodDef iteratorObject_methods[] = { |
| 462 | {"listdir", (PyCFunction)iteratorObject_listdir,METH_VARARGS, iteratorObject_listdir__doc__}, |
| 463 | {NULL, NULL} |
| 464 | }; |
| 465 | |
| 466 | //__________________________________________________________________________________________________ |
| 467 | |
| 468 | static |
| 469 | PyObject *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 | |
| 483 | static |
| 484 | int 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 | |
| 503 | statichere 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 Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 508 | "hfsplus.iterator", /*tp_name*/ |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 509 | 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 | |
| 529 | typedef struct { |
| 530 | PyObject_HEAD |
| 531 | PyObject *x_attr; |
| 532 | FSRef ref; |
| 533 | int typeknown; |
| 534 | Boolean directory; |
| 535 | } fsRefObject; |
| 536 | |
| 537 | staticforward PyTypeObject fsRefObject_Type; |
| 538 | |
| 539 | #define fsRefObject_Check(v) ((v)->ob_type == &fsRefObject_Type) |
| 540 | |
| 541 | static |
| 542 | fsRefObject *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 | |
| 568 | Boolean 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 | // |
| 580 | static |
| 581 | void fsRefObject_dealloc(fsRefObject *self) |
| 582 | { |
| 583 | Py_XDECREF(self->x_attr); |
| 584 | PyObject_Del(self); |
| 585 | } |
| 586 | |
| 587 | //__________________________________________________________________________________________________ |
| 588 | static char fsRefObject_opendir__doc__[] = |
| 589 | "opendir() -> iterator\n\n\ |
| 590 | Return an iterator"; |
| 591 | |
| 592 | static |
| 593 | PyObject *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 | //__________________________________________________________________________________________________ |
| 604 | static char fsRefObject_namedfsref__doc__[] = |
| 605 | "namedfsref(String) -> FSref\n\n\ |
| 606 | Return an FSRef for the named item in the container FSRef"; |
| 607 | |
| 608 | static |
| 609 | PyObject *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 | //__________________________________________________________________________________________________ |
| 638 | static char fsRefObject_parent__doc__[] = |
| 639 | "parent() -> FSref\n\n\ |
| 640 | Return an FSRef for the parent of this FSRef, or NULL (if this \n\ |
| 641 | is the root.)"; |
| 642 | |
| 643 | static |
| 644 | PyObject *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 Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 666 | static char fsRefObject_as_fsref__doc__[] = |
| 667 | "as_fsref() -> macfs.fsref\n\n\ |
| 668 | Return a macfs.fsref-style object from an hfsplus.fsref style object"; |
| 669 | |
| 670 | static |
| 671 | PyObject *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 Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 679 | static char fsRefObject_openfork__doc__[] = |
| 680 | "openfork([resourcefork [,perms]]) -> forkRef\n\n\ |
| 681 | Return a forkRef object for reading/writing/etc. Optionally,\n\ |
| 682 | pass 1 for the resourcefork param to open the resource fork,\n\ |
| 683 | and permissions to open read-write or something else:\n\ |
| 684 | 0: fsCurPerm\n\ |
| 685 | 1: fsRdPerm (default)\n\ |
| 686 | 2: fsWrPerm\n\ |
| 687 | 3: fsRdWrPerm\n\ |
| 688 | 4: fsRdWrShPerm"; |
| 689 | |
| 690 | static |
| 691 | PyObject *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 | |
| 707 | static |
| 708 | PyObject *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 | //__________________________________________________________________________________________________ |
| 756 | static char fsRefObject_create__doc__[] = |
| 757 | "create(name [,dict]) -> FSRef\n\n\ |
| 758 | Create a file in the specified directory with the given name\n\ |
| 759 | and return an FSRef object of the newly created item"; |
| 760 | |
| 761 | static |
| 762 | PyObject *fsRefObject_create(fsRefObject *self, PyObject *args) |
| 763 | { |
| 764 | return fsRefObject_createcommon(false, self, args); |
| 765 | } |
| 766 | |
| 767 | //__________________________________________________________________________________________________ |
| 768 | static char fsRefObject_mkdir__doc__[] = |
| 769 | "mkdir(name [,dict]) -> FSRef\n\n\ |
| 770 | Create a directory in the specified directory with the given name\n\ |
| 771 | and return an FSRef object of the newly created item"; |
| 772 | |
| 773 | static |
| 774 | PyObject *fsRefObject_mkdir(fsRefObject *self, PyObject *args) |
| 775 | { |
| 776 | return fsRefObject_createcommon(true, self, args); |
| 777 | } |
| 778 | |
| 779 | //__________________________________________________________________________________________________ |
| 780 | static char fsRefObject_getcatinfo__doc__[] = |
| 781 | "getcatinfo([bitmap]) -> Dict\n\n\ |
| 782 | Returns a dictionary of attributes for the given item\n\ |
| 783 | and an optional bitmap describing the attributes to be\n\ |
| 784 | fetched (see CarbonCore/Files.h for details of the bit\n\ |
| 785 | definitions and key definitions)."; |
| 786 | |
| 787 | static |
| 788 | PyObject *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 | //__________________________________________________________________________________________________ |
| 814 | static char fsRefObject_setcatinfo__doc__[] = |
| 815 | "setcatinfo(Dict [,bitmap]) -> None\n\n\ |
| 816 | Sets attributes for the given item. An optional\n\ |
| 817 | bitmap describing the attributes to be set can be\n\ |
| 818 | given as well (see CarbonCore/Files.h for details\n\ |
| 819 | of the bit definitions and key definitions)."; |
| 820 | |
| 821 | static |
| 822 | PyObject *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 | //__________________________________________________________________________________________________ |
| 845 | static char fsRefObject_delete__doc__[] = |
| 846 | "delete() -> None\n\n\ |
| 847 | Delete the item"; |
| 848 | |
| 849 | static |
| 850 | PyObject *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 | //__________________________________________________________________________________________________ |
| 865 | static char fsRefObject_exchange__doc__[] = |
| 866 | "exchange(FSRef) -> None\n\n\ |
| 867 | Exchange this file with another."; |
| 868 | |
| 869 | static |
| 870 | PyObject *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 | //__________________________________________________________________________________________________ |
| 892 | static char fsRefObject_move__doc__[] = |
| 893 | "move(FSRef) -> FSRef\n\n\ |
| 894 | Move the item to the container and return an FSRef to\n\ |
| 895 | the new item."; |
| 896 | |
| 897 | static |
| 898 | PyObject *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 | //__________________________________________________________________________________________________ |
| 926 | static char fsRefObject_rename__doc__[] = |
| 927 | "rename(String [,encoding]) -> FSRef\n\n\ |
| 928 | Rename the item to the new name and return an FSRef to\n\ |
| 929 | the new item."; |
| 930 | |
| 931 | static |
| 932 | PyObject *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 | |
| 966 | static 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 Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 981 | {"as_fsref", (PyCFunction)fsRefObject_as_fsref, METH_VARARGS, fsRefObject_as_fsref__doc__}, |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 982 | {NULL, NULL} |
| 983 | }; |
| 984 | |
| 985 | //__________________________________________________________________________________________________ |
| 986 | |
| 987 | static |
| 988 | PyObject *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 | |
| 1002 | static |
| 1003 | int 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 | |
| 1022 | statichere 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 Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1027 | "hfsplus.fsref", /*tp_name*/ |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1028 | 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 Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1048 | static char hfsplusmodule_getcatinfo__doc__[] = |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1049 | "getcatinfo(path[,bitmap]) -> Dict\n\n\ |
| 1050 | Returns a dictionary of attributes for the given item\n\ |
| 1051 | and an optional bitmap describing the attributes to be\n\ |
| 1052 | fetched (see CarbonCore/Files.h for details of the bit\n\ |
| 1053 | definitions and key definitions)."; |
| 1054 | |
| 1055 | static |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1056 | PyObject *hfsplusmodule_getcatinfo(PyObject *self, PyObject *args) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1057 | { |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1058 | PyObject *dict; |
| 1059 | FSRef ref; |
| 1060 | OSErr err; |
| 1061 | FSCatalogInfo info = {0}; |
| 1062 | HFSUniStr255 uni; |
| 1063 | FSCatalogInfoBitmap bitmap = kFSCatInfoGettableInfo; |
| 1064 | |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1065 | if (!PyArg_ParseTuple(args, "O&|l", PyMac_GetFSRef, &ref, &bitmap)) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1066 | return NULL; |
| 1067 | |
| 1068 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1069 | err = FSGetCatalogInfo(&ref, bitmap, &info, &uni, NULL, NULL); |
| 1070 | Py_END_ALLOW_THREADS |
| 1071 | if (err != noErr) |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1072 | return macos_error_for_call(err, "FSGetCatalogInfo", NULL); |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1073 | |
| 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 Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1083 | static char hfsplusmodule_opendir__doc__[] = |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1084 | "opendir(path) -> iterator\n\n\ |
| 1085 | Return an iterator for listdir."; |
| 1086 | |
| 1087 | static |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1088 | PyObject *hfsplusmodule_opendir(PyObject *self, PyObject *args) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1089 | { |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1090 | iteratorObject *rv; |
| 1091 | FSRef ref; |
| 1092 | OSErr err; |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1093 | #if 0 |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1094 | Boolean isdir; |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1095 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1096 | |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1097 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &ref)) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1098 | return NULL; |
| 1099 | |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1100 | #if 0 |
| 1101 | if (isdir == false) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1102 | return PyErr_Format(PyExc_SyntaxError, "requires a directory"); |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1103 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1104 | |
| 1105 | rv = newIteratorObject(args, &ref); |
| 1106 | if (rv == NULL) |
| 1107 | return NULL; |
| 1108 | return (PyObject *) rv; |
| 1109 | } |
| 1110 | |
| 1111 | //__________________________________________________________________________________________________ |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1112 | static char hfsplusmodule_fsref__doc__[] = |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1113 | "fsref(path) -> FSRef\n\n\ |
| 1114 | Return an FSRef object."; |
| 1115 | |
| 1116 | static |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1117 | PyObject *hfsplusmodule_fsref(PyObject *self, PyObject *args) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1118 | { |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1119 | fsRefObject *obj; |
| 1120 | FSRef ref; |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1121 | Boolean isdir = 0; |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1122 | |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1123 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &ref)) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1124 | return NULL; |
| 1125 | |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1126 | obj = newFSRefObject(args, &ref, true, isdir); |
| 1127 | if (obj == NULL) |
| 1128 | return NULL; |
| 1129 | return (PyObject *) obj; |
| 1130 | } |
| 1131 | |
| 1132 | //__________________________________________________________________________________________________ |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1133 | static char hfsplusmodule_openfork__doc__[] = |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1134 | "openfork(path[,resourcefork[,perms]]) -> forkRef\n\n\ |
| 1135 | Return a forkRef object for reading/writing/etc. Optionally,\n\ |
| 1136 | pass 1 for the resourcefork param to open the resource fork,\n\ |
| 1137 | and permissions to open read-write or something else:\n\ |
| 1138 | 0: fsCurPerm\n\ |
| 1139 | 1: fsRdPerm\n\ |
| 1140 | 2: fsWrPerm\n\ |
| 1141 | 3: fsRdWrPerm\n\ |
| 1142 | 4: fsRdWrShPerm"; |
| 1143 | |
| 1144 | static |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1145 | PyObject *hfsplusmodule_openfork(PyObject *self, PyObject *args) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1146 | { |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1147 | forkRefObject *rv; |
| 1148 | FSRef ref; |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1149 | #if 0 |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1150 | Boolean isdir; |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1151 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1152 | int resfork = 0, perms = fsRdPerm; |
| 1153 | |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1154 | if (!PyArg_ParseTuple(args, "s|ii", PyMac_GetFSRef, &ref, &resfork, &perms)) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1155 | return NULL; |
| 1156 | |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1157 | #if 0 |
| 1158 | if (isdir == true) { |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1159 | return PyErr_Format(PyExc_SyntaxError, "requires a file"); |
| 1160 | } |
Jack Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1161 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1162 | |
| 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 Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1172 | static 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 Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1177 | {NULL, NULL} |
| 1178 | }; |
| 1179 | |
| 1180 | //__________________________________________________________________________________________________ |
Jack Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1181 | // Initialization function for the module (*must* be called inithfsplus) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1182 | // |
| 1183 | DL_EXPORT(void) |
Jack Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1184 | inithfsplus(void) |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1185 | { |
| 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 Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1195 | m = Py_InitModule("hfsplus", hfsplusmodule_methods); |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1196 | |
| 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 Jansen | 5cc6d6e | 2001-11-06 15:57:59 +0000 | [diff] [blame] | 1209 | ErrorObject = PyErr_NewException("hfsplus.error", NULL, NULL); |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1210 | PyDict_SetItemString(d, "error", ErrorObject); |
| 1211 | } |
| 1212 | |
| 1213 | //__________________________________________________________________________________________________ |
| 1214 | //_________________________________________ UTILITIES ______________________________________________ |
| 1215 | //__________________________________________________________________________________________________ |
| 1216 | |
| 1217 | static const char *_kFSCatInfoPrintableName = "pname"; |
| 1218 | static const char *_kFSCatInfoUnicodeName = "uname"; |
| 1219 | |
| 1220 | static const char *_kFSCatInfoVolume = "volume"; |
| 1221 | static const char *_kFSCatInfoNodeID = "nodeid"; |
| 1222 | static const char *_kFSCatInfoParentDirID = "parid"; |
| 1223 | static const char *_kFSCatInfoTextEncoding = "encoding"; |
| 1224 | static const char *_kFSCatInfoNodeFlags = "flags"; |
| 1225 | |
| 1226 | static const char *_kFSCatInfoDataLogical = "datalogicalsize"; |
| 1227 | static const char *_kFSCatInfoRsrcLogical = "rsrclogicalsize"; |
| 1228 | static const char *_kFSCatInfoDataPhysical = "dataphysicalsize"; |
| 1229 | static const char *_kFSCatInfoRsrcPhysical = "rsrcphysicalsize"; |
| 1230 | |
| 1231 | static const char *_kFSCatInfoUserID = "uid"; |
| 1232 | static const char *_kFSCatInfoGroupID = "gid"; |
| 1233 | static const char *_kFSCatInfoUserAccess = "useraccess"; |
| 1234 | static const char *_kFSCatInfoMode = "mode"; |
| 1235 | |
| 1236 | static const char *_kFSCatInfoFinderInfo = "finfo"; |
| 1237 | static const char *_kFSCatInfoFinderXInfo = "fxinfo"; |
| 1238 | |
| 1239 | static const char *_kFSCatInfoCreateDate = "crdate"; |
| 1240 | static const char *_kFSCatInfoContentMod = "mddate"; |
| 1241 | static const char *_kFSCatInfoAttrMod = "amdate"; |
| 1242 | static const char *_kFSCatInfoAccessDate = "acdate"; |
| 1243 | static const char *_kFSCatInfoBackupDate = "bkdate"; |
| 1244 | |
| 1245 | //__________________________________________________________________________________________________ |
| 1246 | |
| 1247 | static |
| 1248 | PyObject *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 Jansen | 101de91 | 2001-12-05 23:27:58 +0000 | [diff] [blame] | 1254 | PyOS_snprintf(buffer, sizeof(buffer), "mac error calling %s on %s", name, item); |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1255 | else |
Jack Jansen | 101de91 | 2001-12-05 23:27:58 +0000 | [diff] [blame] | 1256 | PyOS_snprintf(buffer, sizeof(buffer), "mac error calling %s", name); |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1257 | |
| 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 | |
| 1267 | static |
| 1268 | int 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 | |
| 1281 | static |
| 1282 | int 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 | |
| 1295 | static |
| 1296 | int 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 | |
| 1308 | static |
| 1309 | int 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 | |
| 1322 | static |
| 1323 | int 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 | |
| 1336 | static |
| 1337 | int 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 | |
| 1351 | static |
| 1352 | int 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 | |
| 1368 | static |
| 1369 | void 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 Jansen | 101de91 | 2001-12-05 23:27:58 +0000 | [diff] [blame] | 1379 | PyOS_snprintf(localbuf, sizeof(localbuf), "%c", uch); |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1380 | } else { |
Jack Jansen | 101de91 | 2001-12-05 23:27:58 +0000 | [diff] [blame] | 1381 | PyOS_snprintf(localbuf, sizeof(localbuf), "\\u%04x", uch); |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1382 | } |
| 1383 | strcat(buffer, localbuf); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | //__________________________________________________________________________________________________ |
| 1388 | |
| 1389 | static |
| 1390 | int cataloginfo_from_dict(FSCatalogInfoBitmap bitmap, FSCatalogInfo *info, const PyObject *dict) |
| 1391 | { |
| 1392 | UInt32 storage; |
Jack Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1393 | #if UNIVERSAL_INTERFACES_VERSION > 0x0332 |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1394 | FSPermissionInfo *permissions; |
Jack Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1395 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1396 | |
| 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 Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1404 | #if UNIVERSAL_INTERFACES_VERSION > 0x0332 |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1405 | // 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 Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1413 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1414 | // 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 | |
| 1439 | static |
| 1440 | PyObject *dict_from_cataloginfo(FSCatalogInfoBitmap bitmap, const FSCatalogInfo *info, HFSUniStr255 *uni) |
| 1441 | { |
| 1442 | PyObject *dict; |
| 1443 | PyObject *id; |
Jack Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1444 | #if UNIVERSAL_INTERFACES_VERSION > 0x0332 |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1445 | FSPermissionInfo *permissions; |
Jack Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1446 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1447 | 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 Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1476 | #if UNIVERSAL_INTERFACES_VERSION > 0x0332 |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1477 | // 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 Jansen | 24aa3ce | 2001-11-06 12:06:39 +0000 | [diff] [blame] | 1483 | #endif |
Jack Jansen | e9b2a05 | 2001-11-06 11:10:13 +0000 | [diff] [blame] | 1484 | |
| 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 | |
| 1507 | static |
| 1508 | Boolean 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 | |
| 1524 | static |
| 1525 | PyObject *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 | |