| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 7 | /* |
| 8 | Name |
| 9 | vmnetfil.cpp - network file operations, local mode |
| 10 | Function |
| 11 | This module contains network file functions that are needed in both |
| 12 | server and local modes. This file should be linked into all builds, |
| 13 | whether or not the server mode is used. |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 09/08/10 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #include "t3std.h" |
| 21 | #include "os.h" |
| 22 | #include "osifcnet.h" |
| 23 | #include "vmnetfil.h" |
| 24 | #include "vmnet.h" |
| 25 | #include "vmfile.h" |
| 26 | #include "vmerr.h" |
| 27 | #include "vmerrnum.h" |
| 28 | #include "vmrun.h" |
| 29 | #include "vmglob.h" |
| 30 | #include "vmpredef.h" |
| 31 | #include "vmimport.h" |
| 32 | #include "sha2.h" |
| 33 | #include "vmhash.h" |
| 34 | #include "vmbif.h" |
| 35 | #include "vmtype.h" |
| 36 | #include "vmobj.h" |
| 37 | #include "vmtmpfil.h" |
| 38 | #include "vmtobj.h" |
| 39 | #include "vmimage.h" |
| 40 | #include "vmstack.h" |
| 41 | #include "charmap.h" |
| 42 | #include "vmlst.h" |
| 43 | #include "vmfilnam.h" |
| 44 | #include "vmfilobj.h" |
| 45 | #include "vmstr.h" |
| 46 | |
| 47 | |
| 48 | /* ------------------------------------------------------------------------ */ |
| 49 | /* |
| 50 | * Open a network file |
| 51 | */ |
| 52 | CVmNetFile *CVmNetFile::open(VMG_ const vm_val_t *val, const vm_rcdesc *rc, |
| 53 | int mode, os_filetype_t typ, |
| 54 | const char *mime_type) |
| 55 | { |
| 56 | vm_val_t filespec; |
| 57 | |
| 58 | /* check for a TadsObject implementing getFilename */ |
| 59 | if (G_predef->filespec_getFilename != VM_INVALID_PROP |
| 60 | && vm_val_cast_ok(CVmObjTads, val)) |
| 61 | { |
| 62 | /* call getFilename - the return value is the file spec */ |
| 63 | G_interpreter->get_prop( |
| 64 | vmg_ 0, val, G_predef->filespec_getFilename, val, 0, rc); |
| 65 | |
| 66 | /* the result is the real file spec */ |
| 67 | filespec = *G_interpreter->get_r0(); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | /* it's not a TadsObject, so it must directly have the file name */ |
| 72 | filespec = *val; |
| 73 | } |
| 74 | |
| 75 | /* check the file spec argument type */ |
| 76 | CVmNetFile *nf = 0; |
| 77 | CVmObjTemporaryFile *tmp = 0; |
| 78 | CVmObjFileName *ofn = 0; |
| 79 | if ((tmp = vm_val_cast(CVmObjTemporaryFile, &filespec)) != 0) |
| 80 | { |
| 81 | /* if the temporary file object is invalid, it's an error */ |
| 82 | if (tmp->get_fname() == 0) |
| 83 | err_throw(VMERR_CREATE_FILE); |
| 84 | |
| 85 | /* create the local file descriptor for the temp file path */ |
| 86 | nf = open_local(vmg_ tmp->get_fname(), 0, mode, typ); |
| 87 | |
| 88 | /* mark it as a temp file */ |
| 89 | nf->is_temp = TRUE; |
| 90 | } |
| 91 | else if (filespec.is_numeric(vmg0_) |
| 92 | || ((ofn = vm_val_cast(CVmObjFileName, &filespec)) != 0 |
| 93 | && ofn->is_special_file())) |
| 94 | { |
| 95 | /* |
| 96 | * It's a special file ID, either as an integer or as a FileName |
| 97 | * wrapping a special file int. Get the value. |
| 98 | */ |
| 99 | int32_t sfid = (ofn != 0 ? ofn->get_sfid() |
| 100 | : filespec.num_to_int(vmg0_)); |
| 101 | |
| 102 | /* resolve the file system path for the given special file ID */ |
| 103 | char fname[OSFNMAX] = { '\0' }; |
| 104 | if (!CVmObjFile::sfid_to_path(vmg_ fname, sizeof(fname), sfid)) |
| 105 | err_throw(VMERR_BAD_VAL_BIF); |
| 106 | |
| 107 | /* create the special file descriptor */ |
| 108 | nf = open(vmg_ fname, sfid, mode, typ, mime_type); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | /* anything else has to be a string */ |
| 113 | char fname[OSFNMAX]; |
| 114 | CVmBif::get_fname_val(vmg_ fname, sizeof(fname), &filespec); |
| 115 | |
| 116 | /* |
| 117 | * if it's a local file, and it has a relative path, explicitly |
| 118 | * apply the image file path as the default working directory |
| 119 | */ |
| 120 | if (!os_is_file_absolute(fname) && !is_net_mode(vmg0_)) |
| 121 | { |
| 122 | /* build the full, absolute name based on the file base path */ |
| 123 | char fnabs[OSFNMAX]; |
| 124 | os_build_full_path(fnabs, sizeof(fnabs), G_file_path, fname); |
| 125 | |
| 126 | /* replace the relative path with the new absolute path */ |
| 127 | lib_strcpy(fname, sizeof(fname), fnabs); |
| 128 | } |
| 129 | |
| 130 | /* create the regular network file descriptor */ |
| 131 | nf = open(vmg_ fname, 0, mode, typ, mime_type); |
| 132 | } |
| 133 | |
| 134 | /* if they gave us an object as our file spec, remember it */ |
| 135 | if (nf != 0 && val->typ == VM_OBJ) |
| 136 | nf->filespec = val->val.obj; |
| 137 | |
| 138 | /* return the network file descriptor */ |
| 139 | return nf; |
| 140 | } |
| 141 | |
| 142 | /* ------------------------------------------------------------------------ */ |
| 143 | /* |
| 144 | * Rename a file |
| 145 | */ |
| 146 | void CVmNetFile::rename_to_local(VMG_ CVmNetFile *newname) |
| 147 | { |
| 148 | /* if the new name isn't local, this isn't supported */ |
| 149 | if (newname->is_net_file()) |
| 150 | err_throw(VMERR_RENAME_FILE); |
| 151 | |
| 152 | /* if the destination file already exists, it's an error */ |
| 153 | if (!osfacc(newname->lclfname)) |
| 154 | err_throw(VMERR_RENAME_FILE); |
| 155 | |
| 156 | /* do the rename */ |
| 157 | if (!os_rename_file(lclfname, newname->lclfname)) |
| 158 | err_throw(VMERR_RENAME_FILE); |
| 159 | } |
| 160 | |
| 161 | /* ------------------------------------------------------------------------ */ |
| 162 | /* |
| 163 | * Create a local directory |
| 164 | */ |
| 165 | void CVmNetFile::mkdir_local(VMG_ int create_parents) |
| 166 | { |
| 167 | /* try creating the directory */ |
| 168 | if (!os_mkdir(lclfname, create_parents)) |
| 169 | err_throw(VMERR_CREATE_FILE); |
| 170 | } |
| 171 | |
| 172 | /* ------------------------------------------------------------------------ */ |
| 173 | /* |
| 174 | * Empty a local directory |
| 175 | */ |
| 176 | static void empty_dir(VMG_ const char *dir) |
| 177 | { |
| 178 | /* open the directory search */ |
| 179 | osdirhdl_t dirhdl; |
| 180 | if (os_open_dir(dir, &dirhdl)) |
| 181 | { |
| 182 | err_try |
| 183 | { |
| 184 | /* keep going until we're out of files */ |
| 185 | char fname[OSFNMAX]; |
| 186 | while (os_read_dir(dirhdl, fname, sizeof(fname))) |
| 187 | { |
| 188 | /* get the full path */ |
| 189 | char path[OSFNMAX]; |
| 190 | os_build_full_path(path, sizeof(path), dir, fname); |
| 191 | |
| 192 | /* get the mode */ |
| 193 | unsigned long fmode; |
| 194 | unsigned long fattr; |
| 195 | if (osfmode(path, FALSE, &fmode, &fattr)) |
| 196 | { |
| 197 | /* check whether it's a directory or an ordinary file */ |
| 198 | if ((fmode & OSFMODE_DIR) != 0) |
| 199 | { |
| 200 | /* |
| 201 | * directory - skip the special '.' and '..' links, |
| 202 | * since they'd get us stuck in a loop |
| 203 | */ |
| 204 | os_specfile_t st = os_is_special_file(fname); |
| 205 | if (st != OS_SPECFILE_SELF && st != OS_SPECFILE_PARENT) |
| 206 | { |
| 207 | /* recursively empty the directory */ |
| 208 | empty_dir(vmg_ path); |
| 209 | |
| 210 | /* remove this directory */ |
| 211 | if (!os_rmdir(path)) |
| 212 | err_throw(VMERR_DELETE_FILE); |
| 213 | } |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | /* ordinary file - delete it */ |
| 218 | if (osfdel(path)) |
| 219 | err_throw(VMERR_DELETE_FILE); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | err_finally |
| 225 | { |
| 226 | /* close the directory search handle */ |
| 227 | os_close_dir(dirhdl); |
| 228 | } |
| 229 | err_end; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Remove a local directory |
| 235 | */ |
| 236 | void CVmNetFile::rmdir_local(VMG_ int remove_contents) |
| 237 | { |
| 238 | /* if desired, recursively remove the directory's contents */ |
| 239 | if (remove_contents) |
| 240 | empty_dir(vmg_ lclfname); |
| 241 | |
| 242 | /* try removing the directory */ |
| 243 | if (!os_rmdir(lclfname)) |
| 244 | err_throw(VMERR_DELETE_FILE); |
| 245 | } |
| 246 | |
| 247 | /* ------------------------------------------------------------------------ */ |
| 248 | /* |
| 249 | * static directory lister |
| 250 | */ |
| 251 | static int s_readdir_local(VMG_ const char *lclfname, |
| 252 | const char *nominal_path, |
| 253 | vm_val_t *retval, const vm_rcdesc *rc, |
| 254 | const vm_val_t *cb, int recursive) |
| 255 | { |
| 256 | /* note the nominal path string length */ |
| 257 | size_t nominal_path_len = strlen(nominal_path); |
| 258 | |
| 259 | /* |
| 260 | * If the caller wants a result list, create a list for the return |
| 261 | * value. We don't know how many elements the list will need, so |
| 262 | * create it with an arbitrary initial size, and expand it later as |
| 263 | * needed. |
| 264 | */ |
| 265 | CVmObjList *lst = 0; |
| 266 | if (retval != 0) |
| 267 | { |
| 268 | /* create the list */ |
| 269 | const size_t initlen = 32; |
| 270 | retval->set_obj(CVmObjList::create(vmg_ FALSE, initlen)); |
| 271 | lst = (CVmObjList *)vm_objp(vmg_ retval->val.obj); |
| 272 | |
| 273 | /* clear it */ |
| 274 | lst->cons_clear(0, initlen - 1); |
| 275 | |
| 276 | /* save the new list on the stack for gc protection */ |
| 277 | G_stk->push(retval); |
| 278 | } |
| 279 | |
| 280 | /* presume success */ |
| 281 | int ok = TRUE; |
| 282 | |
| 283 | /* open the directory */ |
| 284 | osdirhdl_t dirhdl; |
| 285 | if (os_open_dir(lclfname, &dirhdl)) |
| 286 | { |
| 287 | err_try |
| 288 | { |
| 289 | /* iterate over the directory's contents */ |
| 290 | int idx = 0; |
| 291 | char curname[OSFNMAX]; |
| 292 | while (os_read_dir(dirhdl, curname, sizeof(curname))) |
| 293 | { |
| 294 | char *unm = 0; |
| 295 | vm_obj_id_t fnobj = VM_INVALID_OBJ; |
| 296 | |
| 297 | err_try |
| 298 | { |
| 299 | /* map the filename to UTF8 */ |
| 300 | size_t unmlen = G_cmap_from_fname->map_str_alo( |
| 301 | &unm, curname); |
| 302 | |
| 303 | /* create the FileName object */ |
| 304 | fnobj = CVmObjFileName::combine_path( |
| 305 | vmg_ nominal_path, nominal_path_len, |
| 306 | unm, unmlen, TRUE); |
| 307 | |
| 308 | /* push it for gc protection */ |
| 309 | G_stk->push()->set_obj(fnobj); |
| 310 | } |
| 311 | err_finally |
| 312 | { |
| 313 | if (unm != 0) |
| 314 | t3free(unm); |
| 315 | } |
| 316 | err_end; |
| 317 | |
| 318 | /* if we're building a list, add the file to the list */ |
| 319 | if (retval != 0) |
| 320 | { |
| 321 | vm_val_t ele; |
| 322 | ele.set_obj(fnobj); |
| 323 | lst->cons_ensure_space(vmg_ idx, 16); |
| 324 | lst->cons_set_element(idx, &ele); |
| 325 | lst->cons_set_len(++idx); |
| 326 | } |
| 327 | |
| 328 | /* if there's a callback, invoke it */ |
| 329 | if (cb != 0) |
| 330 | { |
| 331 | G_stk->push()->set_obj(fnobj); |
| 332 | G_interpreter->call_func_ptr(vmg_ cb, 1, rc, 0); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * If we're doing a recursive listing, and this is a |
| 337 | * directory, list its ocntents. Skip self and parent |
| 338 | * directory links ('.' and '..' on Unix), since those |
| 339 | * would cause infinite recursion. |
| 340 | */ |
| 341 | os_specfile_t st; |
| 342 | if (recursive |
| 343 | && (st = os_is_special_file(curname)) != OS_SPECFILE_SELF |
| 344 | && st != OS_SPECFILE_PARENT) |
| 345 | { |
| 346 | /* build the full path name */ |
| 347 | char fullname[OSFNMAX]; |
| 348 | os_build_full_path(fullname, sizeof(fullname), |
| 349 | lclfname, curname); |
| 350 | |
| 351 | /* check to see if it's a directory */ |
| 352 | unsigned long fmode; |
| 353 | unsigned long fattr; |
| 354 | if (osfmode(fullname, FALSE, &fmode, &fattr) |
| 355 | && (fmode & OSFMODE_DIR) != 0) |
| 356 | { |
| 357 | /* get the combined path from the FileName object */ |
| 358 | const char *path = vm_objid_cast(CVmObjFileName, fnobj) |
| 359 | ->get_path_string(); |
| 360 | |
| 361 | /* build the actual combined path */ |
| 362 | char subfname[OSFNMAX]; |
| 363 | os_build_full_path( |
| 364 | subfname, sizeof(subfname), lclfname, curname); |
| 365 | |
| 366 | /* do the recursive listing */ |
| 367 | ok |= s_readdir_local(vmg_ subfname, path + VMB_LEN, |
| 368 | retval, rc, cb, TRUE); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* we're done with the FileName object for this iteration */ |
| 373 | G_stk->discard(1); |
| 374 | } |
| 375 | } |
| 376 | err_finally |
| 377 | { |
| 378 | /* close the directory handle */ |
| 379 | os_close_dir(dirhdl); |
| 380 | } |
| 381 | err_end; |
| 382 | } |
| 383 | |
| 384 | /* discard the gc protection for the list, if applicable */ |
| 385 | if (retval != 0) |
| 386 | G_stk->discard(1); |
| 387 | |
| 388 | /* return the result */ |
| 389 | return ok; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Get a local directory listing, returning a list of FileName objects |
| 394 | * and/or invoking a callback for each file found. |
| 395 | */ |
| 396 | int CVmNetFile::readdir_local(VMG_ const char *nominal_path, |
| 397 | vm_val_t *retval, |
| 398 | const struct vm_rcdesc *rc, |
| 399 | const vm_val_t *cb, int recursive) |
| 400 | { |
| 401 | /* verify that the path exists and refers to a directory */ |
| 402 | unsigned long mode; |
| 403 | unsigned long attr; |
| 404 | if (!osfmode(lclfname, TRUE, &mode, &attr) |
| 405 | || (mode & OSFMODE_DIR) == 0) |
| 406 | return FALSE; |
| 407 | |
| 408 | /* |
| 409 | * if the caller didn't specify a nominal path to use in constructed |
| 410 | * FileName objects, use the actual local path |
| 411 | */ |
| 412 | if (nominal_path == 0) |
| 413 | nominal_path = lclfname; |
| 414 | |
| 415 | /* call our static implementation with our local filename path */ |
| 416 | return s_readdir_local(vmg_ lclfname, nominal_path, |
| 417 | retval, rc, cb, recursive); |
| 418 | } |
| 419 | |
| 420 | /* ------------------------------------------------------------------------ */ |
| 421 | /* |
| 422 | * Mark references for the garbage collector |
| 423 | */ |
| 424 | void CVmNetFile::mark_refs(VMG_ uint state) |
| 425 | { |
| 426 | /* if we have a filespec object, mark it */ |
| 427 | if (filespec != VM_INVALID_OBJ) |
| 428 | G_obj_table->mark_all_refs(filespec, state); |
| 429 | } |
| 430 |