GLib = imports.gi.GLib;
Gio = imports.gi.Gio;
-imports['String.js'].load(String);
+
/**
* @namespace File
SEPARATOR : '/',
+ // fixme - this needs a bitter location..
+ // they where in a string class before, but overriding String methods is not a good normally a good idea..
+
+ rtrim : function (s,toTrim) {
+ if (s.substr(s.length - toTrim.length) == toTrim) {
+ return s.slice(0, s.length - toTrim.length);
+ }
+
+ return s;
+ },
+ trim : function (s,toTrim) {
+ var out = s.ltrim(toTrim);
+ out = out.rtrim(toTrim);
+ return out;
+ },
+
+ ltrim : function (s, toTrim) {
+ if (s.substr(0, toTrim.length) == toTrim) {
+ return s.slice(toTrim.length);
+ }
+
+ return s;
+ },
+
join : function () {
var out = "";
for (var i = 0; i < arguments.length; i++) {
if (i == 0) {
- out += arguments[i].rtrim(File.SEPARATOR);
+ out += this.rtrim(arguments[i], File.SEPARATOR);
}
else if (i == arguments.length - 1) {
- out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
+ out += File.SEPARATOR + this.ltrim(arguments[i], File.SEPARATOR);
}
else {
- out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
+ out += File.SEPARATOR + this.trim(arguments[i], File.SEPARATOR);
}
}
return out;
return new Date(mtime.tv_sec * 1000);
},
- canonical : function (path) {
+ /**
+ * resolve the real path
+ * @arg path {String} Path to resolve
+ * @returns {String} the resolved path path.
+ *
+ */
+ realpath : function (path) {
+ return this.canonical(path);
+ },
+ canonical : function (path) {
var f = Gio.file_new_for_path(String(path));
var can = f.resolve_relative_path('');
return can.get_path();
},
/**
- * write
- * @arg path {String} File to write to
+ * write a string to a file
+ * @arg path {String} File to write to alwasy overwrites.
* @arg string {String} Contents of file.
*
*/
write : function (path, string) {
+ var d = new Date();
var f = Gio.file_new_for_path(String(path));
var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
data_out.put_string(string, null);
data_out.close(null);
+ print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
+
},
/**
* append
}
},
+ /**
+ * Make a symbolic link
+ * @arg new_link {String} The new link
+ * @arg target {String} Where it links to.
+ */
+ link : function (new_link, target) {
+ var dest = Gio.file_new_for_path(String(new_link));
+ return dest.make_symbolic_link(target, null);
+ },
+ /**
+ * Make a directory
+ * FIXME - needs perms setting..
+ *
+ * @arg directory {String} Directory to make
+ */
mkdir : function (destPath) {
var dest = Gio.file_new_for_path(String(destPath));
- return dest.make_directory(null, null);
+ return dest.make_directory(null);
},
- copyFile : function (srcPath, destPath) {
+ /**
+ * Copy a file or (directory maybe?)
+ * @arg srcPath {String} source file
+ * @arg destPath {String} destination file
+ * @arg flags {Gio.FileCopyFlags} to overwrite etc... Gio.FileCopyFlags.OVERWRITE
+ */
+ copy : function (srcPath, destPath, flags) {
+ return this.copyFile(srcPath, destPath, flags);
+ },
+ copyFile : function (srcPath, destPath, flags) {
+
+ flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
var dest = Gio.file_new_for_path(String(destPath));
var src = Gio.file_new_for_path(String(srcPath));
// a bit of a hack for the fact that Gio.File.copy arguments
// can be nulled, but not according to the GIR file
- return src.copy(dest, Gio.FileCopyFlags.NONE);
+ return src.copy(dest, flags);
},
+
+
+
recursiveListing : function (dir) {