filer/examples/idbfs-test4.html

161 lines
4.3 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="stdout"></div>
</body>
<script src="../lib/require.js"></script>
<script>
require.config({
baseUrl: "../lib",
paths: {
"src": "../src"
}
});
require(["buffer", "src/fs"], function(Buffer, IDBFS) {
var LF_NORMAL = "0",
LF_LINK = "1",
LF_SYMLINK = "2",
LF_CHR = "3",
LF_BLK = "4",
LF_DIR = "5",
LF_FIFO = "6",
LF_CONTIG = "7";
function Archive(buffer) {
this.files = [];
this.offset = 0;
this.offset = this.parseTarChunk(buffer, this.offset)
}
Archive.prototype = {
parseTarHeader: function parseTarHeader(buffer) {
var text = new Buffer(buffer).toString();
var i = 0;
var h = {};
h.filename = text.substring(i, i+=100).split("\0", 1)[0];
h.mode = text.substring(i, i+=8).split("\0", 1)[0];
h.uid = text.substring(i, i+=8).split("\0", 1)[0];
h.gid = text.substring(i, i+=8).split("\0", 1)[0];
h.length = parseInt(text.substring(i, i+=12), 8);
h.lastModified = text.substring(i, i+=12).split("\0", 1)[0];
h.checkSum = text.substring(i, i+=8).split("\0", 1)[0];
h.fileType = text.substring(i, i+=1).split("\0", 1)[0];
h.linkName = text.substring(i, i+=100).split("\0", 1)[0];
return h;
},
parseTarChunk: function parseTarChunk(buffer, offset) {
while (buffer.length >= offset + 512) {
var header = this.files.length == 0 ? null : this.files[this.files.length-1];
if (header && header.data == null) {
if (offset + header.length <= buffer.length) {
header.data = buffer.subarray(offset, offset+header.length);
offset += 512 * Math.ceil(header.length / 512);
} else { // not loaded yet
break;
}
} else {
var header = this.parseTarHeader(buffer.subarray(offset, offset+512));
if (header.length > 0 || header.filename != '') {
this.files.push(header);
offset += 512;
header.offset = offset;
} else { // empty header, stop processing
offset = buffer.length;
}
}
}
return offset;
}
};
var xhr = new XMLHttpRequest();
xhr.open('GET', './archive.tar', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
var buffer = new Uint8Array(this.response);
var archive = new Archive(buffer);
IDBFS.mount("default", "format", function(error, fs) {
if(error) {
return console.error(error);
}
function stat(path) {
fs.stat(path, function(error, result) {
if(error) {
return console.error(error);
}
console.info("stat " + path, result);
});
}
function ls(dir) {
fs.open(dir, "", "R", function(error, fd) {
contents = [];
fd.readdir(contents, undefined, function(error, count, contents) {
if(error) {
return console.error(error);
}
console.info("ls " + dir, contents);
fs.close(fd);
});
});
}
var i = 0;
function createNextFile(error) {
if(error) {
return console.error(error);
} else if(i >= archive.files.length) {
stat("/");
ls("/");
stat("/archive");
ls("/archive");
stat("/archive/js");
ls("/archive/js");
stat("/archive/js/gl-matrix.js");
stat("/archive/img");
ls("/archive/img");
stat("/archive/img/firefox_logo-only_RGB-300dpi.jpg");
} else {
var file = archive.files[i ++];
if(LF_DIR == file.fileType) {
fs.mkdir(file.filename, createNextFile);
} else {
fs.open(file.filename, "CREATE", "RW", function(error, fd) {
if(error) {
return console.error(error);
}
fd.write(file.data, function(error, bytes_written, buffer) {
if(error) {
return console.error(error);
}
fs.close(fd, createNextFile);
});
});
}
}
}
createNextFile();
});
};
xhr.send(null);
function unpack(archive) {
}
});
</script>
</html>