Skip to content

Commit d55d24a

Browse files
committed
Runtime: Unix.stat
1 parent e3be168 commit d55d24a

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

runtime/fs.js

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@
2121

2222
//Provides: caml_current_dir
2323
var caml_current_dir = "/";
24+
//Provides: file_inode
25+
var file_inode = 0
2426

2527
//Provides: MlDir
26-
function MlDir(){ this.content={};}
28+
//Requires: file_inode, unix_time
29+
function MlDir(){ this.content={};
30+
this.inode = file_inode++;
31+
var now = unix_time();
32+
this.atime = now;
33+
this.mtime = now;
34+
this.ctime = now;
35+
}
2736
MlDir.prototype = {
2837
exists:function(name){return this.content[name]?1:0;},
2938
mk:function(name,c){this.content[name]=c},
@@ -38,10 +47,24 @@ MlDir.prototype = {
3847
}
3948

4049
//Provides: MlFile
41-
//Requires: caml_create_string
42-
function MlFile(content){ this.data = content }
50+
//Requires: caml_create_string, file_inode, unix_time
51+
function MlFile(content){ this.data = content;
52+
this.inode = file_inode++;
53+
var now = unix_time();
54+
this.atime = now;
55+
this.mtime = now;
56+
this.ctime = now;
57+
}
4358
MlFile.prototype = {
44-
truncate:function(){ this.data = caml_create_string(0) }
59+
truncate:function(){
60+
this.data = caml_create_string(0);
61+
this.modified();
62+
},
63+
modified:function() {
64+
var now = unix_time();
65+
this.atime = now;
66+
this.mtime = now;
67+
}
4568
}
4669

4770
//Provides: caml_root_dir
@@ -265,3 +288,49 @@ function caml_ba_map_file(vfd, kind, layout, shared, dims, pos) {
265288
function caml_ba_map_file_bytecode(argv,argn){
266289
return caml_ba_map_file(argv[0],argv[1],argv[2],argv[3],argv[4],argv[5]);
267290
}
291+
292+
293+
//Provides: unix_stat_file
294+
//Requires: caml_make_path, caml_fs_content, MlFile, MlDir,caml_ml_string_length
295+
function unix_stat_file(f){
296+
if (f instanceof MlDir) {
297+
var kind = 1; //S_DIR
298+
var size = 0;
299+
}
300+
if (f instanceof MlFile) {
301+
var kind = 0; //S_REG
302+
var size = caml_ml_string_length(f.data);
303+
}
304+
305+
return [0,
306+
0, //st_dev
307+
f.inode, // st_ino
308+
kind, // st_kind
309+
436, //st_perm 0o664
310+
1, //st_nlink
311+
1, //st_uid
312+
1, //st_gid
313+
0, //st_rdev
314+
size,//st_size
315+
+f.atime,
316+
+f.mtime,
317+
+f.ctime
318+
]
319+
}
320+
321+
//Provides: unix_stat
322+
//Requires: caml_fs_content, caml_make_path, unix_stat_file
323+
function unix_stat(name){
324+
var f = caml_fs_content(caml_make_path(name));
325+
return unix_stat_file(f)
326+
}
327+
328+
//Provides: unix_lstat
329+
//Requires: unix_stat
330+
var unix_lstat = unix_stat
331+
332+
//Provides: unix_fstat
333+
//Requires: unix_stat_file, caml_global_data
334+
function unix_fstat(idx){
335+
return unix_stat_file(caml_global_data.fds[idx].file)
336+
}

runtime/io.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function caml_std_output(chan,s){
117117
chan.file.data = new_str;
118118
}
119119
chan.offset += slen;
120+
chan.file.modified();
120121
return 0;
121122
}
122123

@@ -209,12 +210,15 @@ function caml_ml_input (chan, s, i, l) {
209210

210211
//Provides: caml_fs_file_content
211212
//Requires: caml_string_of_array, caml_fs_content, caml_make_path, MlFile
212-
//Requires: caml_raise_not_found
213+
//Requires: caml_raise_not_found, unix_time
213214
function caml_fs_file_content(name) {
214215
var path = caml_make_path(name);
215216
var f = caml_fs_content(path);
216-
if(f instanceof MlFile)
217+
if(f instanceof MlFile){
218+
var now = unix_time();
219+
f.atime = now;
217220
return f.data;
221+
}
218222
caml_raise_not_found();
219223
}
220224

0 commit comments

Comments
 (0)