| 1 | /* $Header$ */ |
| 2 | |
| 3 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 4 | /* |
| 5 | Name |
| 6 | vmnetfil.h - network file interface |
| 7 | Function |
| 8 | This module was added in 3.1 to manage access to files stored on a |
| 9 | network storage server when the game is running in web server mode. In |
| 10 | this mode, files are not stored on the same machine as the game server, nor |
| 11 | on the user's client PC, but on a third machine known as the storage |
| 12 | server. The purpose of separating the game (execution) server and the |
| 13 | storage server is that it allows execution servers to be interchangeable, |
| 14 | as they don't store any state. This lets us launch a game on any available |
| 15 | server to distribute execution load. The execution location is transparent |
| 16 | to the user since all execution servers access the same storage pool. |
| 17 | |
| 18 | Access to remote files is handled by local caching. When we open a file |
| 19 | for reading, we download a temporary copy from the storage server via HTTP, |
| 20 | and perform the local file operations on the temp copy. If the file is |
| 21 | opened for writing, we upload our temporary copy to the storage server on |
| 22 | close, again via HTTP. |
| 23 | |
| 24 | The interface is implemented in two versions. The Network version senses |
| 25 | at run-time whether or not we're in web server mode; if so, it does the |
| 26 | HTTP GET/PUT operations to download and upload the temp files, and if not |
| 27 | it just opens files in the local file system. The Local version can be |
| 28 | used in builds that don't include the networking functionality at all; this |
| 29 | version unconditionally maps to local files, and is a very thin layer over |
| 30 | the regular file handling. |
| 31 | |
| 32 | Use the TADSNET macro to control the build configuration. If the macro |
| 33 | is defined, the network run-time checking is included in the build. If |
| 34 | not, the local-only version is selected, in which case the CVmNetFile |
| 35 | object is basically a no-op that just passes through the local filename. |
| 36 | Notes |
| 37 | |
| 38 | Modified |
| 39 | 09/08/10 MJRoberts - Creation |
| 40 | */ |
| 41 | |
| 42 | #ifndef VMNETFIL_H |
| 43 | #define VMNETFIL_H |
| 44 | |
| 45 | #include "t3std.h" |
| 46 | #include "vmtype.h" |
| 47 | #include "os.h" |
| 48 | |
| 49 | |
| 50 | /* ------------------------------------------------------------------------ */ |
| 51 | /* |
| 52 | * File modes for CVmNetFile::open() |
| 53 | */ |
| 54 | #define NETF_READ 0x0001 /* open with read access */ |
| 55 | #define NETF_WRITE 0x0002 /* open with write access */ |
| 56 | #define NETF_CREATE 0x0004 /* create the file if it doesn't exist */ |
| 57 | #define NETF_TRUNC 0x0008 /* discard existing contents if file exists */ |
| 58 | #define NETF_DELETE 0x0010 /* no content access; delete the file */ |
| 59 | |
| 60 | /* composite flags for generic new file creation (create or replace) */ |
| 61 | #define NETF_NEW (NETF_WRITE | NETF_CREATE | NETF_TRUNC) |
| 62 | |
| 63 | |
| 64 | |
| 65 | /* ------------------------------------------------------------------------ */ |
| 66 | /* |
| 67 | * Network file object. |
| 68 | * |
| 69 | * The protocol for operating on a user file is relatively simple to use: |
| 70 | * |
| 71 | * 1. Prepare the network file by calling CVmNetFile::open(). This returns |
| 72 | * a CVmNetFile object that you hang onto for the duration of the file |
| 73 | * manipulation - call this 'nfile'. |
| 74 | * |
| 75 | * 2. Use any local file system API (e.g., any of the osfopxxx() functions) |
| 76 | * to open 'nfile->lclfname'. |
| 77 | * |
| 78 | * 3. Use the local file system APIs (osfread(), osfwrite(), etc) to |
| 79 | * read/write the local file. |
| 80 | * |
| 81 | * 4. Close the local file (with osfcls(), say). |
| 82 | * |
| 83 | * 5. Close the network file by calling 'nfile->close()'. This has the |
| 84 | * side effect of destroying 'nfile', so you don't have to manually |
| 85 | * 'delete' it or otherwise dispose it. |
| 86 | * |
| 87 | * To retrofit this into existing osifc file management code, all that's |
| 88 | * required is to bracket the existing code with the CVmNetFile::open() and |
| 89 | * close() calls. Note that open() and close() can throw errors, so |
| 90 | * protect these calls with err_try if you have to do any resource cleanup |
| 91 | * if an error is thrown. |
| 92 | */ |
| 93 | class CVmNetFile |
| 94 | { |
| 95 | public: |
| 96 | /* |
| 97 | * Are we in network storage server mode? This returns true if we have |
| 98 | * a storage server, false if we're storing files in the local file |
| 99 | * system. |
| 100 | */ |
| 101 | static int is_net_mode(VMG0_) |
| 102 | #ifdef TADSNET |
| 103 | ; |
| 104 | #else |
| 105 | /* local-only implementation - we definitely use local storage only */ |
| 106 | { return FALSE; } |
| 107 | #endif |
| 108 | |
| 109 | /* |
| 110 | * Open a file. In local mode, this simply returns a CVmNetFile object |
| 111 | * with the given name in the lclfname field, so the caller will |
| 112 | * directly access the local file. For a network file, if the existing |
| 113 | * contents are needed, this downloads a copy of the network file to a |
| 114 | * local temporary file, otherwise it simply generates a name for a new |
| 115 | * local temporary file; in any case, it returns the temp file name in |
| 116 | * the lclfname field. |
| 117 | * |
| 118 | * If 'sfid' is non-zero, it's a CVmObjFile::SFID_xxx value specifying |
| 119 | * the special file ID. Use zero for an ordinary file. |
| 120 | * |
| 121 | * This throws an error on any download failure. |
| 122 | */ |
| 123 | static CVmNetFile *open(VMG_ const char *fname, int sfid, int mode, |
| 124 | os_filetype_t typ, const char *mime_type) |
| 125 | #ifdef TADSNET |
| 126 | ; |
| 127 | #else |
| 128 | { |
| 129 | /* |
| 130 | * Local implementation - access the local file directly. We don't |
| 131 | * care about MIME types for the local implementation, since our |
| 132 | * local file system interfaces don't use them. |
| 133 | */ |
| 134 | return new CVmNetFile(fname, sfid, 0, mode, typ, 0); |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | /* |
| 139 | * Open a file, based on an argument value from the byte-code program. |
| 140 | * This accepts string filenames, TemporaryFile (CVmObjTemporaryFile) |
| 141 | * objects, or TadsObject objects providing the file spec interface. |
| 142 | */ |
| 143 | static CVmNetFile *open( |
| 144 | VMG_ const vm_val_t *filespec, const struct vm_rcdesc *rc, |
| 145 | int mode, os_filetype_t typ, const char *mime_type); |
| 146 | |
| 147 | /* |
| 148 | * Open a file in local mode. This is essentially a no-op that allows |
| 149 | * a caller to explicitly manipulate a local file through code that's |
| 150 | * written to support network files, without adding a special case to |
| 151 | * that code. This routine creates a local file descriptor regardless |
| 152 | * of the interpreter mode. |
| 153 | */ |
| 154 | static CVmNetFile *open_local(VMG_ const char *fname, int sfid, |
| 155 | int mode, os_filetype_t typ) |
| 156 | { |
| 157 | /* return an explicitly local file descriptor */ |
| 158 | return new CVmNetFile(fname, sfid, 0, mode, typ, 0); |
| 159 | } |
| 160 | |
| 161 | /* rename a file */ |
| 162 | void rename_to_local(VMG_ CVmNetFile *newname); |
| 163 | void rename_to(VMG_ CVmNetFile *newname) |
| 164 | #ifdef TADSNET |
| 165 | ; |
| 166 | #else |
| 167 | { rename_to_local(vmg_ newname); } |
| 168 | #endif |
| 169 | |
| 170 | /* create a directory as named in the file object */ |
| 171 | void mkdir_local(VMG_ int create_parents); |
| 172 | void mkdir(VMG_ int create_parents) |
| 173 | #ifdef TADSNET |
| 174 | ; |
| 175 | #else |
| 176 | { mkdir_local(vmg_ create_parents); } |
| 177 | #endif |
| 178 | |
| 179 | /* remove the directory named in the file object */ |
| 180 | void rmdir_local(VMG_ int remove_contents); |
| 181 | void rmdir(VMG_ int remove_contents) |
| 182 | #ifdef TADSNET |
| 183 | ; |
| 184 | #else |
| 185 | { rmdir_local(vmg_ remove_contents); } |
| 186 | #endif |
| 187 | |
| 188 | /* |
| 189 | * Is this a network file? This returns true if the file is stored on |
| 190 | * the network, false if it's in the local file system. |
| 191 | */ |
| 192 | int is_net_file() const |
| 193 | { |
| 194 | /* it's a network file if it has a server-side filename */ |
| 195 | return srvfname != 0; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Get the file mode, per osfmode(). For a network file, the only |
| 200 | * possible mode is "file", since we don't support directories or other |
| 201 | * non-file types. On success, fills in *mode with a bitwise |
| 202 | * combination of OSFMODE_xxx flags and returns true; returns false on |
| 203 | * failure. |
| 204 | */ |
| 205 | int get_file_mode(VMG_ unsigned long *mode, unsigned long *attrs, |
| 206 | int follow_links) |
| 207 | #ifdef TADSNET |
| 208 | ; |
| 209 | #else |
| 210 | { |
| 211 | return osfmode(lclfname, follow_links, mode, attrs); |
| 212 | } |
| 213 | #endif |
| 214 | |
| 215 | /* |
| 216 | * Get the file stat() information, per os_file_stat(). This isn't |
| 217 | * supported for network files. |
| 218 | */ |
| 219 | int get_file_stat(VMG_ os_file_stat_t *stat, int follow_links) |
| 220 | #ifdef TADSNET |
| 221 | ; |
| 222 | #else |
| 223 | { |
| 224 | return os_file_stat(lclfname, follow_links, stat); |
| 225 | } |
| 226 | #endif |
| 227 | |
| 228 | /* |
| 229 | * Resolve a symbolic link. If this file is a symbolic link, this |
| 230 | * fills in 'target' with the target path and returns true. Otherwise |
| 231 | * returns false. |
| 232 | */ |
| 233 | int resolve_symlink(VMG_ char *target, size_t target_size) |
| 234 | #ifdef TADSNET |
| 235 | ; |
| 236 | #else |
| 237 | { |
| 238 | return os_resolve_symlink(lclfname, target, target_size); |
| 239 | } |
| 240 | #endif |
| 241 | |
| 242 | |
| 243 | /* |
| 244 | * Get a listing of files in the directory. Creates a list object, and |
| 245 | * fills in the list of FileName (CVmObjFileName) objects giving the |
| 246 | * names of the files in the directory. Returns true on success, false |
| 247 | * on failure. |
| 248 | * |
| 249 | * 'nominal_path' is an optional string to use as the displayed |
| 250 | * directory path name for the constructed FileName objects. If this |
| 251 | * is null, we'll use our actual internal local file path. The nominal |
| 252 | * path can be used to preserve the original relative path name, which |
| 253 | * might be desirable when the netfile object is resolved to an |
| 254 | * absolute path based on a working directory. |
| 255 | */ |
| 256 | int readdir(VMG_ const char *nominal_path, vm_val_t *retval) |
| 257 | #ifdef TADSNET |
| 258 | ; |
| 259 | #else |
| 260 | { |
| 261 | return readdir_local(vmg_ nominal_path, retval, 0, 0, 0); |
| 262 | } |
| 263 | #endif |
| 264 | |
| 265 | /* |
| 266 | * Enumerate files in the directory through a callback function. |
| 267 | */ |
| 268 | int readdir_cb(VMG_ const char *nominal_path, |
| 269 | const struct vm_rcdesc *rc, const vm_val_t *cb, |
| 270 | int recursive) |
| 271 | #ifdef TADSNET |
| 272 | ; |
| 273 | #else |
| 274 | { |
| 275 | return readdir_local(vmg_ nominal_path, 0, rc, cb, recursive); |
| 276 | } |
| 277 | #endif |
| 278 | |
| 279 | /* read a local directory */ |
| 280 | int readdir_local(VMG_ const char *nominal_path, |
| 281 | vm_val_t *retval, |
| 282 | const struct vm_rcdesc *rc, const vm_val_t *cb, |
| 283 | int recursive); |
| 284 | |
| 285 | /* |
| 286 | * Does the given file exist? Returns true if so, nil if not. |
| 287 | */ |
| 288 | static int exists(VMG_ const char *fname, int sfid) |
| 289 | #ifdef TADSNET |
| 290 | ; |
| 291 | #else |
| 292 | { |
| 293 | /* |
| 294 | * local-only mode: check the local file system; it exists if |
| 295 | * osfacc() returns success (zero) |
| 296 | */ |
| 297 | return !osfacc(fname); |
| 298 | } |
| 299 | #endif |
| 300 | |
| 301 | /* |
| 302 | * Test to see if we can write to the given file. We attempt to open |
| 303 | * the file for writing (retaining any existing data in the file, or |
| 304 | * creating a new file if it doesn't exist). If we succeed, we close |
| 305 | * the file (and delete it if it didn't exist), and return TRUE. If we |
| 306 | * fail, return FALSE. |
| 307 | */ |
| 308 | static int can_write(VMG_ const char *fname, int sfid) |
| 309 | #ifdef TADSNET |
| 310 | ; |
| 311 | #else |
| 312 | { |
| 313 | return can_write_local(fname); |
| 314 | } |
| 315 | #endif |
| 316 | |
| 317 | /* |
| 318 | * Close the file. Call this after closing the local file handle. For |
| 319 | * a network file opened with write access, this will copy the local |
| 320 | * temp copy back to the storage server, then delete the local temp |
| 321 | * copy. For any network file, deletes the local temp file. For all |
| 322 | * files, deletes the CVmNetFile object. |
| 323 | * |
| 324 | * Throws an error on upload failure. The CVmNetFile object is always |
| 325 | * reliably deleted, whether or not an error is thrown. |
| 326 | */ |
| 327 | void close(VMG0_) |
| 328 | #ifdef TADSNET |
| 329 | ; |
| 330 | #else |
| 331 | { |
| 332 | /* |
| 333 | * Local file: |
| 334 | * |
| 335 | * - if we opened it in "delete" mode, delete it |
| 336 | *. - if we opened it in "create" mode, set the file's OS type code |
| 337 | */ |
| 338 | int err = 0; |
| 339 | if ((mode & NETF_DELETE) != 0) |
| 340 | { |
| 341 | if (osfdel(lclfname)) |
| 342 | err = VMERR_DELETE_FILE; |
| 343 | } |
| 344 | else if ((mode & NETF_CREATE) != 0) |
| 345 | os_settype(lclfname, typ); |
| 346 | |
| 347 | /* delete self */ |
| 348 | delete this; |
| 349 | |
| 350 | /* if an error occurred, throw the error */ |
| 351 | if (err != 0) |
| 352 | err_throw(err); |
| 353 | } |
| 354 | #endif |
| 355 | |
| 356 | /* |
| 357 | * Abandon the network file. Call this if the local file manipulation |
| 358 | * fails, and the caller decides it doesn't want to save any changes |
| 359 | * back to the network after all. This deletes any local temp file, |
| 360 | * and frees the CVmNetFile object. Call this only after closing the |
| 361 | * local file handle. |
| 362 | */ |
| 363 | void abandon(VMG0_) |
| 364 | { |
| 365 | #ifdef TADSNET |
| 366 | /* delete the temp file */ |
| 367 | if (srvfname != 0) |
| 368 | osfdel(lclfname); |
| 369 | #endif |
| 370 | |
| 371 | /* free self */ |
| 372 | delete this; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Mark references for garbage collection. This marks our reference to |
| 377 | * the filespec object, if we have one. |
| 378 | */ |
| 379 | void mark_refs(VMG_ uint state); |
| 380 | |
| 381 | /* |
| 382 | * The local filename. After obtaining this object from vmnet_fopen(), |
| 383 | * the caller can use any osfopxxx() function to open the file. If |
| 384 | * we're using the network storage server, this will name a temp file |
| 385 | * in the system temp directory; if the file isn't on the storage |
| 386 | * server, this is simply the local file name. |
| 387 | */ |
| 388 | char *lclfname; |
| 389 | |
| 390 | /* special file ID */ |
| 391 | int sfid; |
| 392 | |
| 393 | /* |
| 394 | * The server filename. When we're operating on a storage server file, |
| 395 | * this is the name of the file on the server; otherwise it's null. |
| 396 | */ |
| 397 | char *srvfname; |
| 398 | |
| 399 | /* |
| 400 | * The file spec object. This can be a TemporaryFile object, or a |
| 401 | * TadsObject object with a file spec interface. If the file was |
| 402 | * opened based on a string filename, this is nil. |
| 403 | */ |
| 404 | vm_obj_id_t filespec; |
| 405 | |
| 406 | /* the file mode */ |
| 407 | int mode; |
| 408 | |
| 409 | /* is this a temporary file? */ |
| 410 | int is_temp; |
| 411 | |
| 412 | /* the OS file type */ |
| 413 | os_filetype_t typ; |
| 414 | |
| 415 | /* MIME type of the file */ |
| 416 | char *mime_type; |
| 417 | |
| 418 | protected: |
| 419 | CVmNetFile(const char *lclfname, int sfid, const char *srvfname, |
| 420 | int mode, os_filetype_t typ, const char *mime_type) |
| 421 | { |
| 422 | /* save the filenames, mode, and type */ |
| 423 | this->lclfname = lib_copy_str(lclfname); |
| 424 | this->srvfname = lib_copy_str(srvfname); |
| 425 | this->sfid = sfid; |
| 426 | this->mode = mode; |
| 427 | this->typ = typ; |
| 428 | this->mime_type = lib_copy_str(mime_type); |
| 429 | this->filespec = VM_INVALID_OBJ; |
| 430 | this->is_temp = FALSE; |
| 431 | } |
| 432 | |
| 433 | ~CVmNetFile() |
| 434 | { |
| 435 | lib_free_str(lclfname); |
| 436 | lib_free_str(srvfname); |
| 437 | lib_free_str(mime_type); |
| 438 | } |
| 439 | |
| 440 | /* build the full server-side filename for a given file */ |
| 441 | static const char *build_server_filename( |
| 442 | VMG_ char *dst, size_t dstsiz, const char *fname, int sfid); |
| 443 | |
| 444 | /* check to see if we can create/write to a local file */ |
| 445 | static int can_write_local(const char *fname) |
| 446 | { |
| 447 | /* note whether or not the file exists already */ |
| 448 | int existed = !osfacc(fname); |
| 449 | |
| 450 | /* |
| 451 | * try opening for read/write, keeping the existing contents or |
| 452 | * creating a new file if it doesn't exist |
| 453 | */ |
| 454 | osfildef *fp = osfoprwb(fname, OSFTBIN); |
| 455 | if (fp != 0) |
| 456 | { |
| 457 | /* |
| 458 | * Successfully opened the file, so we evidently can write it. |
| 459 | * We don't actually need to do anything with the file other |
| 460 | * than check that we could open it, so close it. |
| 461 | */ |
| 462 | osfcls(fp); |
| 463 | |
| 464 | /* if the file didn't exist before, restore the status quo ante */ |
| 465 | if (!existed) |
| 466 | osfdel(fname); |
| 467 | |
| 468 | /* indicate success */ |
| 469 | return TRUE; |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | /* couldn't open the file - return failure */ |
| 474 | return FALSE; |
| 475 | } |
| 476 | } |
| 477 | }; |
| 478 | |
| 479 | #endif /* VMNETFIL_H */ |