From 2d3d88d9558c0445119d8cbae55e8b73937778a4 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 25 Aug 2016 14:00:06 -0700 Subject: 3253 - writing to fake files in scenarios High time I committed the part that works. --- 088file.mu | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to '088file.mu') diff --git a/088file.mu b/088file.mu index 09d3b0fa..f2dde0a1 100644 --- a/088file.mu +++ b/088file.mu @@ -73,12 +73,21 @@ def transmit-from-text contents:address:array:character, sink:address:sink:chara def start-writing fs:address:filesystem, filename:address:array:character -> sink:address:sink:character, routine-id:number [ local-scope load-ingredients - file:number <- $open-file-for-writing filename source:address:source:character, sink:address:sink:character <- new-channel 30 - routine-id <- start-running transmit-to-file file, source + { + break-if fs + # real file system + file:number <- $open-file-for-writing filename + assert file, [no such file] + routine-id <- start-running transmit-to-file file, source + reply + } + # fake file system + # beware: doesn't support multiple concurrent writes yet + routine-id <- start-running transmit-to-fake-file fs, filename, source ] -def transmit-to-file file:number, source:address:source:character -> file:number, source:address:source:character [ +def transmit-to-file file:number, source:address:source:character -> source:address:source:character [ local-scope load-ingredients { @@ -89,3 +98,36 @@ def transmit-to-file file:number, source:address:source:character -> file:number } file <- $close-file file ] + +def transmit-to-fake-file fs:address:filesystem, filename:address:array:character, source:address:source:character -> fs:address:filesystem, source:address:source:character [ + local-scope + load-ingredients + # compute new file contents + buf:address:buffer <- new-buffer 30 + { + c:character, done?:boolean, source <- read source + break-if done? + buf <- append buf, c + loop + } + contents:address:array:character <- buffer-to-array buf + new-file-mapping:file-mapping <- merge filename, contents + # write to filesystem + curr-filename:address:array:character <- copy 0 + data:address:array:file-mapping <- get *fs, data:offset + # replace file contents if it already exists + len:number <- length *data + new-len:number <- add len, 1 + new-data:address:array:file-mapping <- new file-mapping:type, new-len + put *fs, data:offset, new-data + # copy over old files + i:number <- copy 0 + { + done?:boolean <- greater-or-equal i, len + break-if done? + tmp:file-mapping <- index *data, i + put-index *new-data, i, tmp + } + # write new file + put-index *new-data, len, new-file-mapping +] -- cgit 1.4.1-2-gfad0