2012-03-24 03:39:15 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2012-10-01 06:55:51 +04:00
|
|
|
const XRE_OS_UPDATE_APPLY_TO_DIR = "OSUpdApplyToD"
|
2012-12-15 04:05:39 +04:00
|
|
|
const UPDATE_ARCHIVE_DIR = "UpdArchD"
|
2012-04-18 14:47:05 +04:00
|
|
|
const LOCAL_DIR = "/data/local";
|
2013-01-31 05:23:51 +04:00
|
|
|
const UPDATES_DIR = "updates/0";
|
|
|
|
const FOTA_DIR = "updates/fota";
|
2012-03-24 03:39:15 +04:00
|
|
|
|
2012-10-01 06:55:51 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "env",
|
|
|
|
"@mozilla.org/process/environment;1",
|
|
|
|
"nsIEnvironment");
|
|
|
|
|
2013-01-10 17:46:39 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "um",
|
|
|
|
"@mozilla.org/updates/update-manager;1",
|
|
|
|
"nsIUpdateManager");
|
|
|
|
|
2012-12-15 04:05:39 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "volumeService",
|
|
|
|
"@mozilla.org/telephony/volume-service;1",
|
|
|
|
"nsIVolumeService");
|
|
|
|
|
2013-11-01 10:35:58 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsISyncMessageSender");
|
|
|
|
|
2012-12-15 04:05:39 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "gExtStorage", function dp_gExtStorage() {
|
|
|
|
return Services.env.get("EXTERNAL_STORAGE");
|
|
|
|
});
|
|
|
|
|
2013-01-10 17:46:39 +04:00
|
|
|
// This exists to mark the affected code for bug 828858.
|
|
|
|
const gUseSDCard = true;
|
|
|
|
|
2012-12-15 04:05:39 +04:00
|
|
|
const VERBOSE = 1;
|
|
|
|
let log =
|
|
|
|
VERBOSE ?
|
|
|
|
function log_dump(msg) { dump("DirectoryProvider: " + msg + "\n"); } :
|
|
|
|
function log_noop(msg) { };
|
|
|
|
|
2012-03-24 03:39:15 +04:00
|
|
|
function DirectoryProvider() {
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectoryProvider.prototype = {
|
|
|
|
classID: Components.ID("{9181eb7c-6f87-11e1-90b1-4f59d80dd2e5}"),
|
2012-04-18 14:47:05 +04:00
|
|
|
|
2012-03-24 03:39:15 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider]),
|
2013-02-26 07:11:14 +04:00
|
|
|
_xpcom_factory: XPCOMUtils.generateSingletonFactory(DirectoryProvider),
|
2012-03-24 03:39:15 +04:00
|
|
|
|
2013-11-01 10:35:58 +04:00
|
|
|
_profD: null,
|
|
|
|
|
2012-03-24 03:39:15 +04:00
|
|
|
getFile: function dp_getFile(prop, persistent) {
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-08-27 19:27:14 +04:00
|
|
|
let localProps = ["cachePDir", "webappsDir", "PrefD", "indexedDBPDir",
|
2012-08-30 01:20:03 +04:00
|
|
|
"permissionDBPDir", "UpdRootD"];
|
2012-04-18 14:47:05 +04:00
|
|
|
if (localProps.indexOf(prop) != -1) {
|
2012-03-24 03:39:15 +04:00
|
|
|
let file = Cc["@mozilla.org/file/local;1"]
|
|
|
|
.createInstance(Ci.nsILocalFile)
|
2012-04-18 14:47:05 +04:00
|
|
|
file.initWithPath(LOCAL_DIR);
|
2012-08-30 01:20:03 +04:00
|
|
|
persistent.value = true;
|
|
|
|
return file;
|
2012-12-15 04:05:39 +04:00
|
|
|
}
|
2013-07-16 17:02:55 +04:00
|
|
|
if (prop == "ProfD") {
|
|
|
|
let dir = Cc["@mozilla.org/file/local;1"]
|
|
|
|
.createInstance(Ci.nsILocalFile);
|
|
|
|
dir.initWithPath(LOCAL_DIR+"/tests/profile");
|
|
|
|
if (dir.exists()) {
|
|
|
|
persistent.value = true;
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
}
|
2012-12-15 04:05:39 +04:00
|
|
|
if (prop == "coreAppsDir") {
|
2012-08-30 01:20:03 +04:00
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile)
|
|
|
|
file.initWithPath("/system/b2g");
|
|
|
|
persistent.value = true;
|
2012-03-24 03:39:15 +04:00
|
|
|
return file;
|
2012-12-15 04:05:39 +04:00
|
|
|
}
|
2013-01-31 05:23:51 +04:00
|
|
|
if (prop == UPDATE_ARCHIVE_DIR) {
|
|
|
|
// getUpdateDir will set persistent to false since it may toggle between
|
|
|
|
// /data/local/ and /mnt/sdcard based on free space and/or availability
|
|
|
|
// of the sdcard.
|
2013-12-17 07:24:14 +04:00
|
|
|
// before download, check if free space is 2.1 times of update.mar
|
|
|
|
return this.getUpdateDir(persistent, UPDATES_DIR, 2.1);
|
2013-01-31 05:23:51 +04:00
|
|
|
}
|
|
|
|
if (prop == XRE_OS_UPDATE_APPLY_TO_DIR) {
|
|
|
|
// getUpdateDir will set persistent to false since it may toggle between
|
|
|
|
// /data/local/ and /mnt/sdcard based on free space and/or availability
|
|
|
|
// of the sdcard.
|
2013-12-17 07:24:14 +04:00
|
|
|
// before apply, check if free space is 1.1 times of update.mar
|
|
|
|
return this.getUpdateDir(persistent, FOTA_DIR, 1.1);
|
2012-03-24 03:39:15 +04:00
|
|
|
}
|
2013-11-01 10:35:58 +04:00
|
|
|
#else
|
|
|
|
// In desktop builds, coreAppsDir is the same as the profile directory.
|
|
|
|
// We just need to get the path from the parent, and it is then used to
|
|
|
|
// build jar:remoteopenfile:// uris.
|
|
|
|
if (prop == "coreAppsDir") {
|
|
|
|
let appsDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
|
|
|
|
appsDir.append("webapps");
|
|
|
|
persistent.value = true;
|
|
|
|
return appsDir;
|
|
|
|
} else if (prop == "ProfD") {
|
|
|
|
let inParent = Cc["@mozilla.org/xre/app-info;1"]
|
|
|
|
.getService(Ci.nsIXULRuntime)
|
|
|
|
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
|
|
|
|
if (inParent) {
|
|
|
|
// Just bail out to use the default from toolkit.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!this._profD) {
|
|
|
|
this._profD = cpmm.sendSyncMessage("getProfD", {})[0];
|
|
|
|
}
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
|
|
|
|
file.initWithPath(this._profD);
|
|
|
|
persistent.value = true;
|
|
|
|
return file;
|
|
|
|
}
|
2012-03-24 03:39:15 +04:00
|
|
|
#endif
|
2012-12-15 04:05:39 +04:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// The VolumeService only exists on the device, and not on desktop
|
|
|
|
volumeHasFreeSpace: function dp_volumeHasFreeSpace(volumePath, requiredSpace) {
|
|
|
|
if (!volumePath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Services.volumeService) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-18 18:13:23 +04:00
|
|
|
let volume = Services.volumeService.createOrGetVolumeByPath(volumePath);
|
2012-12-15 04:05:39 +04:00
|
|
|
if (!volume || volume.state !== Ci.nsIVolume.STATE_MOUNTED) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let stat = volume.getStats();
|
|
|
|
if (!stat) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return requiredSpace <= stat.freeBytes;
|
|
|
|
},
|
|
|
|
|
2013-01-31 05:23:51 +04:00
|
|
|
findUpdateDirWithFreeSpace: function dp_findUpdateDirWithFreeSpace(requiredSpace, subdir) {
|
2012-12-15 04:05:39 +04:00
|
|
|
if (!Services.volumeService) {
|
2013-01-31 05:23:51 +04:00
|
|
|
return this.createUpdatesDir(LOCAL_DIR, subdir);
|
2012-12-15 04:05:39 +04:00
|
|
|
}
|
2013-02-26 07:11:14 +04:00
|
|
|
|
2012-12-15 04:05:39 +04:00
|
|
|
let activeUpdate = Services.um.activeUpdate;
|
2013-01-10 17:46:39 +04:00
|
|
|
if (gUseSDCard) {
|
|
|
|
if (this.volumeHasFreeSpace(gExtStorage, requiredSpace)) {
|
2013-01-31 05:23:51 +04:00
|
|
|
let extUpdateDir = this.createUpdatesDir(gExtStorage, subdir);
|
2013-01-10 17:46:39 +04:00
|
|
|
if (extUpdateDir !== null) {
|
|
|
|
return extUpdateDir;
|
|
|
|
}
|
|
|
|
log("Warning: " + gExtStorage + " has enough free space for update " +
|
|
|
|
activeUpdate.name + ", but is not writable");
|
2012-12-15 04:05:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.volumeHasFreeSpace(LOCAL_DIR, requiredSpace)) {
|
2013-01-31 05:23:51 +04:00
|
|
|
let localUpdateDir = this.createUpdatesDir(LOCAL_DIR, subdir);
|
2012-12-15 04:05:39 +04:00
|
|
|
if (localUpdateDir !== null) {
|
|
|
|
return localUpdateDir;
|
|
|
|
}
|
|
|
|
log("Warning: " + LOCAL_DIR + " has enough free space for update " +
|
|
|
|
activeUpdate.name + ", but is not writable");
|
|
|
|
}
|
2012-03-24 03:39:15 +04:00
|
|
|
|
|
|
|
return null;
|
2012-10-01 06:55:51 +04:00
|
|
|
},
|
|
|
|
|
2013-12-17 07:24:14 +04:00
|
|
|
getUpdateDir: function dp_getUpdateDir(persistent, subdir, multiple) {
|
2012-12-15 04:05:39 +04:00
|
|
|
let defaultUpdateDir = this.getDefaultUpdateDir();
|
|
|
|
persistent.value = false;
|
|
|
|
|
|
|
|
let activeUpdate = Services.um.activeUpdate;
|
|
|
|
if (!activeUpdate) {
|
|
|
|
log("Warning: No active update found, using default update dir: " +
|
|
|
|
defaultUpdateDir);
|
|
|
|
return defaultUpdateDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
let selectedPatch = activeUpdate.selectedPatch;
|
|
|
|
if (!selectedPatch) {
|
|
|
|
log("Warning: No selected patch, using default update dir: " +
|
|
|
|
defaultUpdateDir);
|
|
|
|
return defaultUpdateDir;
|
|
|
|
}
|
|
|
|
|
2013-12-17 07:24:14 +04:00
|
|
|
let requiredSpace = selectedPatch.size * multiple;
|
2013-01-31 05:23:51 +04:00
|
|
|
let updateDir = this.findUpdateDirWithFreeSpace(requiredSpace, subdir);
|
2012-12-15 04:05:39 +04:00
|
|
|
if (updateDir) {
|
|
|
|
return updateDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've gotten this far, there isn't enough free space to download the patch
|
|
|
|
// on either external storage or /data/local. All we can do is report the
|
|
|
|
// error and let upstream code handle it more gracefully.
|
|
|
|
log("Error: No volume found with " + requiredSpace + " bytes for downloading"+
|
|
|
|
" update " + activeUpdate.name);
|
2013-02-26 07:11:14 +04:00
|
|
|
activeUpdate.errorCode = Cr.NS_ERROR_FILE_TOO_BIG;
|
|
|
|
return null;
|
2012-12-15 04:05:39 +04:00
|
|
|
},
|
2012-10-01 06:55:51 +04:00
|
|
|
|
2013-01-31 05:23:51 +04:00
|
|
|
createUpdatesDir: function dp_createUpdatesDir(root, subdir) {
|
2012-12-15 04:05:39 +04:00
|
|
|
let dir = Cc["@mozilla.org/file/local;1"]
|
|
|
|
.createInstance(Ci.nsILocalFile);
|
|
|
|
dir.initWithPath(root);
|
|
|
|
if (!dir.isWritable()) {
|
2013-01-31 05:23:51 +04:00
|
|
|
log("Error: " + dir.path + " isn't writable");
|
2012-12-15 04:05:39 +04:00
|
|
|
return null;
|
|
|
|
}
|
2013-01-31 05:23:51 +04:00
|
|
|
dir.appendRelativePath(subdir);
|
2012-12-15 04:05:39 +04:00
|
|
|
if (dir.exists()) {
|
|
|
|
if (dir.isDirectory() && dir.isWritable()) {
|
|
|
|
return dir;
|
|
|
|
}
|
2013-01-31 05:23:51 +04:00
|
|
|
// subdir is either a file or isn't writable. In either case we
|
2012-12-15 04:05:39 +04:00
|
|
|
// can't use it.
|
|
|
|
log("Error: " + dir.path + " is a file or isn't writable");
|
|
|
|
return null;
|
|
|
|
}
|
2013-01-31 05:23:51 +04:00
|
|
|
// subdir doesn't exist, and the parent is writable, so try to
|
2012-12-15 04:05:39 +04:00
|
|
|
// create it. This can fail if a file named updates exists.
|
|
|
|
try {
|
2015-02-05 01:51:39 +03:00
|
|
|
dir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0770', 8));
|
2012-12-15 04:05:39 +04:00
|
|
|
} catch (e) {
|
|
|
|
// The create failed for some reason. We can't use it.
|
|
|
|
log("Error: " + dir.path + " unable to create directory");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultUpdateDir: function dp_getDefaultUpdateDir() {
|
|
|
|
let path = gExtStorage;
|
2012-10-01 06:55:51 +04:00
|
|
|
if (!path) {
|
2012-12-15 04:05:39 +04:00
|
|
|
path = LOCAL_DIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Services.volumeService) {
|
2013-04-18 18:13:23 +04:00
|
|
|
let extVolume = Services.volumeService.createOrGetVolumeByPath(path);
|
2012-12-15 04:05:39 +04:00
|
|
|
if (!extVolume) {
|
|
|
|
path = LOCAL_DIR;
|
|
|
|
}
|
2012-10-01 06:55:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let dir = Cc["@mozilla.org/file/local;1"]
|
|
|
|
.createInstance(Ci.nsILocalFile)
|
|
|
|
dir.initWithPath(path);
|
|
|
|
|
2012-12-15 04:05:39 +04:00
|
|
|
if (!dir.exists() && path != LOCAL_DIR) {
|
|
|
|
// Fallback to LOCAL_DIR if we didn't fallback earlier
|
|
|
|
dir.initWithPath(LOCAL_DIR);
|
2012-10-01 06:55:51 +04:00
|
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
throw Cr.NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.appendRelativePath("updates");
|
|
|
|
return dir;
|
2012-03-24 03:39:15 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DirectoryProvider]);
|