clean up logging, comments, and caching of open file handlers

This commit is contained in:
Myk Melez 2015-02-06 14:35:51 -08:00
Родитель 783b0248cc
Коммит 426c5353ca
1 изменённых файлов: 78 добавлений и 60 удалений

Просмотреть файл

@ -357,7 +357,7 @@ Native["javax/microedition/io/file/FileSystemRegistry.getRootsImpl.()[Ljava/lang
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.create.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.create: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.create: " + pathname);
var stat = fs.stat(pathname);
@ -367,11 +367,9 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.create.()V"] = function() {
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.exists.()Z"] = function() {
// console.log("exists: " + [p for (p in this)].join(","));
// console.log("exists: " + (this["$fileName"].toNumber()));
var pathname = util.fromJavaString(this.$nativePath);
var exists = fs.exists(pathname);
console.log("DefaultFileHandler.exists: " + pathname + " " + exists);
DEBUG_FS && console.log("DefaultFileHandler.exists: " + pathname + " " + exists);
return exists ? 1 : 0;
};
@ -379,13 +377,13 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.isDirectory.()Z"] = function
var pathname = util.fromJavaString(this.$nativePath);
var stat = fs.stat(pathname);
var isDirectory = !!stat && stat.isDir;
console.log("DefaultFileHandler.isDirectory: " + pathname + " " + (isDirectory));
DEBUG_FS && console.log("DefaultFileHandler.isDirectory: " + pathname + " " + isDirectory);
return isDirectory ? 1 : 0;
}
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.delete.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.delete: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.delete: " + pathname);
if (!fs.remove(pathname)) {
throw $.newIOException();
}
@ -394,17 +392,21 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.delete.()V"] = function() {
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.rename0.(Ljava/lang/String;)V"] = function(newName) {
var pathname = util.fromJavaString(this.$nativePath);
console.log("current nativePath: " + util.fromJavaString(this.$nativePath));
console.log("DefaultFileHandler.rename0: " + pathname);
console.log("newName: " + fs.normalize(util.fromJavaString(newName)));
if (fs.exists(util.fromJavaString(newName)) || !fs.rename(pathname, util.fromJavaString(newName))) {
throw $.newIOException("Rename failed");
var newPathname = util.fromJavaString(newName);
DEBUG_FS && console.log("DefaultFileHandler.rename0: " + pathname + " to " + newPathname);
if (fs.exists(newPathname)) {
throw $.newIOException("file with new name exists");
}
if (!fs.rename(pathname, newPathname)) {
throw $.newIOException("error renaming file");
}
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.truncate.(J)V"] = function(byteOffset) {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.lastModified: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.lastModified: " + pathname);
var stat = fs.stat(pathname);
@ -416,14 +418,14 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.truncate.(J)V"] = function(b
throw $.newIOException("file is directory");
}
// TODO If the file is open, flush it first.
// TODO: If the file is open, flush it first.
fs.truncate(pathname, byteOffset.toNumber());
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.fileSize.()J"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.fileSize: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.fileSize: " + pathname);
return Long.fromNumber(fs.size(pathname));
};
@ -432,13 +434,13 @@ addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.directorySiz
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.canRead.()Z"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.canRead: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.canRead: " + pathname);
return fs.exists(pathname) ? 1 : 0;
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.canWrite.()Z"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.canWrite: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.canWrite: " + pathname);
return fs.exists(pathname) ? 1 : 0;
};
@ -453,6 +455,8 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.setReadable.(Z)V"] = functio
if (!fs.exists(pathname)) {
throw $.newIOException("file does not exist");
}
// Otherwise this is a noop, as files are always readable in our filesystem.
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.setWritable.(Z)V"] = function() {
@ -460,13 +464,15 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.setWritable.(Z)V"] = functio
if (!fs.exists(pathname)) {
throw $.newIOException("file does not exist");
}
// Otherwise this is a noop, as files are always writable in our filesystem.
};
addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.setHidden0.(Z)V");
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.mkdir.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.mkdir: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.mkdir: " + pathname);
if (!fs.mkdir(pathname)) {
throw $.newIOException("error creating " + pathname);
@ -481,40 +487,44 @@ addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.totalSize.()
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.lastModified.()J"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.lastModified: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.lastModified: " + pathname);
var stat = fs.stat(pathname);
return Long.fromNumber(stat != null ? stat.mtime : 0);
};
MIDP.openFiles = new Map();
// A map from DefaultFileHandler instances with open input/output streams
// to the file descriptors used by the fs API to identify open files.
MIDP.openFileHandlers = new Map();
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForRead.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.openForRead: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.openForRead: " + pathname);
var fileNumber = this.$fileName;
if (MIDP.openFiles.has(fileNumber)) {
// Already open.
// XXX Should we track position separately for the input and output
if (MIDP.openFileHandlers.has(this)) {
// The file is already open, so we reset its position so the caller
// will start reading from the beginning of the file.
// XXX Should we track the position separately for the input and output
// streams?
var fd = MIDP.openFiles.get(fileNumber);
var fd = MIDP.openFileHandlers.get(this);
fs.setpos(fd, 0);
return;
}
var stat = fs.stat(pathname);
if (!stat) {
throw $.newIOException("file doesn't exist");
}
if (stat.isDir) {
throw $.newIOException("file is a directory");
}
var fileHandler = this;
asyncImpl("I", new Promise(function(resolve, reject) {
fs.open(pathname, function(fd) {
// The key is the $fileName Long, not the primitive number
// it represents, so a file can be opened by multiple handlers.
MIDP.openFiles.set(fileNumber, fd);
MIDP.openFileHandlers.set(fileHandler, fd);
resolve();
});
}));
@ -522,35 +532,43 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForRead.()V"] = function
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForRead.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.closeForRead: " + pathname);
var fd = MIDP.openFiles.get(this.$fileName);
DEBUG_FS && console.log("DefaultFileHandler.closeForRead: " + pathname);
var fd = MIDP.openFileHandlers.get(this);
fs.close(fd);
MIDP.openFiles.delete(this.$fileName);
MIDP.openFileHandlers.delete(this);
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForWrite.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.openForWrite: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.openForWrite: " + pathname);
var fileNumber = this.$fileName;
if (MIDP.openFiles.has(fileNumber)) {
// Already open.
if (MIDP.openFileHandlers.has(this)) {
// The file is already open, so we reset its position so the caller
// will start writing from the beginning of the file.
// XXX Should we track the position separately for the input and output
// streams?
var fd = MIDP.openFileHandlers.get(this);
fs.setpos(fd, 0);
return;
}
var stat = fs.stat(pathname);
if (!stat) {
throw $.newIOException("file doesn't exist");
throw $.newIOException("file does not exist");
}
if (stat.isDir) {
throw $.newIOException("file is a directory");
}
var fileHandler = this;
asyncImpl("I", new Promise(function(resolve, reject) {
fs.open(pathname, function(fd) {
// The key is the $fileName Long, not the primitive number
// it represents, so a file can be opened by multiple handlers.
MIDP.openFiles.set(fileNumber, fd);
MIDP.openFileHandlers.set(fileHandler, fd);
resolve();
});
}));
@ -558,24 +576,24 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForWrite.()V"] = functio
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForWrite.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.closeForWrite: " + pathname);
var fd = MIDP.openFiles.get(this.$fileName);
DEBUG_FS && console.log("DefaultFileHandler.closeForWrite: " + pathname);
var fd = MIDP.openFileHandlers.get(this);
fs.close(fd);
MIDP.openFiles.delete(this.$fileName);
MIDP.openFileHandlers.delete(this);
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForReadWrite.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.closeForReadWrite: " + pathname);
var fd = MIDP.openFiles.get(this.$fileName);
DEBUG_FS && console.log("DefaultFileHandler.closeForReadWrite: " + pathname);
var fd = MIDP.openFileHandlers.get(this);
fs.close(fd);
MIDP.openFiles.delete(this.$fileName);
MIDP.openFileHandlers.delete(this);
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.read.([BII)I"] = function(b, off, len) {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.read: " + pathname);
var fd = MIDP.openFiles.get(this.$fileName);
DEBUG_FS && console.log("DefaultFileHandler.read: " + pathname);
var fd = MIDP.openFileHandlers.get(this);
if (off < 0 || len < 0 || off > b.byteLength || (b.byteLength - off) < len) {
throw $.newIOException();
@ -594,8 +612,8 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.read.([BII)I"] = function(b,
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.write.([BII)I"] = function(b, off, len) {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.write: " + pathname);
var fd = MIDP.openFiles.get(this.$fileName);
DEBUG_FS && console.log("DefaultFileHandler.write: " + pathname);
var fd = MIDP.openFileHandlers.get(this);
fs.write(fd, b.subarray(off, off + len));
// The "length of data really written," which is always the length requested
// in our implementation.
@ -604,15 +622,15 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.write.([BII)I"] = function(b
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.positionForWrite.(J)V"] = function(offset) {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.positionForWrite: " + pathname);
var fd = MIDP.openFiles.get(this.$fileName);
DEBUG_FS && console.log("DefaultFileHandler.positionForWrite: " + pathname);
var fd = MIDP.openFileHandlers.get(this);
fs.setpos(fd, offset.toNumber());
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.flush.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.flush: " + pathname);
var fd = MIDP.openFiles.get(this.$fileName);
DEBUG_FS && console.log("DefaultFileHandler.flush: " + pathname);
var fd = MIDP.openFileHandlers.get(this);
if (fd) {
fs.flush(fd);
}
@ -620,10 +638,10 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.flush.()V"] = function() {
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.close.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.close: " + pathname);
var fd = MIDP.openFiles.get(this.$fileName);
DEBUG_FS && console.log("DefaultFileHandler.close: " + pathname);
var fd = MIDP.openFileHandlers.get(this);
fs.close(fd);
MIDP.openFiles.delete(this.$fileName);
MIDP.openFileHandlers.delete(this);
};
// Not implemented because we don't use native pointers, so we've commented out
@ -640,7 +658,7 @@ MIDP.openDirHandle = 0;
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openDir.()J"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
console.log("DefaultFileHandler.openDir: " + pathname);
DEBUG_FS && console.log("DefaultFileHandler.openDir: " + pathname);
try {
var files = fs.list(pathname);
@ -671,7 +689,7 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.dirGetNextFile.(JZ)Ljava/lan
function(dirHandle, includeHidden) {
var iterator = MIDP.openDirs.get(dirHandle.toNumber());
var nextFile = iterator.files[++iterator.index];
console.log(iterator.index + " " + nextFile);
DEBUG_FS && console.log(iterator.index + " " + nextFile);
return nextFile ? J2ME.newString(nextFile) : null;
};
@ -682,7 +700,7 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.getMountedRoots.()Ljava/lang
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.getNativePathForRoot.(Ljava/lang/String;)Ljava/lang/String;"] =
function(root) {
// XXX Ensure root is in MIDP.fsRoots?
console.log("getNativePathForRoot: " + util.fromJavaString(root));
DEBUG_FS && console.log("getNativePathForRoot: " + util.fromJavaString(root));
var nativePath = J2ME.newString("/" + util.fromJavaString(root));
return nativePath;
};
@ -694,7 +712,7 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.illegalFileNameChars0.()Ljav
addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.initialize.()V");
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.getSuiteIdString.(I)Ljava/lang/String;"] = function(id) {
console.log("getSuiteIdString: " + id);
DEBUG_FS && console.log("getSuiteIdString: " + id);
// return J2ME.newString(id.toString());
// The implementation adds this to the path of the file, presumably
// to segregate files by midlet, but we only run a single midlet
@ -704,8 +722,8 @@ console.log("getSuiteIdString: " + id);
Native["com/sun/cdc/io/j2me/file/Protocol.available.()I"] = function() {
var pathname = util.fromJavaString(this.$fileHandler.$nativePath);
var fd = MIDP.openFiles.get(this.$fileHandler.$fileName);
var fd = MIDP.openFileHandlers.get(this.$fileHandler);
var available = fs.getsize(fd) - fs.getpos(fd);
console.log("Protocol.available: " + pathname + ": " + available);
DEBUG_FS && console.log("Protocol.available: " + pathname + ": " + available);
return available;
};