Miryafa
Miryafa

Reputation: 192

Firefox add-on - OS.File.open "access denied" after install

Date: Mon Oct 13 2014 14:22:35 GMT-0500 (Central Standard Time) Full Message: Win error 5 during operation open on file log.txt (Access is denied.)

I created an add-on following Mozilla's Add-on SDK tutorials, and cfx test and cfx run both work perfectly. However, after installing the add-on, OS.File.open doesn't work. Here is the relevant code:

var testLogArray = "", testLogFile = "qpasslog.txt";
const {TextEncoder, TextDecoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
Cu.import("resource://gre/modules/Task.jsm");
var testLogEncoder = new TextEncoder();
function addToLogfile(logData) {
    console.log("main-js: Logging "+logData);
    var dataToWrite = testLogEncoder.encode(logData+"\r\n");
    Task.spawn(function() {
        let exists = yield OS.File.exists(testLogFile);
        let pfh = yield OS.File.open(testLogFile, exists ? {read:true, write: true, existing: true, append: true} : {read: true, write: true, create: true, append: false });
        let text = yield pfh.read();
        yield pfh.write(dataToWrite);
        yield pfh.close(); 
    });
}
let currentdate = new Date();
let dateTime = currentdate.getDate() + "/"
            + (currentdate.getMonth()+1)  + "/" 
            + currentdate.getFullYear() + " @ "  
            + currentdate.getHours() + ":"  
            + currentdate.getMinutes() + ":" 
            + currentdate.getSeconds();
addToLogfile("\r\nLog for "+dateTime);

Then I call addToLogfile several times in my program, eg.

addToLogfile("main-js: Simple storage is over quota. Removing "+ss.storage.emails.pop());

And this all worked perfectly during cfx run. It would log the date/time and then the other messages. But now I've installed the program, with cfx xpi and ctrl+o (the xpi file), and:

  1. I don't get any logged messages until I close Firefox
  2. When I close Firefox I get the date/time message, eg.

    Log for 13/10/2014 @ 13:3:28
    
  3. I don't get any other messages, even though the code should always execute.

  4. The browser console shows this message:

    Date: Mon Oct 13 2014 14:25:46 GMT-0500 (Central Standard Time)
    Full Message: Win error 5 during operation open on file qpasslog.txt (Access is denied.)
    

So why does an access denied error only show up sometimes? And how do I write to a file without getting an error?

Upvotes: 1

Views: 457

Answers (1)

Miryafa
Miryafa

Reputation: 192

It's due to not having the correct path to the log file. The answer in the following link fixed it. Mozilla Add-on SDK - OS.File.read (The system cannot find the file specified.)

Upvotes: 1

Related Questions