| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2010 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 | vmtmpfil.cpp - CVmObjTemporaryFile object |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 11/20/10 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmtmpfil.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 "vmbif.h" |
| 38 | #include "vmmeta.h" |
| 39 | #include "vmstack.h" |
| 40 | #include "vmrun.h" |
| 41 | #include "vmstr.h" |
| 42 | #include "utf8.h" |
| 43 | |
| 44 | |
| 45 | /* ------------------------------------------------------------------------ */ |
| 46 | /* |
| 47 | * Allocate an extension structure |
| 48 | */ |
| 49 | vm_tmpfil_ext *vm_tmpfil_ext::alloc_ext(VMG_ CVmObjTemporaryFile *self, |
| 50 | const char *fname) |
| 51 | { |
| 52 | /* calculate how much space we need */ |
| 53 | size_t siz = sizeof(vm_tmpfil_ext) + strlen(fname); |
| 54 | |
| 55 | /* allocate the memory */ |
| 56 | vm_tmpfil_ext *ext = (vm_tmpfil_ext *)G_mem->get_var_heap()->alloc_mem( |
| 57 | siz, self); |
| 58 | |
| 59 | /* copy the filename */ |
| 60 | strcpy(ext->filename, fname); |
| 61 | |
| 62 | /* return the new extension */ |
| 63 | return ext; |
| 64 | } |
| 65 | |
| 66 | /* ------------------------------------------------------------------------ */ |
| 67 | /* |
| 68 | * CVmObjTemporaryFile object statics |
| 69 | */ |
| 70 | |
| 71 | /* metaclass registration object */ |
| 72 | static CVmMetaclassTemporaryFile metaclass_reg_obj; |
| 73 | CVmMetaclass *CVmObjTemporaryFile::metaclass_reg_ = &metaclass_reg_obj; |
| 74 | |
| 75 | /* function table */ |
| 76 | int (CVmObjTemporaryFile::*CVmObjTemporaryFile::func_table_[])( |
| 77 | VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc) = |
| 78 | { |
| 79 | &CVmObjTemporaryFile::getp_undef, /* 0 */ |
| 80 | &CVmObjTemporaryFile::getp_getFilename, /* 1 */ |
| 81 | &CVmObjTemporaryFile::getp_deleteFile /* 2 */ |
| 82 | }; |
| 83 | |
| 84 | /* ------------------------------------------------------------------------ */ |
| 85 | /* |
| 86 | * CVmObjTemporaryFile intrinsic class implementation |
| 87 | */ |
| 88 | |
| 89 | /* |
| 90 | * construction |
| 91 | */ |
| 92 | CVmObjTemporaryFile::CVmObjTemporaryFile(VMG_ const char *fname) |
| 93 | { |
| 94 | /* allocate our extension structure */ |
| 95 | ext_ = (char *)vm_tmpfil_ext::alloc_ext(vmg_ this, fname); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * create dynamically using stack arguments |
| 100 | */ |
| 101 | vm_obj_id_t CVmObjTemporaryFile::create_from_stack( |
| 102 | VMG_ const uchar **pc_ptr, uint argc) |
| 103 | { |
| 104 | /* check arguments */ |
| 105 | if (argc != 0) |
| 106 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 107 | |
| 108 | /* generate a temporary filename in the local file system */ |
| 109 | char fname[OSFNMAX]; |
| 110 | if (!os_gen_temp_filename(fname, sizeof(fname))) |
| 111 | err_throw(VMERR_CREATE_FILE); |
| 112 | |
| 113 | /* allocate the object ID and create the object */ |
| 114 | vm_obj_id_t id = vm_new_id(vmg_ FALSE, FALSE, FALSE); |
| 115 | new (vmg_ id) CVmObjTemporaryFile(vmg_ fname); |
| 116 | |
| 117 | /* temporary file objects are always transient */ |
| 118 | G_obj_table->set_obj_transient(id); |
| 119 | |
| 120 | /* return the new ID */ |
| 121 | return id; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * notify of deletion |
| 126 | */ |
| 127 | void CVmObjTemporaryFile::notify_delete(VMG_ int /*in_root_set*/) |
| 128 | { |
| 129 | /* free our additional data, if we have any */ |
| 130 | if (ext_ != 0) |
| 131 | { |
| 132 | /* delete the local file system object */ |
| 133 | if (get_ext()->filename[0] != '\0') |
| 134 | osfdel(get_ext()->filename); |
| 135 | |
| 136 | /* free the extension */ |
| 137 | G_mem->get_var_heap()->free_mem(ext_); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * set a property |
| 143 | */ |
| 144 | void CVmObjTemporaryFile::set_prop(VMG_ class CVmUndo *undo, |
| 145 | vm_obj_id_t self, vm_prop_id_t prop, |
| 146 | const vm_val_t *val) |
| 147 | { |
| 148 | /* no settable properties - throw an error */ |
| 149 | err_throw(VMERR_INVALID_SETPROP); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * get a property |
| 154 | */ |
| 155 | int CVmObjTemporaryFile::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 156 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 157 | uint *argc) |
| 158 | { |
| 159 | uint func_idx; |
| 160 | |
| 161 | /* translate the property into a function vector index */ |
| 162 | func_idx = G_meta_table |
| 163 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 164 | |
| 165 | /* call the appropriate function */ |
| 166 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 167 | { |
| 168 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 169 | return TRUE; |
| 170 | } |
| 171 | |
| 172 | /* inherit default handling from our base class */ |
| 173 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * apply an undo record |
| 178 | */ |
| 179 | void CVmObjTemporaryFile::apply_undo(VMG_ struct CVmUndoRecord *) |
| 180 | { |
| 181 | /* we're immutable, so there's no undo to worry about */ |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * discard extra undo information |
| 186 | */ |
| 187 | void CVmObjTemporaryFile::discard_undo(VMG_ CVmUndoRecord *) |
| 188 | { |
| 189 | /* we don't create undo records */ |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * load from an image file |
| 194 | */ |
| 195 | void CVmObjTemporaryFile::load_from_image(VMG_ vm_obj_id_t self, |
| 196 | const char *ptr, size_t siz) |
| 197 | { |
| 198 | /* load our image data */ |
| 199 | load_image_data(vmg_ ptr, siz); |
| 200 | |
| 201 | /* |
| 202 | * save our image data pointer in the object table, so that we can |
| 203 | * access it (without storing it ourselves) during a reload |
| 204 | */ |
| 205 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * reload from the image file |
| 210 | */ |
| 211 | void CVmObjTemporaryFile::reload_from_image(VMG_ vm_obj_id_t self, |
| 212 | const char *ptr, size_t siz) |
| 213 | { |
| 214 | /* load our image data */ |
| 215 | load_image_data(vmg_ ptr, siz); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * load or reload data from the image |
| 220 | */ |
| 221 | void CVmObjTemporaryFile::load_image_data(VMG_ const char *ptr, size_t siz) |
| 222 | { |
| 223 | /* free our existing extension, if we have one */ |
| 224 | if (ext_ != 0) |
| 225 | G_mem->get_var_heap()->free_mem(ext_); |
| 226 | |
| 227 | /* |
| 228 | * Allocate the extension. A temp file object loaded from the image is |
| 229 | * invalid; these objects are inherently transient. So simply use an |
| 230 | * empty filename to indicate that the temp file is not openable. |
| 231 | */ |
| 232 | vm_tmpfil_ext *ext = vm_tmpfil_ext::alloc_ext(vmg_ this, ""); |
| 233 | ext_ = (char *)ext; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /* |
| 238 | * save to a file |
| 239 | */ |
| 240 | void CVmObjTemporaryFile::save_to_file(VMG_ class CVmFile *) |
| 241 | { |
| 242 | /* temporary files are always transient, so should never be saved */ |
| 243 | assert(FALSE); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * restore from a file |
| 248 | */ |
| 249 | void CVmObjTemporaryFile::restore_from_file(VMG_ vm_obj_id_t /*self*/, |
| 250 | CVmFile *, CVmObjFixup *) |
| 251 | { |
| 252 | /* temporary files are transient, so should never be saved */ |
| 253 | } |
| 254 | |
| 255 | /* ------------------------------------------------------------------------ */ |
| 256 | /* |
| 257 | * getFilename() method - get the local file system object's name. |
| 258 | */ |
| 259 | int CVmObjTemporaryFile::getp_getFilename(VMG_ vm_obj_id_t self, |
| 260 | vm_val_t *retval, uint *oargc) |
| 261 | { |
| 262 | /* check arguments */ |
| 263 | static CVmNativeCodeDesc desc(0); |
| 264 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 265 | return TRUE; |
| 266 | |
| 267 | /* if we have a filename, return it, otherwise return nil */ |
| 268 | vm_tmpfil_ext *ext = get_ext(); |
| 269 | if (ext->filename[0] != '\0') |
| 270 | { |
| 271 | /* return the filename */ |
| 272 | retval->set_obj(CVmBif::str_from_ui_str(vmg_ ext->filename)); |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | /* no filename */ |
| 277 | retval->set_nil(); |
| 278 | } |
| 279 | |
| 280 | /* handled */ |
| 281 | return TRUE; |
| 282 | } |
| 283 | |
| 284 | /* ------------------------------------------------------------------------ */ |
| 285 | /* |
| 286 | * deleteFile() method. This explicitly releases the temp file, deleting |
| 287 | * the underlying file system object. It's not necessary for the program |
| 288 | * to call this, as we'll automatically release the file when this object |
| 289 | * is deleted by the garbage collector, but this method allows the program |
| 290 | * to explicitly release the resource as soon as it's done with it. |
| 291 | */ |
| 292 | int CVmObjTemporaryFile::getp_deleteFile(VMG_ vm_obj_id_t self, |
| 293 | vm_val_t *retval, uint *oargc) |
| 294 | { |
| 295 | /* check arguments */ |
| 296 | static CVmNativeCodeDesc desc(0); |
| 297 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 298 | return TRUE; |
| 299 | |
| 300 | /* if we have a filename, delete the file */ |
| 301 | vm_tmpfil_ext *ext = get_ext(); |
| 302 | if (ext->filename[0] != '\0') |
| 303 | { |
| 304 | /* delete the system file */ |
| 305 | osfdel(ext->filename); |
| 306 | } |
| 307 | |
| 308 | /* no return value */ |
| 309 | retval->set_nil(); |
| 310 | |
| 311 | /* handled */ |
| 312 | return TRUE; |
| 313 | } |