michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * A worker dedicated for the I/O component of PageThumbs storage. michael@0: * michael@0: * Do not rely on the API of this worker. In a future version, it might be michael@0: * fully replaced by a OS.File global I/O worker. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: importScripts("resource://gre/modules/osfile.jsm"); michael@0: michael@0: let File = OS.File; michael@0: let Type = OS.Shared.Type; michael@0: michael@0: /** michael@0: * Communications with the controller. michael@0: * michael@0: * Accepts messages: michael@0: * {fun:function_name, args:array_of_arguments_or_null} michael@0: * michael@0: * Sends messages: michael@0: * {ok: result} / {fail: serialized_form_of_OS.File.Error} michael@0: */ michael@0: self.onmessage = function onmessage(msg) { michael@0: let data = msg.data; michael@0: let id = data.id; michael@0: let result; michael@0: if (!(data.fun in Agent)) { michael@0: throw new Error("Cannot find method " + data.fun); michael@0: } michael@0: try { michael@0: result = Agent[data.fun].apply(Agent, data.args); michael@0: } catch (ex if ex instanceof StopIteration) { michael@0: // StopIteration cannot be serialized automatically michael@0: self.postMessage({StopIteration: true, id: id}); michael@0: return; michael@0: } catch (ex if ex instanceof OS.File.Error) { michael@0: // Instances of OS.File.Error know how to serialize themselves michael@0: // (deserialization ensures that we end up with OS-specific michael@0: // instances of |OS.File.Error|) michael@0: self.postMessage({fail: OS.File.Error.toMsg(ex), id:id}); michael@0: return; michael@0: } michael@0: // Other exceptions do not, and should be propagated through DOM's michael@0: // built-in mechanism for uncaught errors, although this mechanism michael@0: // may lose interesting information. michael@0: self.postMessage({ok: result, id:id}); michael@0: }; michael@0: michael@0: michael@0: let Agent = { michael@0: // Checks if the specified file exists and has an age less than as michael@0: // specifed (in seconds). michael@0: isFileRecent: function Agent_isFileRecent(path, maxAge) { michael@0: try { michael@0: let stat = OS.File.stat(path); michael@0: let maxDate = new Date(); michael@0: maxDate.setSeconds(maxDate.getSeconds() - maxAge); michael@0: return stat.lastModificationDate > maxDate; michael@0: } catch (ex if ex instanceof OS.File.Error) { michael@0: // file doesn't exist (or can't be stat'd) - must be stale. michael@0: return false; michael@0: } michael@0: }, michael@0: michael@0: remove: function Agent_removeFile(path) { michael@0: try { michael@0: OS.File.remove(path); michael@0: return true; michael@0: } catch (e) { michael@0: return false; michael@0: } michael@0: }, michael@0: michael@0: expireFilesInDirectory: michael@0: function Agent_expireFilesInDirectory(path, filesToKeep, minChunkSize) { michael@0: let entries = this.getFileEntriesInDirectory(path, filesToKeep); michael@0: let limit = Math.max(minChunkSize, Math.round(entries.length / 2)); michael@0: michael@0: for (let entry of entries) { michael@0: this.remove(entry.path); michael@0: michael@0: // Check if we reached the limit of files to remove. michael@0: if (--limit <= 0) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: }, michael@0: michael@0: getFileEntriesInDirectory: michael@0: function Agent_getFileEntriesInDirectory(path, skipFiles) { michael@0: let iter = new OS.File.DirectoryIterator(path); michael@0: if (!iter.exists()) { michael@0: return []; michael@0: } michael@0: michael@0: let skip = new Set(skipFiles); michael@0: michael@0: return [entry michael@0: for (entry in iter) michael@0: if (!entry.isDir && !entry.isSymLink && !skip.has(entry.name))]; michael@0: }, michael@0: michael@0: moveOrDeleteAllThumbnails: michael@0: function Agent_moveOrDeleteAllThumbnails(pathFrom, pathTo) { michael@0: OS.File.makeDir(pathTo, {ignoreExisting: true}); michael@0: if (pathFrom == pathTo) { michael@0: return true; michael@0: } michael@0: let iter = new OS.File.DirectoryIterator(pathFrom); michael@0: if (iter.exists()) { michael@0: for (let entry in iter) { michael@0: if (entry.isDir || entry.isSymLink) { michael@0: continue; michael@0: } michael@0: michael@0: michael@0: let from = OS.Path.join(pathFrom, entry.name); michael@0: let to = OS.Path.join(pathTo, entry.name); michael@0: michael@0: try { michael@0: OS.File.move(from, to, {noOverwrite: true, noCopy: true}); michael@0: } catch (e) { michael@0: OS.File.remove(from); michael@0: } michael@0: } michael@0: } michael@0: iter.close(); michael@0: michael@0: try { michael@0: OS.File.removeEmptyDir(pathFrom); michael@0: } catch (e) { michael@0: // This could fail if there's something in michael@0: // the folder we're not permitted to remove. michael@0: } michael@0: michael@0: return true; michael@0: }, michael@0: michael@0: writeAtomic: function Agent_writeAtomic(path, buffer, options) { michael@0: return File.writeAtomic(path, michael@0: buffer, michael@0: options); michael@0: }, michael@0: michael@0: makeDir: function Agent_makeDir(path, options) { michael@0: return File.makeDir(path, options); michael@0: }, michael@0: michael@0: copy: function Agent_copy(source, dest, options) { michael@0: return File.copy(source, dest, options); michael@0: }, michael@0: michael@0: wipe: function Agent_wipe(path) { michael@0: let iterator = new File.DirectoryIterator(path); michael@0: try { michael@0: for (let entry in iterator) { michael@0: try { michael@0: File.remove(entry.path); michael@0: } catch (ex) { michael@0: // If a file cannot be removed, we should still continue. michael@0: // This can happen at least for any of the following reasons: michael@0: // - access denied; michael@0: // - file has been removed recently during a previous wipe michael@0: // and the file system has not flushed that yet (yes, this michael@0: // can happen under Windows); michael@0: // - file has been removed by the user or another process. michael@0: } michael@0: } michael@0: } finally { michael@0: iterator.close(); michael@0: } michael@0: }, michael@0: michael@0: exists: function Agent_exists(path) { michael@0: return File.exists(path); michael@0: }, michael@0: }; michael@0: