Debian Codegraph @

terps/tads/tads3/vmtmpfil.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2010 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmtmpfil.h - CVmObjTemporaryFile object
12 Function
13 The TemporaryFile intrinsic class describes a temp file on disk. This
14 isn't an open instance of a temp file; rather, it basically represents a
15 filename in the local file system.
16
17 The reason to make this an object is that it provides a natural way of
18 identifying and freeing temp files. The File class can validate that the
19 program is supplying a valid temporary file generated by the system,
20 because only the system can create a TemporaryFile object. This prevents a
21 malicious program from trying to circumvent the file safety levels by
22 faking a temp file name; it's essentially impossible to invent a fake name
23 since the name is encapsulated in the system object. The system can tell
24 when it's safe to delete the underlying file system object through the
25 magic of garbage collection: when the TemporaryFile object is collected,
26 the program is definitely done with it, since the program doesn't have any
27 references to it.
28 Notes
29
30 Modified
31 11/20/10 MJRoberts - Creation
32 */
33
34 #ifndef VMTMPFIL_H
35 #define VMTMPFIL_H
36
37 #include "t3std.h"
38 #include "vmtype.h"
39 #include "vmglob.h"
40 #include "vmobj.h"
41 #include "vmundo.h"
42
43
44 /* ------------------------------------------------------------------------ */
45 /*
46 * Image file data block:
47 *
48 * This object doesn't store anything in an image file. A temporary file
49 * object is completely transient and doesn't have anything to store.
50 */
51
52
53 /* ------------------------------------------------------------------------ */
54 /*
55 * Our in-memory extension data structure. This is usually a 'struct'
56 * that contains the same information as in the image file, but using
57 * native types.
58 */
59 struct vm_tmpfil_ext
60 {
61 /* allocate the structure */
62 static vm_tmpfil_ext *alloc_ext(
63 VMG_ class CVmObjTemporaryFile *self, const char *filename);
64
65 /*
66 * The filename, as a null-terminated string. We overallocate the
67 * structure with enough space. If this is set to an empty string, it
68 * means that the temporary file is no longer valid. This happens if
69 * the object is loaded from an image file or explicitly released via
70 * the deleteFile() method.
71 */
72 char filename[1];
73 };
74
75
76 /* ------------------------------------------------------------------------ */
77 /*
78 * CVmObjTemporaryFile intrinsic class definition
79 */
80
81 class CVmObjTemporaryFile: public CVmObject
82 {
83 friend class CVmMetaclassTemporaryFile;
84
85 public:
86 /* metaclass registration object */
87 static class CVmMetaclass *metaclass_reg_;
88 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
89
90 /* am I of the given metaclass? */
91 virtual int is_of_metaclass(class CVmMetaclass *meta) const
92 {
93 /* try my own metaclass and my base class */
94 return (meta == metaclass_reg_
95 || CVmObject::is_of_metaclass(meta));
96 }
97
98 /* is this a CVmObjTemporaryFile object? */
99 static int is_tmpfil_obj(VMG_ vm_obj_id_t obj)
100 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
101
102 /*
103 * Get the name of the temp file (as a null-terminated string).
104 * Returns null if the temp file is invalid.
105 */
106 const char *get_fname() const
107 {
108 const char *f = get_ext()->filename;
109 return f[0] != 0 ? f : 0;
110 }
111
112 /* create dynamically using stack arguments */
113 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
114 uint argc);
115
116 /*
117 * call a static property - we don't have any of our own, so simply
118 * "inherit" the base class handling
119 */
120 static int call_stat_prop(VMG_ vm_val_t *result,
121 const uchar **pc_ptr, uint *argc,
122 vm_prop_id_t prop)
123 {
124 return CVmObject::
125 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
126 }
127
128 /* notify of deletion */
129 void notify_delete(VMG_ int in_root_set);
130
131 /* set a property */
132 void set_prop(VMG_ class CVmUndo *undo,
133 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
134
135 /* get a property */
136 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
137 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
138
139 /*
140 * receive savepoint notification - we don't keep any
141 * savepoint-relative records, so we don't need to do anything here
142 */
143 void notify_new_savept() { }
144
145 /* apply an undo record */
146 void apply_undo(VMG_ struct CVmUndoRecord *rec);
147
148 /* discard an undo record */
149 void discard_undo(VMG_ struct CVmUndoRecord *);
150
151 /* mark our undo record references */
152 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
153
154 /* mark our references */
155 void mark_refs(VMG_ uint) { }
156
157 /* remove weak references */
158 void remove_stale_weak_refs(VMG0_) { }
159 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
160
161 /* load from an image file */
162 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
163
164 /* reload from an image file */
165 void reload_from_image(VMG_ vm_obj_id_t self,
166 const char *ptr, size_t siz);
167
168 /* rebuild for image file */
169 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
170
171 /* save to a file */
172 void save_to_file(VMG_ class CVmFile *fp);
173
174 /* restore from a file */
175 void restore_from_file(VMG_ vm_obj_id_t self,
176 class CVmFile *fp, class CVmObjFixup *fixups);
177
178 /* we're immutable, so we can't have changed since loading */
179 int is_changed_since_load() const { return FALSE; }
180
181 protected:
182 /* get my extension data */
183 vm_tmpfil_ext *get_ext() const { return (vm_tmpfil_ext *)ext_; }
184
185 /* load or reload image data */
186 void load_image_data(VMG_ const char *ptr, size_t siz);
187
188 /* create a with no initial contents */
189 CVmObjTemporaryFile() { ext_ = 0; }
190
191 /* create with contents */
192 CVmObjTemporaryFile(VMG_ const char *fname);
193
194 /* property evaluator - undefined function */
195 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
196
197 /* getFilename() method */
198 int getp_getFilename(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
199
200 /* deleteFile() method */
201 int getp_deleteFile(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
202
203 /* property evaluation function table */
204 static int (CVmObjTemporaryFile::*func_table_[])(VMG_ vm_obj_id_t self,
205 vm_val_t *retval, uint *argc);
206 };
207
208
209 /* ------------------------------------------------------------------------ */
210 /*
211 * CVmObjTemporaryFile metaclass registration table object
212 */
213 class CVmMetaclassTemporaryFile: public CVmMetaclass
214 {
215 public:
216 /* get the global name */
217 const char *get_meta_name() const { return "tempfile/030000"; }
218
219 /* create from image file */
220 void create_for_image_load(VMG_ vm_obj_id_t id)
221 {
222 new (vmg_ id) CVmObjTemporaryFile();
223 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
224 }
225
226 /* create from restoring from saved state */
227 void create_for_restore(VMG_ vm_obj_id_t id)
228 {
229 new (vmg_ id) CVmObjTemporaryFile();
230 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
231 }
232
233 /* create dynamically using stack arguments */
234 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
235 { return CVmObjTemporaryFile::create_from_stack(vmg_ pc_ptr, argc); }
236
237 /* call a static property */
238 int call_stat_prop(VMG_ vm_val_t *result,
239 const uchar **pc_ptr, uint *argc,
240 vm_prop_id_t prop)
241 {
242 return CVmObjTemporaryFile::
243 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
244 }
245 };
246
247 #endif /* VMTMPFIL_H */
248
249 /*
250 * Register the class
251 */
252 VM_REGISTER_METACLASS(CVmObjTemporaryFile)