| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2012 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmfilnam.cpp - CVmObjFileName object |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 03/03/12 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmfilnam.h" |
| 29 | #include "vmerr.h" |
| 30 | #include "vmerrnum.h" |
| 31 | #include "vmtype.h" |
| 32 | #include "vmglob.h" |
| 33 | #include "vmobj.h" |
| 34 | #include "vmundo.h" |
| 35 | #include "vmmcreg.h" |
| 36 | #include "vmfile.h" |
| 37 | #include "vmfilobj.h" |
| 38 | #include "vmbif.h" |
| 39 | #include "vmmeta.h" |
| 40 | #include "vmstack.h" |
| 41 | #include "vmrun.h" |
| 42 | #include "vmstr.h" |
| 43 | #include "utf8.h" |
| 44 | #include "charmap.h" |
| 45 | #include "vmnetfil.h" |
| 46 | #include "vmpredef.h" |
| 47 | |
| 48 | |
| 49 | /* ------------------------------------------------------------------------ */ |
| 50 | /* |
| 51 | * Allocate an extension structure |
| 52 | */ |
| 53 | vm_filnam_ext *vm_filnam_ext::alloc_ext( |
| 54 | VMG_ CVmObjFileName *self, int32_t sfid, const char *str, size_t len) |
| 55 | { |
| 56 | /* |
| 57 | * Calculate how much space we need - we need space for the name string |
| 58 | * plus its length prefix; store the name string with a null terminator |
| 59 | * for convenience in calling osifc routines. |
| 60 | */ |
| 61 | size_t siz = (sizeof(vm_filnam_ext)-1) + (VMB_LEN + len + 1); |
| 62 | |
| 63 | /* allocate the memory */ |
| 64 | vm_filnam_ext *ext = (vm_filnam_ext *)G_mem->get_var_heap()->alloc_mem( |
| 65 | siz, self); |
| 66 | |
| 67 | /* store the special file ID; assume it's valid */ |
| 68 | ext->sfid = sfid; |
| 69 | ext->sfid_valid = TRUE; |
| 70 | |
| 71 | /* assume that the file didn't come from a manual user interaction */ |
| 72 | ext->from_ui = 0; |
| 73 | |
| 74 | /* set the string length and null terminator */ |
| 75 | vmb_put_len(ext->str, len); |
| 76 | ext->str[VMB_LEN + len] = '\0'; |
| 77 | |
| 78 | /* copy the string, if provided */ |
| 79 | if (str != 0) |
| 80 | memcpy(ext->str + VMB_LEN, str, len); |
| 81 | |
| 82 | /* return the new extension */ |
| 83 | return ext; |
| 84 | } |
| 85 | |
| 86 | /* ------------------------------------------------------------------------ */ |
| 87 | /* |
| 88 | * CVmObjFileName object statics |
| 89 | */ |
| 90 | |
| 91 | /* metaclass registration object */ |
| 92 | static CVmMetaclassFileName metaclass_reg_obj; |
| 93 | CVmMetaclass *CVmObjFileName::metaclass_reg_ = &metaclass_reg_obj; |
| 94 | |
| 95 | /* function table */ |
| 96 | int (CVmObjFileName::*CVmObjFileName::func_table_[])( |
| 97 | VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc) = |
| 98 | { |
| 99 | &CVmObjFileName::getp_undef, /* 0 */ |
| 100 | &CVmObjFileName::getp_getName, /* 1 */ |
| 101 | &CVmObjFileName::getp_getRootName, /* 2 */ |
| 102 | &CVmObjFileName::getp_getPath, /* 3 */ |
| 103 | &CVmObjFileName::getp_fromUniversal, /* 4 */ |
| 104 | &CVmObjFileName::getp_toUniversal, /* 5 */ |
| 105 | &CVmObjFileName::getp_addToPath, /* 6 */ |
| 106 | &CVmObjFileName::getp_isAbsolute, /* 7 */ |
| 107 | &CVmObjFileName::getp_getAbsolutePath, /* 8 */ |
| 108 | &CVmObjFileName::getp_getRootDirs, /* 9 */ |
| 109 | &CVmObjFileName::getp_getFileType, /* 10 */ |
| 110 | &CVmObjFileName::getp_getFileInfo, /* 11 */ |
| 111 | &CVmObjFileName::getp_deleteFile, /* 12 */ |
| 112 | &CVmObjFileName::getp_renameFile, /* 13 */ |
| 113 | &CVmObjFileName::getp_listDir, /* 14 */ |
| 114 | &CVmObjFileName::getp_forEachFile, /* 15 */ |
| 115 | &CVmObjFileName::getp_createDirectory, /* 16 */ |
| 116 | &CVmObjFileName::getp_removeDirectory /* 17 */ |
| 117 | }; |
| 118 | |
| 119 | /* property indices */ |
| 120 | const int VMOFN_FROMUNIVERSAL = 4; |
| 121 | const int VMOFN_GETROOTDIRS = 9; |
| 122 | const int VMOFN_GETFILETYPE = 10; |
| 123 | const int VMOFN_GETFILEINFO = 11; |
| 124 | const int VMOFN_DELETEFILE = 12; |
| 125 | const int VMOFN_RENAMEFILE = 13; |
| 126 | const int VMOFN_LISTDIR = 14; |
| 127 | const int VMOFN_FOREACHFILE = 15; |
| 128 | const int VMOFN_CREATEDIR = 16; |
| 129 | const int VMOFN_REMOVEDIR = 17; |
| 130 | |
| 131 | |
| 132 | |
| 133 | /* ------------------------------------------------------------------------ */ |
| 134 | /* |
| 135 | * Cover functions for the various OS path operations. These test the |
| 136 | * network storage mode: if we're in network mode, we use the storage |
| 137 | * server syntax, which follows Unix rules regardless of the local OS. |
| 138 | * Otherwise we use the local OS rules, via the osifc functions. |
| 139 | */ |
| 140 | |
| 141 | /* canonicalize a network path: resolve . and .. links */ |
| 142 | static void nf_canonicalize(char *path) |
| 143 | { |
| 144 | /* note if it's an absolute path */ |
| 145 | int abs = path[0] == '/'; |
| 146 | |
| 147 | /* make a list of the path elements */ |
| 148 | char **ele = new char*[strlen(path)]; |
| 149 | int cnt; |
| 150 | for (cnt = 0, ele[0] = strtok(path, "/") ; ele[cnt] != 0 ; |
| 151 | ele[++cnt] = strtok(0, "/")) ; |
| 152 | |
| 153 | /* remove '.' and empty elements, and cancel '..' against prior elements */ |
| 154 | int src, dst; |
| 155 | for (src = dst = 0 ; src < cnt ; ) |
| 156 | { |
| 157 | if (ele[src][0] == '\0' || strcmp(ele[src], ".") == 0) |
| 158 | { |
| 159 | /* empty or '.' - simply omit it */ |
| 160 | ++src; |
| 161 | } |
| 162 | else if (strcmp(ele[src], "..") == 0) |
| 163 | { |
| 164 | /* |
| 165 | * '..' - cancel against the previous element if there is one |
| 166 | * and it's not '..', otherwise keep it |
| 167 | */ |
| 168 | if (dst > 0 && strcmp(ele[dst-1], "..") != 0) |
| 169 | --dst, ++src; |
| 170 | else |
| 171 | ele[dst++] = ele[src++]; |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | /* ordinary element - copy it */ |
| 176 | ele[dst++] = ele[src++]; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* reconstruct the string */ |
| 181 | char *p = path; |
| 182 | if (abs) |
| 183 | *p++ = '/'; |
| 184 | for (int i = 0 ; i < dst ; ++i) |
| 185 | { |
| 186 | size_t l = strlen(ele[i]); |
| 187 | memcpy(p, ele[i], l); |
| 188 | p += l; |
| 189 | if (i + 1 < dst) |
| 190 | *p++ = '/'; |
| 191 | } |
| 192 | *p = '\0'; |
| 193 | |
| 194 | /* if it's complete empty, make it '.' */ |
| 195 | if (path[0] == '\0') |
| 196 | path[0] = '.', path[1] = '\0'; |
| 197 | |
| 198 | /* done with the path list */ |
| 199 | delete [] ele; |
| 200 | } |
| 201 | |
| 202 | /* build a full path */ |
| 203 | static void fn_build_full_path( |
| 204 | VMG_ char *buf, size_t buflen, |
| 205 | const char *dir, const char *fname, int literal) |
| 206 | { |
| 207 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 208 | { |
| 209 | /* |
| 210 | * Network storage mode - use Unix rules. First, if the filename |
| 211 | * is in absolute format already, use it unchanged. If not, append |
| 212 | * the filename to the directory, adding a '/' between the two if |
| 213 | * the directory doesn't already end with a slash. |
| 214 | */ |
| 215 | if (fname[0] == '/') |
| 216 | { |
| 217 | /* the file is absolute - return it unchanged */ |
| 218 | lib_strcpy(buf, buflen, fname); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | /* note the last character of the directory path */ |
| 223 | size_t dirlen = strlen(dir); |
| 224 | char dirlast = dirlen != 0 ? dir[dirlen-1] : 0; |
| 225 | |
| 226 | /* build the result */ |
| 227 | t3sprintf(buf, buflen, "%s%s%s", |
| 228 | dir, dirlast == '/' ? "" : "/", fname); |
| 229 | } |
| 230 | |
| 231 | /* if not in literal mode, canonicalize the result */ |
| 232 | if (!literal) |
| 233 | nf_canonicalize(buf); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | /* local storage mode - use local OS rules */ |
| 238 | if (literal) |
| 239 | os_combine_paths(buf, buflen, dir, fname); |
| 240 | else |
| 241 | os_build_full_path(buf, buflen, dir, fname); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* get the root name from a path string */ |
| 246 | static const char *fn_get_root_name(VMG_ const char *str) |
| 247 | { |
| 248 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 249 | { |
| 250 | /* |
| 251 | * network storage mode - use Unix rules, so simply find the |
| 252 | * rightmost '/' and return the portion after it (or the whole |
| 253 | * name, if there's no '/') |
| 254 | */ |
| 255 | const char *r = strrchr(str, '/'); |
| 256 | return r != 0 ? r + 1 : str; |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | /* local storage mode - use local OS rules */ |
| 261 | return os_get_root_name(str); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* get the path name portion from a filename string */ |
| 266 | static void fn_get_path_name(VMG_ char *buf, size_t buflen, const char *str) |
| 267 | { |
| 268 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 269 | { |
| 270 | /* |
| 271 | * Network storage mode - use Unix rules, so simply find the |
| 272 | * rightmost '/' and return the portion before it (or an empty |
| 273 | * string, if there's no '/'). If the right most '/' is the first |
| 274 | * character, or is only preceded by '/' characters, it's a root |
| 275 | * path, so return '/' as the path. |
| 276 | */ |
| 277 | const char *r = strrchr(str, '/'); |
| 278 | if (r == 0) |
| 279 | { |
| 280 | /* there's no '/' at all, so there's no path */ |
| 281 | buf[0] = '\0'; |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | /* skip any more '/'s immediately preceding the one we found */ |
| 286 | for ( ; r > str && *(r-1) == '/' ; --r) ; |
| 287 | |
| 288 | /* if we're at the first character, it's a root path */ |
| 289 | if (r == str) |
| 290 | { |
| 291 | /* return "/" for the root path */ |
| 292 | lib_strcpy(buf, buflen, "/"); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | /* return the portion up to but not including the '/' */ |
| 297 | lib_strcpy(buf, buflen, str, r - str); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | /* local storage mode - use local OS rules */ |
| 304 | return os_get_path_name(buf, buflen, str); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* is the file in absolute path format? */ |
| 309 | static int fn_is_file_absolute(VMG_ const char *str) |
| 310 | { |
| 311 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 312 | { |
| 313 | /* network storage mode - it's absolute if it starts with '/' */ |
| 314 | return str[0] == '/'; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | /* local storage mode - use local OS rules */ |
| 319 | return os_is_file_absolute(str); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* get the absolute path */ |
| 324 | static int fn_get_abs_filename(VMG_ char *buf, size_t buflen, const char *str) |
| 325 | { |
| 326 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 327 | { |
| 328 | /* |
| 329 | * network storage mode - the storage server doesn't have such a |
| 330 | * thing as an absolute path; all paths are relative to the game's |
| 331 | * sandbox folder |
| 332 | */ |
| 333 | return FALSE; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | /* local storage mode - use local OS rules */ |
| 338 | return os_get_abs_filename(buf, buflen, str); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* check for a special filename */ |
| 343 | static int fn_is_special_file(VMG_ const char *str) |
| 344 | { |
| 345 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 346 | { |
| 347 | /* network storage mode treat "." and ".." as special */ |
| 348 | if (strcmp(str, ".") == 0) |
| 349 | return OS_SPECFILE_SELF; |
| 350 | else if (strcmp(str, "..") == 0) |
| 351 | return OS_SPECFILE_PARENT; |
| 352 | else |
| 353 | return OS_SPECFILE_NONE; |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | /* local storage mode - use local OS rules */ |
| 358 | return os_is_special_file(str); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* get the special directory path for the working directory */ |
| 363 | static const char *fn_pwd_dir(VMG0_) |
| 364 | { |
| 365 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 366 | return "."; |
| 367 | else |
| 368 | return OSPATHPWD; |
| 369 | } |
| 370 | |
| 371 | /* convert from universal notation to local notation */ |
| 372 | static void fn_cvt_url_dir(VMG_ char *buf, size_t buflen, const char *str) |
| 373 | { |
| 374 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 375 | { |
| 376 | /* |
| 377 | * network storage mode - the universal and local notation are the |
| 378 | * same, since the storage server uses unix-style syntax |
| 379 | */ |
| 380 | lib_strcpy(buf, buflen, str); |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | /* local storage mode - use the local OS conversion */ |
| 385 | os_cvt_url_dir(buf, buflen, str); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* convert from local notation to universal notation */ |
| 390 | static void fn_cvt_dir_url(VMG_ char *buf, size_t buflen, const char *str) |
| 391 | { |
| 392 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 393 | { |
| 394 | /* network storage mode - local and universal notation are the same */ |
| 395 | lib_strcpy(buf, buflen, str); |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | /* local storage mode - use the local OS conversion */ |
| 400 | os_cvt_dir_url(buf, buflen, str); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* compare filenames */ |
| 405 | static int fn_file_names_equal(VMG_ const char *a, const char *b) |
| 406 | { |
| 407 | if (CVmNetFile::is_net_mode(vmg0_)) |
| 408 | { |
| 409 | /* |
| 410 | * network storage mode - compare using Unix rules; this is a |
| 411 | * simple case-sensitive string comparison |
| 412 | */ |
| 413 | return strcmp(a, b) == 0; |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | /* local storage mode - use the local OS routine */ |
| 418 | return os_file_names_equal(a, b); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | |
| 423 | /* ------------------------------------------------------------------------ */ |
| 424 | /* |
| 425 | * CVmObjFileName intrinsic class implementation |
| 426 | */ |
| 427 | |
| 428 | /* |
| 429 | * construction |
| 430 | */ |
| 431 | CVmObjFileName::CVmObjFileName(VMG_ int32_t sfid, const char *str, size_t len) |
| 432 | { |
| 433 | /* allocate our extension structure */ |
| 434 | ext_ = (char *)vm_filnam_ext::alloc_ext(vmg_ this, sfid, str, len); |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Get the local path string for a given value. If the value is a string, |
| 439 | * we'll treat it as a local path name. If it's a FileName, we'll return |
| 440 | * its local file name. In either case, returns a pointer to the string in |
| 441 | * our usual VMB_LEN prefix format. |
| 442 | */ |
| 443 | const char *CVmObjFileName::get_local_path(VMG_ const vm_val_t *val) |
| 444 | { |
| 445 | CVmObjFileName *fn; |
| 446 | const char *str; |
| 447 | if ((fn = vm_val_cast(CVmObjFileName, val)) != 0) |
| 448 | { |
| 449 | /* it's a FileName object - return a copy of its filename string */ |
| 450 | return fn->get_ext()->str; |
| 451 | } |
| 452 | else if ((str = val->get_as_string(vmg0_)) != 0) |
| 453 | { |
| 454 | /* it's a string, so convert from URL notation to local path syntax */ |
| 455 | return str; |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | /* it's not a string or FileName */ |
| 460 | return 0; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Combine two local path elements to create a full local path. Returns a |
| 466 | * new object representing the combined path. |
| 467 | */ |
| 468 | vm_obj_id_t CVmObjFileName::combine_path( |
| 469 | VMG_ const char *path, size_t pathl, const char *file, size_t filel, |
| 470 | int literal) |
| 471 | { |
| 472 | /* |
| 473 | * Estimate the space we'll need for the combined result. This is just |
| 474 | * the sum of the two sizes on most systems, plus a path separator. On |
| 475 | * some older systems this might add a bit more overhead, such as VMS |
| 476 | * bracket notation for the directory. Pad it out a bit to be safe. |
| 477 | */ |
| 478 | size_t buflen = pathl + filel + 32; |
| 479 | if (buflen < OSFNMAX) |
| 480 | buflen = OSFNMAX; |
| 481 | |
| 482 | char *buf = 0, *pathz = 0, *filez = 0; |
| 483 | vm_obj_id_t id = VM_INVALID_OBJ; |
| 484 | err_try |
| 485 | { |
| 486 | /* allocate null-terminated copies of the source strings */ |
| 487 | pathz = lib_copy_str(path, pathl); |
| 488 | filez = lib_copy_str(file, filel); |
| 489 | |
| 490 | /* allocate the result buffer */ |
| 491 | buf = lib_alloc_str(buflen); |
| 492 | |
| 493 | /* build the full path */ |
| 494 | fn_build_full_path(vmg_ buf, buflen, pathz, filez, literal); |
| 495 | |
| 496 | /* create the FileName for the combined path */ |
| 497 | id = create_from_local(vmg_ buf, strlen(buf)); |
| 498 | } |
| 499 | err_finally |
| 500 | { |
| 501 | /* delete the allocated strings */ |
| 502 | lib_free_str(buf); |
| 503 | lib_free_str(pathz); |
| 504 | lib_free_str(filez); |
| 505 | } |
| 506 | err_end; |
| 507 | |
| 508 | /* return the new object */ |
| 509 | return id; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Convert a string from URL to local filename notation. Allocates the |
| 514 | * result string; the caller must free the string with lib_free_str(). |
| 515 | */ |
| 516 | char *CVmObjFileName::url_to_local( |
| 517 | VMG_ const char *str, size_t len, int nullterm) |
| 518 | { |
| 519 | char *strz = 0, *buf = 0; |
| 520 | err_try |
| 521 | { |
| 522 | /* |
| 523 | * Estimate the length of the return string we'll need. Most |
| 524 | * systems these days have Unix-like file naming, where the result |
| 525 | * path will be the same length as the source path. There are |
| 526 | * older systems with more complex conventions; for example, VMS |
| 527 | * puts directories in brackets. To allow for this kind of |
| 528 | * variation, allocate space for the combined string plus some |
| 529 | * overhead. In any case, use the local maximum filename length as |
| 530 | * the lower limit. |
| 531 | */ |
| 532 | size_t buflen = len + 32; |
| 533 | if (buflen < OSFNMAX) |
| 534 | buflen = OSFNMAX; |
| 535 | |
| 536 | /* make a null-terminated copy of the string if necessary */ |
| 537 | if (!nullterm) |
| 538 | strz = lib_copy_str(str, len); |
| 539 | |
| 540 | /* allocate a conversion buffer */ |
| 541 | buf = lib_alloc_str(buflen); |
| 542 | |
| 543 | /* convert the name */ |
| 544 | fn_cvt_url_dir(vmg_ buf, buflen, nullterm ? str : strz); |
| 545 | } |
| 546 | err_catch_disc |
| 547 | { |
| 548 | /* delete the buffer and re-throw the error */ |
| 549 | lib_free_str(buf); |
| 550 | err_rethrow(); |
| 551 | } |
| 552 | err_finally |
| 553 | { |
| 554 | /* we're done with copy of the source string (if we made one) */ |
| 555 | lib_free_str(strz); |
| 556 | } |
| 557 | err_end; |
| 558 | |
| 559 | /* return the buffer */ |
| 560 | return buf; |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * create dynamically using stack arguments |
| 565 | */ |
| 566 | vm_obj_id_t CVmObjFileName::create_from_stack( |
| 567 | VMG_ const uchar **pc_ptr, uint argc) |
| 568 | { |
| 569 | /* parse arguments */ |
| 570 | const char *path, *file; |
| 571 | vm_obj_id_t id = VM_INVALID_OBJ; |
| 572 | if (argc == 0) |
| 573 | { |
| 574 | /* no arguments - create a file representing the working directory */ |
| 575 | const char *pwd = fn_pwd_dir(vmg0_); |
| 576 | id = create_from_local(vmg_ pwd, strlen(pwd)); |
| 577 | } |
| 578 | else if (argc == 1 && (file = G_stk->get(0)->get_as_string(vmg0_)) != 0) |
| 579 | { |
| 580 | /* it's a string in local filename notation */ |
| 581 | id = create_from_local(vmg_ file + VMB_LEN, vmb_get_len(file)); |
| 582 | } |
| 583 | else if (argc == 1 && G_stk->get(0)->is_numeric(vmg0_)) |
| 584 | { |
| 585 | /* it's a special file ID - resolve to a path */ |
| 586 | int32_t sfid = G_stk->get(0)->num_to_int(vmg0_); |
| 587 | char buf[OSFNMAX]; |
| 588 | if (!CVmObjFile::sfid_to_path(vmg_ buf, sizeof(buf), sfid)) |
| 589 | err_throw(VMERR_BAD_VAL_BIF); |
| 590 | |
| 591 | /* create the file */ |
| 592 | id = create_from_sfid(vmg_ sfid, buf, strlen(buf)); |
| 593 | } |
| 594 | else if (argc == 2 |
| 595 | && (path = get_local_path(vmg_ G_stk->get(0))) != 0 |
| 596 | && (file = get_local_path(vmg_ G_stk->get(1))) != 0) |
| 597 | { |
| 598 | /* create a FileName from the combination of the two path strings */ |
| 599 | id = combine_path(vmg_ path + VMB_LEN, vmb_get_len(path), |
| 600 | file + VMB_LEN, vmb_get_len(file), FALSE); |
| 601 | } |
| 602 | else if (argc >= 3) |
| 603 | { |
| 604 | /* invalid number of arguments */ |
| 605 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | /* wrong types */ |
| 610 | err_throw(VMERR_BAD_TYPE_BIF); |
| 611 | } |
| 612 | |
| 613 | /* discard arguments */ |
| 614 | G_stk->discard(argc); |
| 615 | |
| 616 | /* return the new ID */ |
| 617 | return id; |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * create from URL notation |
| 622 | */ |
| 623 | vm_obj_id_t CVmObjFileName::create_from_url(VMG_ const char *str, size_t len) |
| 624 | { |
| 625 | char *lcl = 0; |
| 626 | vm_obj_id_t fn = VM_INVALID_OBJ; |
| 627 | err_try |
| 628 | { |
| 629 | /* convert to local notation */ |
| 630 | lcl = url_to_local(vmg_ str, len, FALSE); |
| 631 | |
| 632 | /* create the FileName object */ |
| 633 | fn = create_from_local(vmg_ lcl, strlen(lcl)); |
| 634 | } |
| 635 | err_finally |
| 636 | { |
| 637 | /* we're done with the conversion buffer and path */ |
| 638 | lib_free_str(lcl); |
| 639 | } |
| 640 | err_end; |
| 641 | |
| 642 | /* return the filename object */ |
| 643 | return fn; |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * create from a local path string |
| 648 | */ |
| 649 | vm_obj_id_t CVmObjFileName::create_from_local(VMG_ const char *str, size_t len) |
| 650 | { |
| 651 | /* allocate the new ID */ |
| 652 | vm_obj_id_t id = vm_new_id(vmg_ FALSE, FALSE, FALSE); |
| 653 | |
| 654 | /* create the object */ |
| 655 | new (vmg_ id) CVmObjFileName(vmg_ 0, str, len); |
| 656 | |
| 657 | /* return the new ID */ |
| 658 | return id; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * create from a special file ID |
| 663 | */ |
| 664 | vm_obj_id_t CVmObjFileName::create_from_sfid(VMG_ int32_t sfid, |
| 665 | const char *str, size_t len) |
| 666 | { |
| 667 | /* allocate the new ID */ |
| 668 | vm_obj_id_t id = vm_new_id(vmg_ FALSE, FALSE, FALSE); |
| 669 | |
| 670 | /* create the object */ |
| 671 | new (vmg_ id) CVmObjFileName(vmg_ sfid, str, len); |
| 672 | |
| 673 | /* return the new ID */ |
| 674 | return id; |
| 675 | } |
| 676 | |
| 677 | /* ------------------------------------------------------------------------ */ |
| 678 | /* |
| 679 | * notify of deletion |
| 680 | */ |
| 681 | void CVmObjFileName::notify_delete(VMG_ int /*in_root_set*/) |
| 682 | { |
| 683 | /* free our additional data, if we have any */ |
| 684 | if (ext_ != 0) |
| 685 | G_mem->get_var_heap()->free_mem(ext_); |
| 686 | } |
| 687 | |
| 688 | /* ------------------------------------------------------------------------ */ |
| 689 | /* |
| 690 | * set a property |
| 691 | */ |
| 692 | void CVmObjFileName::set_prop(VMG_ class CVmUndo *undo, |
| 693 | vm_obj_id_t self, vm_prop_id_t prop, |
| 694 | const vm_val_t *val) |
| 695 | { |
| 696 | /* no settable properties - throw an error */ |
| 697 | err_throw(VMERR_INVALID_SETPROP); |
| 698 | } |
| 699 | |
| 700 | /* ------------------------------------------------------------------------ */ |
| 701 | /* |
| 702 | * get a property |
| 703 | */ |
| 704 | int CVmObjFileName::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 705 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 706 | uint *argc) |
| 707 | { |
| 708 | /* translate the property into a function vector index */ |
| 709 | uint func_idx = G_meta_table->prop_to_vector_idx( |
| 710 | metaclass_reg_->get_reg_idx(), prop); |
| 711 | |
| 712 | /* call the appropriate function */ |
| 713 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 714 | { |
| 715 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 716 | return TRUE; |
| 717 | } |
| 718 | |
| 719 | /* inherit default handling from our base class */ |
| 720 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * call a static property |
| 725 | */ |
| 726 | int CVmObjFileName::call_stat_prop(VMG_ vm_val_t *result, |
| 727 | const uchar **pc_ptr, uint *argc, |
| 728 | vm_prop_id_t prop) |
| 729 | { |
| 730 | /* look up the function */ |
| 731 | uint midx = G_meta_table |
| 732 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 733 | |
| 734 | switch (midx) |
| 735 | { |
| 736 | case VMOFN_FROMUNIVERSAL: |
| 737 | return s_getp_fromUniversal(vmg_ result, argc); |
| 738 | |
| 739 | case VMOFN_GETROOTDIRS: |
| 740 | return s_getp_getRootDirs(vmg_ result, argc); |
| 741 | |
| 742 | default: |
| 743 | /* it's not ours - inherit the default handling */ |
| 744 | return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | |
| 749 | /* ------------------------------------------------------------------------ */ |
| 750 | /* |
| 751 | * load from an image file |
| 752 | */ |
| 753 | void CVmObjFileName::load_from_image(VMG_ vm_obj_id_t self, |
| 754 | const char *ptr, size_t siz) |
| 755 | { |
| 756 | /* load our image data */ |
| 757 | load_image_data(vmg_ ptr, siz); |
| 758 | |
| 759 | /* |
| 760 | * save our image data pointer in the object table, so that we can |
| 761 | * access it (without storing it ourselves) during a reload |
| 762 | */ |
| 763 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * reload from the image file |
| 768 | */ |
| 769 | void CVmObjFileName::reload_from_image(VMG_ vm_obj_id_t self, |
| 770 | const char *ptr, size_t siz) |
| 771 | { |
| 772 | /* load our image data */ |
| 773 | load_image_data(vmg_ ptr, siz); |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * load or reload data from the image |
| 778 | */ |
| 779 | void CVmObjFileName::load_image_data(VMG_ const char *ptr, size_t siz) |
| 780 | { |
| 781 | /* free our existing extension, if we have one */ |
| 782 | if (ext_ != 0) |
| 783 | G_mem->get_var_heap()->free_mem(ext_); |
| 784 | |
| 785 | /* get the special file ID */ |
| 786 | int32_t sfid = osrp4s(ptr); |
| 787 | |
| 788 | /* if there's a special file ID, look it up; otherwise load the name */ |
| 789 | vm_filnam_ext *ext; |
| 790 | if (sfid != 0) |
| 791 | { |
| 792 | /* translate the special file ID to a path */ |
| 793 | char sfbuf[OSFNMAX]; |
| 794 | int ok = CVmObjFile::sfid_to_path(vmg_ sfbuf, sizeof(sfbuf), sfid); |
| 795 | |
| 796 | /* allocate the extension */ |
| 797 | ext = vm_filnam_ext::alloc_ext(vmg_ this, sfid, sfbuf, strlen(sfbuf)); |
| 798 | |
| 799 | /* note whether the special file ID is valid */ |
| 800 | ext->sfid_valid = (char)ok; |
| 801 | } |
| 802 | else |
| 803 | { |
| 804 | /* get the filename pointer and length */ |
| 805 | const char *str = ptr + 4; |
| 806 | size_t len = vmb_get_len(str); |
| 807 | str += VMB_LEN; |
| 808 | |
| 809 | char *lcl = 0; |
| 810 | err_try |
| 811 | { |
| 812 | /* convert the universal path string to local notation */ |
| 813 | lcl = url_to_local(vmg_ str, len, FALSE); |
| 814 | |
| 815 | /* allocate the extension */ |
| 816 | ext = vm_filnam_ext::alloc_ext(vmg_ this, 0, lcl, strlen(lcl)); |
| 817 | } |
| 818 | err_finally |
| 819 | { |
| 820 | lib_free_str(lcl); |
| 821 | } |
| 822 | err_end; |
| 823 | } |
| 824 | |
| 825 | /* store the extension */ |
| 826 | ext_ = (char *)ext; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | /* ------------------------------------------------------------------------ */ |
| 831 | /* |
| 832 | * save to a file |
| 833 | */ |
| 834 | void CVmObjFileName::save_to_file(VMG_ class CVmFile *fp) |
| 835 | { |
| 836 | /* get our extension */ |
| 837 | vm_filnam_ext *ext = get_ext(); |
| 838 | |
| 839 | /* write the special file ID */ |
| 840 | fp->write_int4(ext->sfid); |
| 841 | |
| 842 | /* if it's an ordinary file, write the path name */ |
| 843 | if (ext->sfid == 0) |
| 844 | { |
| 845 | char *u = 0; |
| 846 | err_try |
| 847 | { |
| 848 | /* write the path in universal notation */ |
| 849 | u = to_universal(vmg0_); |
| 850 | fp->write_str_short_prefix(u, strlen(u)); |
| 851 | } |
| 852 | err_finally |
| 853 | { |
| 854 | lib_free_str(u); |
| 855 | } |
| 856 | err_end; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * restore from a file |
| 862 | */ |
| 863 | void CVmObjFileName::restore_from_file(VMG_ vm_obj_id_t self, |
| 864 | CVmFile *fp, CVmObjFixup *fixups) |
| 865 | { |
| 866 | /* free our existing extension, if we have one */ |
| 867 | if (ext_ != 0) |
| 868 | G_mem->get_var_heap()->free_mem(ext_); |
| 869 | |
| 870 | /* read the special file ID */ |
| 871 | int32_t sfid = fp->read_int4(); |
| 872 | |
| 873 | /* if there's a special file ID, look it up; otherwise read the name */ |
| 874 | vm_filnam_ext *ext; |
| 875 | if (sfid != 0) |
| 876 | { |
| 877 | /* get the special file path */ |
| 878 | char sfbuf[OSFNMAX]; |
| 879 | int ok = CVmObjFile::sfid_to_path(vmg_ sfbuf, sizeof(sfbuf), sfid); |
| 880 | |
| 881 | /* create the extension */ |
| 882 | ext = vm_filnam_ext::alloc_ext(vmg_ this, sfid, sfbuf, strlen(sfbuf)); |
| 883 | |
| 884 | /* note whether the ID is valid on this platform */ |
| 885 | ext->sfid_valid = (char)ok; |
| 886 | } |
| 887 | else |
| 888 | { |
| 889 | char *uni = 0, *lcl = 0; |
| 890 | err_try |
| 891 | { |
| 892 | /* read the filename length */ |
| 893 | size_t len = fp->read_uint2(); |
| 894 | |
| 895 | /* load the saved universal path */ |
| 896 | uni = lib_alloc_str(len + 1); |
| 897 | fp->read_bytes(uni, len); |
| 898 | uni[len] = '\0'; |
| 899 | |
| 900 | /* convert the saved universal path to local notation */ |
| 901 | lcl = url_to_local(vmg_ uni, len, TRUE); |
| 902 | |
| 903 | /* allocate the extension structure */ |
| 904 | ext = vm_filnam_ext::alloc_ext(vmg_ this, 0, lcl, strlen(lcl)); |
| 905 | } |
| 906 | err_finally |
| 907 | { |
| 908 | /* we're done with the allocated buffers */ |
| 909 | lib_free_str(lcl); |
| 910 | lib_free_str(uni); |
| 911 | } |
| 912 | err_end; |
| 913 | } |
| 914 | |
| 915 | /* remember the new extension */ |
| 916 | ext_ = (char *)ext; |
| 917 | } |
| 918 | |
| 919 | |
| 920 | /* ------------------------------------------------------------------------ */ |
| 921 | /* |
| 922 | * Get my path string. This validates the special file ID, and throws an |
| 923 | * error if it's not valid. It's possible to create a FileName with an |
| 924 | * invalid special file ID by loading a saved game that was created on a |
| 925 | * platform with a newer version that includes new SFIDs that don't exist |
| 926 | * in this interpreter version. In such a case we load the special file, |
| 927 | * and it can be saved again, but it's not usable locally and has no path |
| 928 | * information. |
| 929 | */ |
| 930 | const char *CVmObjFileName::get_path_string() const |
| 931 | { |
| 932 | /* make sure this isn't an invalid special file */ |
| 933 | if (get_ext()->sfid && !get_ext()->sfid_valid) |
| 934 | err_throw(VMERR_FILE_NOT_FOUND); |
| 935 | |
| 936 | /* return my path */ |
| 937 | return get_ext()->str; |
| 938 | } |
| 939 | |
| 940 | |
| 941 | /* ------------------------------------------------------------------------ */ |
| 942 | /* |
| 943 | * Compare filenames by the path strings |
| 944 | */ |
| 945 | int CVmObjFileName::equals(VMG_ vm_obj_id_t self, const vm_val_t *val, |
| 946 | int /*depth*/) const |
| 947 | { |
| 948 | /* check the type of the other value */ |
| 949 | CVmObjFileName *fn; |
| 950 | const char *str; |
| 951 | if ((fn = vm_val_cast(CVmObjFileName, val)) != 0) |
| 952 | { |
| 953 | /* it's another FileName object - compare its path */ |
| 954 | return fn_file_names_equal( |
| 955 | vmg_ get_ext()->get_str(), fn->get_ext()->get_str()); |
| 956 | } |
| 957 | else if ((str = val->get_as_string(vmg0_)) != 0) |
| 958 | { |
| 959 | char *strz = 0; |
| 960 | int eq; |
| 961 | err_try |
| 962 | { |
| 963 | /* make a null-terminated copy of the other name */ |
| 964 | strz = lib_copy_str(str + VMB_LEN, vmb_get_len(str)); |
| 965 | |
| 966 | /* compare the names */ |
| 967 | eq = fn_file_names_equal(vmg_ get_ext()->get_str(), strz); |
| 968 | } |
| 969 | err_finally |
| 970 | { |
| 971 | lib_free_str(strz); |
| 972 | } |
| 973 | err_end; |
| 974 | |
| 975 | /* return the result */ |
| 976 | return eq; |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | /* other types are definitely not equal */ |
| 981 | return FALSE; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /* ------------------------------------------------------------------------ */ |
| 986 | /* |
| 987 | * Add a value - this appends a path element |
| 988 | */ |
| 989 | int CVmObjFileName::add_val(VMG_ vm_val_t *result, |
| 990 | vm_obj_id_t self, const vm_val_t *val) |
| 991 | { |
| 992 | /* |
| 993 | * get the other value's path - this can come from a FileName object or |
| 994 | * a string |
| 995 | */ |
| 996 | const char *other = get_local_path(vmg_ val); |
| 997 | if (other == 0) |
| 998 | err_throw(VMERR_BAD_TYPE_ADD); |
| 999 | |
| 1000 | /* build a new object from the combined paths */ |
| 1001 | result->set_obj(combine_path( |
| 1002 | vmg_ get_ext()->get_str(), get_ext()->get_len(), |
| 1003 | other + VMB_LEN, vmb_get_len(other), FALSE)); |
| 1004 | |
| 1005 | /* success */ |
| 1006 | return TRUE; |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | /* ------------------------------------------------------------------------ */ |
| 1011 | /* |
| 1012 | * fromUniversal static method |
| 1013 | */ |
| 1014 | int CVmObjFileName::s_getp_fromUniversal(VMG_ vm_val_t *retval, uint *oargc) |
| 1015 | { |
| 1016 | /* check arguments */ |
| 1017 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1018 | static CVmNativeCodeDesc desc(1); |
| 1019 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1020 | return TRUE; |
| 1021 | |
| 1022 | /* get the filename argument as a string */ |
| 1023 | const char *str = G_stk->get(0)->get_as_string(vmg0_); |
| 1024 | if (str == 0) |
| 1025 | err_throw(VMERR_BAD_VAL_BIF); |
| 1026 | |
| 1027 | /* create and return the new FileName from the url syntax */ |
| 1028 | retval->set_obj(create_from_url(vmg_ str + VMB_LEN, vmb_get_len(str))); |
| 1029 | |
| 1030 | /* discard arguments */ |
| 1031 | G_stk->discard(argc); |
| 1032 | |
| 1033 | /* handled */ |
| 1034 | return TRUE; |
| 1035 | } |
| 1036 | |
| 1037 | /* ------------------------------------------------------------------------ */ |
| 1038 | /* |
| 1039 | * Get the filename string in univeral (URL) format. This allocates a |
| 1040 | * buffer, which the caller must free with lib_free_str() when done. |
| 1041 | */ |
| 1042 | char *CVmObjFileName::to_universal(VMG0_) const |
| 1043 | { |
| 1044 | /* get our local filename string */ |
| 1045 | const char *fname = get_ext()->get_str(); |
| 1046 | size_t fnamel = get_ext()->get_len(); |
| 1047 | |
| 1048 | /* |
| 1049 | * estimate the result size - the URL string should be about the same |
| 1050 | * length as the source string, but leave a little extra space just in |
| 1051 | * case we have to add some path separators that are implied in the |
| 1052 | * local format |
| 1053 | */ |
| 1054 | size_t buflen = fnamel + 32; |
| 1055 | if (buflen < OSFNMAX) |
| 1056 | buflen = OSFNMAX; |
| 1057 | |
| 1058 | /* allocate buffer */ |
| 1059 | char *buf = lib_alloc_str(buflen); |
| 1060 | |
| 1061 | /* do the conversion */ |
| 1062 | fn_cvt_dir_url(vmg_ buf, buflen, fname); |
| 1063 | |
| 1064 | /* return the new string */ |
| 1065 | return buf; |
| 1066 | } |
| 1067 | |
| 1068 | /* ------------------------------------------------------------------------ */ |
| 1069 | /* |
| 1070 | * toUniversal method |
| 1071 | */ |
| 1072 | int CVmObjFileName::getp_toUniversal(VMG_ vm_obj_id_t self, |
| 1073 | vm_val_t *retval, uint *oargc) |
| 1074 | { |
| 1075 | /* check arguments */ |
| 1076 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1077 | static CVmNativeCodeDesc desc(0); |
| 1078 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1079 | return TRUE; |
| 1080 | |
| 1081 | char *buf = 0; |
| 1082 | err_try |
| 1083 | { |
| 1084 | /* allocate the temporary result buffer */ |
| 1085 | buf = to_universal(vmg0_); |
| 1086 | |
| 1087 | /* return a new String object from the result */ |
| 1088 | retval->set_obj(CVmObjString::create(vmg_ FALSE, buf, strlen(buf))); |
| 1089 | } |
| 1090 | err_finally |
| 1091 | { |
| 1092 | /* free the temp buffer */ |
| 1093 | lib_free_str(buf); |
| 1094 | } |
| 1095 | err_end; |
| 1096 | |
| 1097 | /* discard arguments */ |
| 1098 | G_stk->discard(argc); |
| 1099 | |
| 1100 | /* handled */ |
| 1101 | return TRUE; |
| 1102 | } |
| 1103 | |
| 1104 | /* ------------------------------------------------------------------------ */ |
| 1105 | /* |
| 1106 | * getName method |
| 1107 | */ |
| 1108 | int CVmObjFileName::getp_getName(VMG_ vm_obj_id_t self, |
| 1109 | vm_val_t *retval, uint *oargc) |
| 1110 | { |
| 1111 | /* check arguments */ |
| 1112 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1113 | static CVmNativeCodeDesc desc(0); |
| 1114 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1115 | return TRUE; |
| 1116 | |
| 1117 | /* get our extension */ |
| 1118 | vm_filnam_ext *ext = get_ext(); |
| 1119 | |
| 1120 | /* return the path string */ |
| 1121 | retval->set_obj(CVmObjString::create( |
| 1122 | vmg_ FALSE, ext->get_str(), ext->get_len())); |
| 1123 | |
| 1124 | /* discard arguments */ |
| 1125 | G_stk->discard(argc); |
| 1126 | |
| 1127 | /* handled */ |
| 1128 | return TRUE; |
| 1129 | } |
| 1130 | |
| 1131 | /* ------------------------------------------------------------------------ */ |
| 1132 | /* |
| 1133 | * getRootName method |
| 1134 | */ |
| 1135 | int CVmObjFileName::getp_getRootName(VMG_ vm_obj_id_t self, |
| 1136 | vm_val_t *retval, uint *oargc) |
| 1137 | { |
| 1138 | /* check arguments */ |
| 1139 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1140 | static CVmNativeCodeDesc desc(0); |
| 1141 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1142 | return TRUE; |
| 1143 | |
| 1144 | /* presume we won't find a root name */ |
| 1145 | retval->set_nil(); |
| 1146 | |
| 1147 | /* extract the root name from our filename string */ |
| 1148 | const char *r = fn_get_root_name(vmg_ get_ext()->get_str()); |
| 1149 | |
| 1150 | /* if we found a root name, return it as a string */ |
| 1151 | if (r != 0 && r[0] != '\0') |
| 1152 | retval->set_obj(CVmObjString::create(vmg_ FALSE, r, strlen(r))); |
| 1153 | |
| 1154 | /* discard arguments */ |
| 1155 | G_stk->discard(argc); |
| 1156 | |
| 1157 | /* handled */ |
| 1158 | return TRUE; |
| 1159 | } |
| 1160 | |
| 1161 | /* ------------------------------------------------------------------------ */ |
| 1162 | /* |
| 1163 | * getPath method |
| 1164 | */ |
| 1165 | int CVmObjFileName::getp_getPath(VMG_ vm_obj_id_t self, |
| 1166 | vm_val_t *retval, uint *oargc) |
| 1167 | { |
| 1168 | /* check arguments */ |
| 1169 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1170 | static CVmNativeCodeDesc desc(0); |
| 1171 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1172 | return TRUE; |
| 1173 | |
| 1174 | /* presume we'll return nil */ |
| 1175 | retval->set_nil(); |
| 1176 | |
| 1177 | /* get our extension */ |
| 1178 | vm_filnam_ext *ext = get_ext(); |
| 1179 | |
| 1180 | /* get our filename string */ |
| 1181 | const char *fname = ext->get_str(); |
| 1182 | size_t fnamel = ext->get_len(); |
| 1183 | |
| 1184 | /* |
| 1185 | * Estimate how much space we'll need for the extracted path name. The |
| 1186 | * path portion should be shorter than the whole name, but add some |
| 1187 | * padding for overhead in case the local system needs to add any |
| 1188 | * syntax (e.g., VMS directory brackets). |
| 1189 | */ |
| 1190 | size_t buflen = fnamel + 32; |
| 1191 | if (buflen < OSFNMAX) |
| 1192 | buflen = OSFNMAX; |
| 1193 | |
| 1194 | char *buf = 0; |
| 1195 | err_try |
| 1196 | { |
| 1197 | /* allocate a temporary result buffer */ |
| 1198 | buf = lib_alloc_str(buflen); |
| 1199 | |
| 1200 | /* extract the path name */ |
| 1201 | fn_get_path_name(vmg_ buf, buflen, fname); |
| 1202 | |
| 1203 | /* return the path as a FileName, or nil if there's no path */ |
| 1204 | if (buf[0] != '\0') |
| 1205 | retval->set_obj(create_from_local(vmg_ buf, strlen(buf))); |
| 1206 | } |
| 1207 | err_finally |
| 1208 | { |
| 1209 | /* free the temp buffer */ |
| 1210 | lib_free_str(buf); |
| 1211 | } |
| 1212 | err_end; |
| 1213 | |
| 1214 | /* discard arguments */ |
| 1215 | G_stk->discard(argc); |
| 1216 | |
| 1217 | /* handled */ |
| 1218 | return TRUE; |
| 1219 | } |
| 1220 | |
| 1221 | /* ------------------------------------------------------------------------ */ |
| 1222 | /* |
| 1223 | * addToPath method |
| 1224 | */ |
| 1225 | int CVmObjFileName::getp_addToPath(VMG_ vm_obj_id_t self, |
| 1226 | vm_val_t *retval, uint *oargc) |
| 1227 | { |
| 1228 | /* check arguments */ |
| 1229 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1230 | static CVmNativeCodeDesc desc(1); |
| 1231 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1232 | return TRUE; |
| 1233 | |
| 1234 | /* get the target path - it can be a FileName or string */ |
| 1235 | const char *file = get_local_path(vmg_ G_stk->get(0)); |
| 1236 | if (file == 0) |
| 1237 | err_throw(VMERR_BAD_TYPE_BIF); |
| 1238 | |
| 1239 | /* create a new object from the combined path */ |
| 1240 | retval->set_obj(combine_path( |
| 1241 | vmg_ get_ext()->get_str(), get_ext()->get_len(), |
| 1242 | file + VMB_LEN, vmb_get_len(file), FALSE)); |
| 1243 | |
| 1244 | /* discard arguments */ |
| 1245 | G_stk->discard(argc); |
| 1246 | |
| 1247 | /* handled */ |
| 1248 | return TRUE; |
| 1249 | } |
| 1250 | |
| 1251 | /* ------------------------------------------------------------------------ */ |
| 1252 | /* |
| 1253 | * isAbsolute method |
| 1254 | */ |
| 1255 | int CVmObjFileName::getp_isAbsolute(VMG_ vm_obj_id_t self, |
| 1256 | vm_val_t *retval, uint *oargc) |
| 1257 | { |
| 1258 | /* check arguments */ |
| 1259 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1260 | static CVmNativeCodeDesc desc(0); |
| 1261 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1262 | return TRUE; |
| 1263 | |
| 1264 | /* ask the OS if we're absolute, and set the return value accordingly */ |
| 1265 | retval->set_logical(fn_is_file_absolute(vmg_ get_ext()->get_str())); |
| 1266 | |
| 1267 | /* discard arguments */ |
| 1268 | G_stk->discard(argc); |
| 1269 | |
| 1270 | /* handled */ |
| 1271 | return TRUE; |
| 1272 | } |
| 1273 | |
| 1274 | /* ------------------------------------------------------------------------ */ |
| 1275 | /* |
| 1276 | * getAbsolutePath method |
| 1277 | */ |
| 1278 | int CVmObjFileName::getp_getAbsolutePath(VMG_ vm_obj_id_t self, |
| 1279 | vm_val_t *retval, uint *oargc) |
| 1280 | { |
| 1281 | /* check arguments */ |
| 1282 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1283 | static CVmNativeCodeDesc desc(0); |
| 1284 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1285 | return TRUE; |
| 1286 | |
| 1287 | /* get our local filename string */ |
| 1288 | const char *fname = get_ext()->get_str(); |
| 1289 | size_t fnamel = get_ext()->get_len(); |
| 1290 | |
| 1291 | /* we haven't allocated any strings yet */ |
| 1292 | char *buf = 0, *outbuf = 0; |
| 1293 | |
| 1294 | err_try |
| 1295 | { |
| 1296 | /* |
| 1297 | * if it's not absolute already, combine it with our internal |
| 1298 | * working directory to get the fully qualified path |
| 1299 | */ |
| 1300 | const char *src = fname; |
| 1301 | if (!fn_is_file_absolute(vmg_ fname)) |
| 1302 | { |
| 1303 | /* allocate space for the combined path */ |
| 1304 | size_t buflen = fnamel + strlen(G_file_path) + 32; |
| 1305 | if (buflen < OSFNMAX) |
| 1306 | buflen = OSFNMAX; |
| 1307 | |
| 1308 | /* build the combined path */ |
| 1309 | buf = lib_alloc_str(buflen); |
| 1310 | fn_build_full_path(vmg_ buf, buflen, G_file_path, fname, TRUE); |
| 1311 | |
| 1312 | /* use this as the new source path */ |
| 1313 | src = buf; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * Even though the name should already be absolute, run it through |
| 1318 | * os_get_abs_filename() anyway, since that will convert the name |
| 1319 | * to a more canonical format on many systems. |
| 1320 | */ |
| 1321 | |
| 1322 | /* figure how much space we'll need, adding some padding */ |
| 1323 | size_t buflen = strlen(src) + 32; |
| 1324 | if (buflen < OSFNMAX) |
| 1325 | buflen = OSFNMAX; |
| 1326 | |
| 1327 | /* allocate the buffer and do the conversion */ |
| 1328 | outbuf = lib_alloc_str(buflen); |
| 1329 | if (fn_get_abs_filename(vmg_ outbuf, buflen, src)) |
| 1330 | { |
| 1331 | /* success - return the result string */ |
| 1332 | retval->set_obj(CVmObjString::create( |
| 1333 | vmg_ FALSE, outbuf, strlen(outbuf))); |
| 1334 | } |
| 1335 | else |
| 1336 | { |
| 1337 | /* failure - return nil */ |
| 1338 | retval->set_nil(); |
| 1339 | } |
| 1340 | } |
| 1341 | err_finally |
| 1342 | { |
| 1343 | /* free the allocated string */ |
| 1344 | lib_free_str(buf); |
| 1345 | lib_free_str(outbuf); |
| 1346 | } |
| 1347 | err_end; |
| 1348 | |
| 1349 | /* discard arguments */ |
| 1350 | G_stk->discard(argc); |
| 1351 | |
| 1352 | /* handled */ |
| 1353 | return TRUE; |
| 1354 | } |
| 1355 | |
| 1356 | /* ------------------------------------------------------------------------ */ |
| 1357 | /* |
| 1358 | * getRootDirs method |
| 1359 | */ |
| 1360 | int CVmObjFileName::s_getp_getRootDirs(VMG_ vm_val_t *retval, uint *oargc) |
| 1361 | { |
| 1362 | /* check arguments */ |
| 1363 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1364 | static CVmNativeCodeDesc desc(0); |
| 1365 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1366 | return TRUE; |
| 1367 | |
| 1368 | /* |
| 1369 | * See how much space we need for the root list. If we're in network |
| 1370 | * storage mode, there's no concept of a root folder, since everything |
| 1371 | * is relative to the user+game folder. |
| 1372 | */ |
| 1373 | size_t buflen; |
| 1374 | if (!CVmNetFile::is_net_mode(vmg0_) |
| 1375 | && (buflen = os_get_root_dirs(0, 0)) != 0) |
| 1376 | { |
| 1377 | /* allocate space */ |
| 1378 | char *buf = new char[buflen]; |
| 1379 | char *lclstr = 0; |
| 1380 | err_try |
| 1381 | { |
| 1382 | /* get the root list for real this time */ |
| 1383 | os_get_root_dirs(buf, buflen); |
| 1384 | |
| 1385 | /* count the strings in the list, filtered for file safety */ |
| 1386 | int cnt = 0; |
| 1387 | char *p; |
| 1388 | for (p = buf ; *p != '\0' ; p += strlen(p) + 1) |
| 1389 | { |
| 1390 | /* if we're allowed to get metadata on this path, count it */ |
| 1391 | if (CVmObjFile::query_safety_for_open( |
| 1392 | vmg_ p, VMOBJFILE_ACCESS_GETINFO)) |
| 1393 | ++cnt; |
| 1394 | } |
| 1395 | |
| 1396 | /* create the list; clear it and push it for gc protection */ |
| 1397 | retval->set_obj(CVmObjList::create(vmg_ FALSE, cnt)); |
| 1398 | CVmObjList *lst = vm_objid_cast(CVmObjList, retval->val.obj); |
| 1399 | lst->cons_clear(); |
| 1400 | G_stk->push(retval); |
| 1401 | |
| 1402 | /* build the list */ |
| 1403 | for (p = buf, cnt = 0 ; *p != '\0' ; p += strlen(p) + 1) |
| 1404 | { |
| 1405 | /* check to see if we're allowed to access this root */ |
| 1406 | if (CVmObjFile::query_safety_for_open( |
| 1407 | vmg_ p, VMOBJFILE_ACCESS_GETINFO)) |
| 1408 | { |
| 1409 | /* map the string to utf8 */ |
| 1410 | size_t len = G_cmap_from_fname->map_str_alo(&lclstr, p); |
| 1411 | |
| 1412 | /* create the FileName object */ |
| 1413 | vm_val_t ele; |
| 1414 | ele.set_obj(create_from_local(vmg_ lclstr, len)); |
| 1415 | |
| 1416 | /* done with the mapped string */ |
| 1417 | t3free(lclstr); |
| 1418 | lclstr = 0; |
| 1419 | |
| 1420 | /* add it to the list */ |
| 1421 | lst->cons_set_element(cnt++, &ele); |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* done with our gc protection for the list */ |
| 1426 | G_stk->discard(); |
| 1427 | } |
| 1428 | err_finally |
| 1429 | { |
| 1430 | /* free the root list buffer */ |
| 1431 | delete [] buf; |
| 1432 | |
| 1433 | /* free the mapped filename string */ |
| 1434 | if (lclstr != 0) |
| 1435 | t3free(lclstr); |
| 1436 | } |
| 1437 | err_end; |
| 1438 | } |
| 1439 | else |
| 1440 | { |
| 1441 | /* no root list - return an empty list */ |
| 1442 | retval->set_obj(CVmObjList::create(vmg_ FALSE, (size_t)0)); |
| 1443 | } |
| 1444 | |
| 1445 | /* discard arguments */ |
| 1446 | G_stk->discard(argc); |
| 1447 | |
| 1448 | /* handled */ |
| 1449 | return TRUE; |
| 1450 | } |
| 1451 | |
| 1452 | /* ------------------------------------------------------------------------ */ |
| 1453 | /* |
| 1454 | * deleteFile method |
| 1455 | */ |
| 1456 | int CVmObjFileName::getp_deleteFile(VMG_ vm_obj_id_t self, |
| 1457 | vm_val_t *retval, uint *oargc) |
| 1458 | { |
| 1459 | /* check arguments */ |
| 1460 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1461 | static CVmNativeCodeDesc desc(0); |
| 1462 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1463 | return TRUE; |
| 1464 | |
| 1465 | /* get the filename; use the DELETE access mode */ |
| 1466 | vm_rcdesc rc(vmg_ "FileName.deleteFile", |
| 1467 | metaclass_reg_->get_class_obj(vmg0_), |
| 1468 | VMOFN_DELETEFILE, G_stk->get(0), argc); |
| 1469 | CVmNetFile *netfile = CVmObjFile::get_filename_from_obj( |
| 1470 | vmg_ self, &rc, VMOBJFILE_ACCESS_DELETE, |
| 1471 | OSFTUNK, "application/octet-stream"); |
| 1472 | |
| 1473 | /* close the network file descriptor - this will delete the file */ |
| 1474 | netfile->close(vmg0_); |
| 1475 | |
| 1476 | /* no return value */ |
| 1477 | retval->set_nil(); |
| 1478 | |
| 1479 | /* discard arguments */ |
| 1480 | G_stk->discard(argc); |
| 1481 | |
| 1482 | /* handled */ |
| 1483 | return TRUE; |
| 1484 | } |
| 1485 | |
| 1486 | /* ------------------------------------------------------------------------ */ |
| 1487 | /* |
| 1488 | * renameFile method |
| 1489 | */ |
| 1490 | int CVmObjFileName::getp_renameFile(VMG_ vm_obj_id_t self, |
| 1491 | vm_val_t *retval, uint *oargc) |
| 1492 | { |
| 1493 | /* check arguments */ |
| 1494 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1495 | static CVmNativeCodeDesc desc(1); |
| 1496 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1497 | return TRUE; |
| 1498 | |
| 1499 | /* set up our recursive callback descriptor in case netfile needs it */ |
| 1500 | vm_rcdesc rc(vmg_ "FileName.renameFile", |
| 1501 | metaclass_reg_->get_class_obj(vmg0_), |
| 1502 | VMOFN_RENAMEFILE, G_stk->get(0), argc); |
| 1503 | |
| 1504 | CVmNetFile *oldfile = 0, *newfile = 0; |
| 1505 | err_try |
| 1506 | { |
| 1507 | /* get the old filename from self; use the RENAME FROM access mode */ |
| 1508 | oldfile = CVmObjFile::get_filename_from_obj( |
| 1509 | vmg_ self, &rc, VMOBJFILE_ACCESS_RENAME_FROM, |
| 1510 | OSFTUNK, "application/octet-stream"); |
| 1511 | |
| 1512 | /* get the new filename argument; use RENAME TO access mode */ |
| 1513 | newfile = CVmObjFile::get_filename_arg( |
| 1514 | vmg_ G_stk->get(0), &rc, VMOBJFILE_ACCESS_RENAME_TO, FALSE, |
| 1515 | OSFTUNK, "application/octet-stream"); |
| 1516 | |
| 1517 | /* rename the file */ |
| 1518 | oldfile->rename_to(vmg_ newfile); |
| 1519 | } |
| 1520 | err_finally |
| 1521 | { |
| 1522 | /* done with the netfile objects */ |
| 1523 | if (oldfile != 0) |
| 1524 | oldfile->abandon(vmg0_); |
| 1525 | if (newfile != 0) |
| 1526 | newfile->abandon(vmg0_); |
| 1527 | } |
| 1528 | err_end; |
| 1529 | |
| 1530 | /* no return value */ |
| 1531 | retval->set_nil(); |
| 1532 | |
| 1533 | /* discard arguments */ |
| 1534 | G_stk->discard(argc); |
| 1535 | |
| 1536 | /* handled */ |
| 1537 | return TRUE; |
| 1538 | } |
| 1539 | |
| 1540 | /* ------------------------------------------------------------------------ */ |
| 1541 | /* |
| 1542 | * Translate an os_filemode_t value to a FileTypeXxx value |
| 1543 | */ |
| 1544 | static int32_t filemode_to_filetype(int osmode) |
| 1545 | { |
| 1546 | /* |
| 1547 | * mappings from OSFMODE_xxx values to FileTypeXxx values (see |
| 1548 | * include/filename.h in the system headers) |
| 1549 | */ |
| 1550 | static const struct { |
| 1551 | unsigned long osmode; |
| 1552 | int32_t tadsmode; |
| 1553 | } xlat[] = { |
| 1554 | { OSFMODE_FILE, 0x0001 }, // FileTypeFile |
| 1555 | { OSFMODE_DIR, 0x0002 }, // FileTypeDir |
| 1556 | { OSFMODE_CHAR, 0x0004 }, // FileTypeChar |
| 1557 | { OSFMODE_BLK, 0x0008 }, // FileTypeBlock |
| 1558 | { OSFMODE_PIPE, 0x0010 }, // FileTypePipe |
| 1559 | { OSFMODE_SOCKET, 0x0020 }, // FileTypeSocket |
| 1560 | { OSFMODE_LINK, 0x0040 } // FileTypeLink |
| 1561 | }; |
| 1562 | |
| 1563 | /* run through the bit flags, and set the translated bits */ |
| 1564 | int32_t tadsmode = 0; |
| 1565 | for (int i = 0 ; i < countof(xlat) ; ++i) |
| 1566 | { |
| 1567 | /* if this OS mode bit is set, set the corresponding TADS bit */ |
| 1568 | if ((osmode & xlat[i].osmode) != 0) |
| 1569 | tadsmode |= xlat[i].tadsmode; |
| 1570 | } |
| 1571 | |
| 1572 | /* return the result */ |
| 1573 | return tadsmode; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
| 1577 | * Translate OSFATTR_xxx values to FileAttrXxx values |
| 1578 | */ |
| 1579 | static int32_t map_file_attrs(unsigned long osattrs) |
| 1580 | { |
| 1581 | /* |
| 1582 | * mappings from OSFATTR_xxx values to FileAttrXxx values (see |
| 1583 | * include/filename.h in the system headers) |
| 1584 | */ |
| 1585 | static const struct { |
| 1586 | unsigned long osattr; |
| 1587 | int32_t tadsattr; |
| 1588 | } xlat[] = { |
| 1589 | { OSFATTR_HIDDEN, 0x0001 }, // FileTypeHidden |
| 1590 | { OSFATTR_SYSTEM, 0x0002 }, // FileTypeSystem |
| 1591 | { OSFATTR_READ, 0x0004 }, // FileTypeRead |
| 1592 | { OSFATTR_WRITE, 0x0008 } // FileTypeWrite |
| 1593 | }; |
| 1594 | |
| 1595 | /* run through the bit flags, and set the translated bits */ |
| 1596 | int32_t tadsattrs = 0; |
| 1597 | for (int i = 0 ; i < countof(xlat) ; ++i) |
| 1598 | { |
| 1599 | /* if this OS mode bit is set, set the corresponding TADS bit */ |
| 1600 | if ((osattrs & xlat[i].osattr) != 0) |
| 1601 | tadsattrs |= xlat[i].tadsattr; |
| 1602 | } |
| 1603 | |
| 1604 | /* return the result */ |
| 1605 | return tadsattrs; |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * Push a file timestamp value |
| 1610 | */ |
| 1611 | static void push_file_time(VMG_ os_time_t t) |
| 1612 | { |
| 1613 | /* |
| 1614 | * if the time value is zero, it means that the local system doesn't |
| 1615 | * support this type of timestamp; push it as nil |
| 1616 | */ |
| 1617 | if (t == 0) |
| 1618 | { |
| 1619 | /* no time value - push nil */ |
| 1620 | G_stk->push()->set_nil(); |
| 1621 | } |
| 1622 | else |
| 1623 | { |
| 1624 | /* we have a valid time, so create a Date object to represent it */ |
| 1625 | G_stk->push()->set_obj(CVmObjDate::create_from_time_t(vmg_ FALSE, t)); |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * Get my special file type flags, if applicable. This inspects the root |
| 1631 | * name to see if it's a special relative link, such as Unix "." or "..". |
| 1632 | */ |
| 1633 | int32_t CVmObjFileName::special_filetype_flags(VMG0_) |
| 1634 | { |
| 1635 | /* no special flags so far */ |
| 1636 | int32_t flags = 0; |
| 1637 | |
| 1638 | /* get my extension */ |
| 1639 | vm_filnam_ext *ext = get_ext(); |
| 1640 | |
| 1641 | /* get the root name from my filename string */ |
| 1642 | const char *root = fn_get_root_name(vmg_ (char *)ext->get_str()); |
| 1643 | |
| 1644 | /* check the root name to see if it's special */ |
| 1645 | switch (fn_is_special_file(vmg_ root)) |
| 1646 | { |
| 1647 | case OS_SPECFILE_SELF: |
| 1648 | flags |= 0x0080; // FileTypeSelfLink |
| 1649 | break; |
| 1650 | |
| 1651 | case OS_SPECFILE_PARENT: |
| 1652 | flags |= 0x0100; // FileTypeParentLink |
| 1653 | break; |
| 1654 | |
| 1655 | default: |
| 1656 | break; |
| 1657 | } |
| 1658 | |
| 1659 | /* return the flags */ |
| 1660 | return flags; |
| 1661 | } |
| 1662 | |
| 1663 | /* ------------------------------------------------------------------------ */ |
| 1664 | /* |
| 1665 | * getFileType method |
| 1666 | */ |
| 1667 | int CVmObjFileName::getp_getFileType(VMG_ vm_obj_id_t self, |
| 1668 | vm_val_t *retval, uint *oargc) |
| 1669 | { |
| 1670 | /* check arguments */ |
| 1671 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1672 | static CVmNativeCodeDesc desc(0); |
| 1673 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1674 | return TRUE; |
| 1675 | |
| 1676 | /* get the filename; use the GETINFO access mode */ |
| 1677 | vm_rcdesc rc(vmg_ "FileName.getFileType", |
| 1678 | metaclass_reg_->get_class_obj(vmg0_), |
| 1679 | VMOFN_GETFILETYPE, G_stk->get(0), argc); |
| 1680 | CVmNetFile *netfile = CVmObjFile::get_filename_from_obj( |
| 1681 | vmg_ self, &rc, VMOBJFILE_ACCESS_GETINFO, |
| 1682 | OSFTUNK, "application/octet-stream"); |
| 1683 | |
| 1684 | /* check to see if we're following links (follow them by default) */ |
| 1685 | int follow_links = TRUE; |
| 1686 | if (argc >= 1) |
| 1687 | { |
| 1688 | /* |
| 1689 | * note that our 'asLink' parameter has the opposite sense from |
| 1690 | * 'follow_links', so invert the parameter value |
| 1691 | */ |
| 1692 | follow_links = !G_stk->get(0)->get_logical(); |
| 1693 | if (!G_stk->get(0)->is_logical()) |
| 1694 | err_throw(VMERR_BAD_VAL_BIF); |
| 1695 | } |
| 1696 | |
| 1697 | int ok; |
| 1698 | unsigned long osmode, osattr; |
| 1699 | err_try |
| 1700 | { |
| 1701 | /* get the file type from the netfile object */ |
| 1702 | ok = netfile->get_file_mode(vmg_ &osmode, &osattr, follow_links); |
| 1703 | } |
| 1704 | err_finally |
| 1705 | { |
| 1706 | /* done with the netfile object */ |
| 1707 | netfile->abandon(vmg0_); |
| 1708 | } |
| 1709 | err_end; |
| 1710 | |
| 1711 | /* figure the return value */ |
| 1712 | if (ok) |
| 1713 | { |
| 1714 | /* success - return the file mode, translated to FileTypeXxx bits */ |
| 1715 | retval->set_int( |
| 1716 | filemode_to_filetype(osmode) | special_filetype_flags(vmg0_)); |
| 1717 | } |
| 1718 | else |
| 1719 | { |
| 1720 | /* failed - simply return nil */ |
| 1721 | retval->set_nil(); |
| 1722 | } |
| 1723 | |
| 1724 | /* discard arguments */ |
| 1725 | G_stk->discard(argc); |
| 1726 | |
| 1727 | /* handled */ |
| 1728 | return TRUE; |
| 1729 | } |
| 1730 | |
| 1731 | /* ------------------------------------------------------------------------ */ |
| 1732 | /* |
| 1733 | * getFileInfo method |
| 1734 | */ |
| 1735 | int CVmObjFileName::getp_getFileInfo(VMG_ vm_obj_id_t self, |
| 1736 | vm_val_t *retval, uint *oargc) |
| 1737 | { |
| 1738 | /* check arguments */ |
| 1739 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1740 | static CVmNativeCodeDesc desc(0); |
| 1741 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1742 | return TRUE; |
| 1743 | |
| 1744 | /* get the filename; use the GETINFO access mode */ |
| 1745 | vm_rcdesc rc(vmg_ "FileName.getFileInfo", |
| 1746 | metaclass_reg_->get_class_obj(vmg0_), |
| 1747 | VMOFN_GETFILETYPE, G_stk->get(0), argc); |
| 1748 | CVmNetFile *netfile = CVmObjFile::get_filename_from_obj( |
| 1749 | vmg_ self, &rc, VMOBJFILE_ACCESS_GETINFO, |
| 1750 | OSFTUNK, "application/octet-stream"); |
| 1751 | |
| 1752 | /* check to see if we're following links (follow them by default) */ |
| 1753 | int follow_links = TRUE; |
| 1754 | if (argc >= 1) |
| 1755 | { |
| 1756 | /* |
| 1757 | * note that our 'asLink' parameter has the opposite sense from |
| 1758 | * 'follow_links', so invert the parameter value |
| 1759 | */ |
| 1760 | follow_links = G_stk->get(0)->get_logical(); |
| 1761 | if (!G_stk->get(0)->is_logical()) |
| 1762 | err_throw(VMERR_BAD_VAL_BIF); |
| 1763 | } |
| 1764 | |
| 1765 | int ok; |
| 1766 | os_file_stat_t stat; |
| 1767 | err_try |
| 1768 | { |
| 1769 | /* get the file type from the netfile object */ |
| 1770 | ok = netfile->get_file_stat(vmg_ &stat, follow_links); |
| 1771 | } |
| 1772 | err_finally |
| 1773 | { |
| 1774 | /* done with the netfile object */ |
| 1775 | netfile->abandon(vmg0_); |
| 1776 | } |
| 1777 | err_end; |
| 1778 | |
| 1779 | /* build the return value */ |
| 1780 | if (ok) |
| 1781 | { |
| 1782 | /* |
| 1783 | * Success - build a new FileInfo(mode, size, ctime, mtime, atime, |
| 1784 | * target, attrs). Start by pushing the constructor arguments |
| 1785 | * (last to first). |
| 1786 | */ |
| 1787 | |
| 1788 | /* push the file attributes */ |
| 1789 | G_stk->push()->set_int(map_file_attrs(stat.attrs)); |
| 1790 | |
| 1791 | /* if this is a symbolic link file, get the target path */ |
| 1792 | char target[OSFNMAX]; |
| 1793 | if ((stat.mode & OSFMODE_LINK) != 0 |
| 1794 | && netfile->resolve_symlink(vmg_ target, sizeof(target))) |
| 1795 | { |
| 1796 | /* push the name as a string in UTF8 */ |
| 1797 | G_stk->push()->set_obj(CVmObjString::create( |
| 1798 | vmg_ FALSE, target, strlen(target), G_cmap_from_fname)); |
| 1799 | } |
| 1800 | else |
| 1801 | { |
| 1802 | /* no symbolic link name - push nil as the symlink argument */ |
| 1803 | G_stk->push()->set_nil(); |
| 1804 | } |
| 1805 | |
| 1806 | /* push the timestamps */ |
| 1807 | push_file_time(vmg_ stat.acc_time); |
| 1808 | push_file_time(vmg_ stat.mod_time); |
| 1809 | push_file_time(vmg_ stat.cre_time); |
| 1810 | |
| 1811 | /* |
| 1812 | * Push the file size. If the high part is non-zero, or the low |
| 1813 | * part is greater than 0x7fffffff, the value won't fit in an |
| 1814 | * int32_t, so we need to push this as a BigNumber value. |
| 1815 | */ |
| 1816 | if (stat.sizehi != 0 || stat.sizelo > 0x7FFFFFFFU) |
| 1817 | { |
| 1818 | /* push it as a BigNumber value */ |
| 1819 | G_interpreter->push_obj(vmg_ CVmObjBigNum::create_int64( |
| 1820 | vmg_ FALSE, stat.sizehi, stat.sizelo)); |
| 1821 | } |
| 1822 | else |
| 1823 | { |
| 1824 | /* it'll fit in a simple integer value */ |
| 1825 | G_interpreter->push_int(vmg_ (int32_t)stat.sizelo); |
| 1826 | } |
| 1827 | |
| 1828 | /* push the file mode, translated to FileTypeXxx bits */ |
| 1829 | G_interpreter->push_int( |
| 1830 | vmg_ filemode_to_filetype(stat.mode) |
| 1831 | | special_filetype_flags(vmg0_)); |
| 1832 | |
| 1833 | /* |
| 1834 | * Call the constructor. If there's a FileInfo object exported, |
| 1835 | * create one of those. Otherwise just create a list from the |
| 1836 | * values. |
| 1837 | */ |
| 1838 | const int argc = 7; |
| 1839 | vm_obj_id_t fi = G_predef->file_info; |
| 1840 | if (fi != VM_INVALID_OBJ) |
| 1841 | { |
| 1842 | /* create the FileInfo instance */ |
| 1843 | vm_objp(vmg_ fi)->create_instance(vmg_ fi, 0, argc); |
| 1844 | |
| 1845 | /* make sure we got an object back */ |
| 1846 | if (G_interpreter->get_r0()->typ != VM_OBJ) |
| 1847 | err_throw(VMERR_OBJ_VAL_REQD); |
| 1848 | |
| 1849 | /* set the object return value */ |
| 1850 | retval->set_obj(G_interpreter->get_r0()->val.obj); |
| 1851 | } |
| 1852 | else |
| 1853 | { |
| 1854 | /* return the information as a list */ |
| 1855 | retval->set_obj(CVmObjList::create_from_stack(vmg_ 0, argc)); |
| 1856 | } |
| 1857 | } |
| 1858 | else |
| 1859 | { |
| 1860 | /* failed - simply return nil */ |
| 1861 | retval->set_nil(); |
| 1862 | } |
| 1863 | |
| 1864 | /* discard arguments */ |
| 1865 | G_stk->discard(argc); |
| 1866 | |
| 1867 | /* handled */ |
| 1868 | return TRUE; |
| 1869 | } |
| 1870 | |
| 1871 | /* ------------------------------------------------------------------------ */ |
| 1872 | /* |
| 1873 | * listDir method |
| 1874 | */ |
| 1875 | int CVmObjFileName::getp_listDir(VMG_ vm_obj_id_t self, |
| 1876 | vm_val_t *retval, uint *oargc) |
| 1877 | { |
| 1878 | /* check arguments */ |
| 1879 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1880 | static CVmNativeCodeDesc desc(0); |
| 1881 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1882 | return TRUE; |
| 1883 | |
| 1884 | /* get the filename; use the READ DIR access mode */ |
| 1885 | vm_rcdesc rc(vmg_ "FileName.listDir", |
| 1886 | metaclass_reg_->get_class_obj(vmg0_), |
| 1887 | VMOFN_LISTDIR, G_stk->get(0), argc); |
| 1888 | CVmNetFile *netfile = CVmObjFile::get_filename_from_obj( |
| 1889 | vmg_ self, &rc, VMOBJFILE_ACCESS_READDIR, |
| 1890 | OSFTUNK, "application/octet-stream"); |
| 1891 | |
| 1892 | err_try |
| 1893 | { |
| 1894 | /* get the directory listing */ |
| 1895 | if (!netfile->readdir(vmg_ get_ext()->get_str(), retval)) |
| 1896 | G_interpreter->throw_new_class(vmg_ G_predef->file_open_exc, |
| 1897 | 0, "error reading directory"); |
| 1898 | } |
| 1899 | err_finally |
| 1900 | { |
| 1901 | /* done with the netfile object */ |
| 1902 | netfile->abandon(vmg0_); |
| 1903 | } |
| 1904 | err_end; |
| 1905 | |
| 1906 | |
| 1907 | /* discard arguments */ |
| 1908 | G_stk->discard(argc); |
| 1909 | |
| 1910 | /* handled */ |
| 1911 | return TRUE; |
| 1912 | } |
| 1913 | |
| 1914 | /* ------------------------------------------------------------------------ */ |
| 1915 | /* |
| 1916 | * forEachFile method |
| 1917 | */ |
| 1918 | int CVmObjFileName::getp_forEachFile(VMG_ vm_obj_id_t self, |
| 1919 | vm_val_t *retval, uint *oargc) |
| 1920 | { |
| 1921 | /* check arguments */ |
| 1922 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1923 | static CVmNativeCodeDesc desc(1, 1); |
| 1924 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1925 | return TRUE; |
| 1926 | |
| 1927 | /* get the filename; use the READ DIR access mode */ |
| 1928 | vm_rcdesc rc(vmg_ "FileName.forEachFile", |
| 1929 | metaclass_reg_->get_class_obj(vmg0_), |
| 1930 | VMOFN_FOREACHFILE, G_stk->get(0), argc); |
| 1931 | CVmNetFile *netfile = CVmObjFile::get_filename_from_obj( |
| 1932 | vmg_ self, &rc, VMOBJFILE_ACCESS_READDIR, |
| 1933 | OSFTUNK, "application/octet-stream"); |
| 1934 | |
| 1935 | /* get the 'recursive' flag, if present */ |
| 1936 | int recursive = FALSE; |
| 1937 | if (argc >= 2) |
| 1938 | { |
| 1939 | if (G_stk->get(1)->is_logical()) |
| 1940 | recursive = G_stk->get(1)->get_logical(); |
| 1941 | else |
| 1942 | err_throw(VMERR_BAD_TYPE_BIF); |
| 1943 | } |
| 1944 | |
| 1945 | err_try |
| 1946 | { |
| 1947 | /* do the directory enumeration through the callback */ |
| 1948 | if (!netfile->readdir_cb(vmg_ get_ext()->get_str(), |
| 1949 | &rc, G_stk->get(0), recursive)) |
| 1950 | G_interpreter->throw_new_class(vmg_ G_predef->file_open_exc, |
| 1951 | 0, "error reading directory"); |
| 1952 | } |
| 1953 | err_finally |
| 1954 | { |
| 1955 | /* done with the netfile object */ |
| 1956 | netfile->abandon(vmg0_); |
| 1957 | } |
| 1958 | err_end; |
| 1959 | |
| 1960 | |
| 1961 | /* discard arguments */ |
| 1962 | G_stk->discard(argc); |
| 1963 | |
| 1964 | /* handled */ |
| 1965 | return TRUE; |
| 1966 | } |
| 1967 | |
| 1968 | /* ------------------------------------------------------------------------ */ |
| 1969 | /* |
| 1970 | * createDirectory method |
| 1971 | */ |
| 1972 | int CVmObjFileName::getp_createDirectory(VMG_ vm_obj_id_t self, |
| 1973 | vm_val_t *retval, uint *oargc) |
| 1974 | { |
| 1975 | /* check arguments */ |
| 1976 | uint argc = (oargc != 0 ? *oargc : 0); |
| 1977 | static CVmNativeCodeDesc desc(0, 1); |
| 1978 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 1979 | return TRUE; |
| 1980 | |
| 1981 | /* get the filename; use MKDIR access mode */ |
| 1982 | vm_rcdesc rc(vmg_ "FileName.createDirectory", |
| 1983 | metaclass_reg_->get_class_obj(vmg0_), |
| 1984 | VMOFN_CREATEDIR, G_stk->get(0), argc); |
| 1985 | CVmNetFile *netfile = CVmObjFile::get_filename_from_obj( |
| 1986 | vmg_ self, &rc, VMOBJFILE_ACCESS_MKDIR, |
| 1987 | OSFTUNK, "application/octet-stream"); |
| 1988 | |
| 1989 | err_try |
| 1990 | { |
| 1991 | /* get the "create parents" parameter */ |
| 1992 | int create_parents = FALSE; |
| 1993 | if (argc >= 1) |
| 1994 | { |
| 1995 | /* check that it's a true/nil value */ |
| 1996 | if (!G_stk->get(0)->is_logical()) |
| 1997 | err_throw(VMERR_BAD_TYPE_BIF); |
| 1998 | |
| 1999 | /* retrieve the flag */ |
| 2000 | create_parents = G_stk->get(0)->get_logical(); |
| 2001 | } |
| 2002 | |
| 2003 | /* create the directory */ |
| 2004 | netfile->mkdir(vmg_ create_parents); |
| 2005 | } |
| 2006 | err_finally |
| 2007 | { |
| 2008 | /* done with the netfile object */ |
| 2009 | netfile->abandon(vmg0_); |
| 2010 | } |
| 2011 | err_end; |
| 2012 | |
| 2013 | /* discard arguments */ |
| 2014 | G_stk->discard(argc); |
| 2015 | |
| 2016 | /* handled */ |
| 2017 | return TRUE; |
| 2018 | } |
| 2019 | |
| 2020 | /* ------------------------------------------------------------------------ */ |
| 2021 | /* |
| 2022 | * removeDirectory method |
| 2023 | */ |
| 2024 | int CVmObjFileName::getp_removeDirectory(VMG_ vm_obj_id_t self, |
| 2025 | vm_val_t *retval, uint *oargc) |
| 2026 | { |
| 2027 | /* check arguments */ |
| 2028 | uint argc = (oargc != 0 ? *oargc : 0); |
| 2029 | static CVmNativeCodeDesc desc(0, 1); |
| 2030 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 2031 | return TRUE; |
| 2032 | |
| 2033 | /* get the filename; use RMDIR access mode */ |
| 2034 | vm_rcdesc rc(vmg_ "FileName.removeDirectory", |
| 2035 | metaclass_reg_->get_class_obj(vmg0_), |
| 2036 | VMOFN_REMOVEDIR, G_stk->get(0), argc); |
| 2037 | CVmNetFile *netfile = CVmObjFile::get_filename_from_obj( |
| 2038 | vmg_ self, &rc, VMOBJFILE_ACCESS_RMDIR, |
| 2039 | OSFTUNK, "application/octet-stream"); |
| 2040 | |
| 2041 | err_try |
| 2042 | { |
| 2043 | /* get the "remove contents" parameter */ |
| 2044 | int remove_contents = FALSE; |
| 2045 | if (argc >= 1) |
| 2046 | { |
| 2047 | /* check that it's a true/nil value */ |
| 2048 | if (!G_stk->get(0)->is_logical()) |
| 2049 | err_throw(VMERR_BAD_TYPE_BIF); |
| 2050 | |
| 2051 | /* retrieve the flag */ |
| 2052 | remove_contents = G_stk->get(0)->get_logical(); |
| 2053 | } |
| 2054 | |
| 2055 | /* remove the directory */ |
| 2056 | netfile->rmdir(vmg_ remove_contents); |
| 2057 | } |
| 2058 | err_finally |
| 2059 | { |
| 2060 | /* done with the netfile object */ |
| 2061 | netfile->abandon(vmg0_); |
| 2062 | } |
| 2063 | err_end; |
| 2064 | |
| 2065 | /* discard arguments */ |
| 2066 | G_stk->discard(argc); |
| 2067 | |
| 2068 | /* handled */ |
| 2069 | return TRUE; |
| 2070 | } |
| 2071 |