summaryrefslogtreecommitdiffstats
path: root/b2g/components/DirectoryProvider.js
blob: a7dccd0c9f8083f3ea8ee2ed500b31f23bffb5a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* 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");
Cu.import("resource://gre/modules/AppConstants.jsm");

const XRE_OS_UPDATE_APPLY_TO_DIR = "OSUpdApplyToD"
const UPDATE_ARCHIVE_DIR = "UpdArchD"
const LOCAL_DIR = "/data/local";
const UPDATES_DIR = "updates/0";
const FOTA_DIR = "updates/fota";
const COREAPPSDIR_PREF = "b2g.coreappsdir"

XPCOMUtils.defineLazyServiceGetter(Services, "env",
                                   "@mozilla.org/process/environment;1",
                                   "nsIEnvironment");

XPCOMUtils.defineLazyServiceGetter(Services, "um",
                                   "@mozilla.org/updates/update-manager;1",
                                   "nsIUpdateManager");

XPCOMUtils.defineLazyServiceGetter(Services, "volumeService",
                                   "@mozilla.org/telephony/volume-service;1",
                                   "nsIVolumeService");

XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
                                   "@mozilla.org/childprocessmessagemanager;1",
                                   "nsISyncMessageSender");

XPCOMUtils.defineLazyGetter(this, "gExtStorage", function dp_gExtStorage() {
    return Services.env.get("EXTERNAL_STORAGE");
});

// This exists to mark the affected code for bug 828858.
const gUseSDCard = true;

const VERBOSE = 1;
var log =
  VERBOSE ?
  function log_dump(msg) { dump("DirectoryProvider: " + msg + "\n"); } :
  function log_noop(msg) { };

function DirectoryProvider() {
}

DirectoryProvider.prototype = {
  classID: Components.ID("{9181eb7c-6f87-11e1-90b1-4f59d80dd2e5}"),

  QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider]),
  _xpcom_factory: XPCOMUtils.generateSingletonFactory(DirectoryProvider),

  _profD: null,

  getFile: function(prop, persistent) {
    if (AppConstants.platform === "gonk") {
      return this.getFileOnGonk(prop, persistent);
    }
    return this.getFileNotGonk(prop, persistent);
  },

  getFileOnGonk: function(prop, persistent) {
    let localProps = ["cachePDir", "webappsDir", "PrefD", "indexedDBPDir",
                      "permissionDBPDir", "UpdRootD"];
    if (localProps.indexOf(prop) != -1) {
      let file = Cc["@mozilla.org/file/local;1"]
                   .createInstance(Ci.nsILocalFile)
      file.initWithPath(LOCAL_DIR);
      persistent.value = true;
      return file;
    }
    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;
      }
    }
    if (prop == "coreAppsDir") {
      let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile)
      file.initWithPath("/system/b2g");
      persistent.value = true;
      return file;
    }
    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.
      // before download, check if free space is 2.1 times of update.mar
      return this.getUpdateDir(persistent, UPDATES_DIR, 2.1);
    }
    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.
      // before apply, check if free space is 1.1 times of update.mar
      return this.getUpdateDir(persistent, FOTA_DIR, 1.1);
    }
    return null;
  },

  getFileNotGonk: function(prop, persistent) {
    // In desktop builds, coreAppsDir is the same as the profile
    // directory unless otherwise specified. We just need to get the
    // path from the parent, and it is then used to build
    // jar:remoteopenfile:// uris.
    if (prop == "coreAppsDir") {
      let coreAppsDirPref;
      try {
        coreAppsDirPref = Services.prefs.getCharPref(COREAPPSDIR_PREF);
      } catch (e) {
        // coreAppsDirPref may not exist if we're on an older version
        // of gaia, so just fail silently.
      }
      let appsDir;
      // If pref doesn't exist or isn't set, default to old value
      if (!coreAppsDirPref || coreAppsDirPref == "") {
        appsDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
        appsDir.append("webapps");
      } else {
        appsDir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile)
        appsDir.initWithPath(coreAppsDirPref);
      }
      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;
    }
    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;
    }
    let volume = Services.volumeService.createOrGetVolumeByPath(volumePath);
    if (!volume || volume.state !== Ci.nsIVolume.STATE_MOUNTED) {
      return false;
    }
    let stat = volume.getStats();
    if (!stat) {
      return false;
    }
    return requiredSpace <= stat.freeBytes;
  },

  findUpdateDirWithFreeSpace: function dp_findUpdateDirWithFreeSpace(requiredSpace, subdir) {
    if (!Services.volumeService) {
      return this.createUpdatesDir(LOCAL_DIR, subdir);
    }

    let activeUpdate = Services.um.activeUpdate;
    if (gUseSDCard) {
      if (this.volumeHasFreeSpace(gExtStorage, requiredSpace)) {
        let extUpdateDir = this.createUpdatesDir(gExtStorage, subdir);
        if (extUpdateDir !== null) {
          return extUpdateDir;
        }
        log("Warning: " + gExtStorage + " has enough free space for update " +
            activeUpdate.name + ", but is not writable");
      }
    }

    if (this.volumeHasFreeSpace(LOCAL_DIR, requiredSpace)) {
      let localUpdateDir = this.createUpdatesDir(LOCAL_DIR, subdir);
      if (localUpdateDir !== null) {
        return localUpdateDir;
      }
      log("Warning: " + LOCAL_DIR + " has enough free space for update " +
          activeUpdate.name + ", but is not writable");
    }

    return null;
  },

  getUpdateDir: function dp_getUpdateDir(persistent, subdir, multiple) {
    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;
    }

    let requiredSpace = selectedPatch.size * multiple;
    let updateDir = this.findUpdateDirWithFreeSpace(requiredSpace, subdir);
    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);
    activeUpdate.errorCode = Cr.NS_ERROR_FILE_TOO_BIG;
    return null;
  },

  createUpdatesDir: function dp_createUpdatesDir(root, subdir) {
      let dir = Cc["@mozilla.org/file/local;1"]
                   .createInstance(Ci.nsILocalFile);
      dir.initWithPath(root);
      if (!dir.isWritable()) {
        log("Error: " + dir.path + " isn't writable");
        return null;
      }
      dir.appendRelativePath(subdir);
      if (dir.exists()) {
        if (dir.isDirectory() && dir.isWritable()) {
          return dir;
        }
        // subdir is either a file or isn't writable. In either case we
        // can't use it.
        log("Error: " + dir.path + " is a file or isn't writable");
        return null;
      }
      // subdir doesn't exist, and the parent is writable, so try to
      // create it. This can fail if a file named updates exists.
      try {
        dir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0770', 8));
      } 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;
    if (!path) {
      path = LOCAL_DIR;
    }

    if (Services.volumeService) {
      let extVolume = Services.volumeService.createOrGetVolumeByPath(path);
      if (!extVolume) {
        path = LOCAL_DIR;
      }
    }

    let dir = Cc["@mozilla.org/file/local;1"]
                 .createInstance(Ci.nsILocalFile)
    dir.initWithPath(path);

    if (!dir.exists() && path != LOCAL_DIR) {
      // Fallback to LOCAL_DIR if we didn't fallback earlier
      dir.initWithPath(LOCAL_DIR);

      if (!dir.exists()) {
        throw Cr.NS_ERROR_FILE_NOT_FOUND;
      }
    }

    dir.appendRelativePath("updates");
    return dir;
  }
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DirectoryProvider]);