             (cb) =>
                 if opts.filename?
                     dbg("open output file #{opts.filename}")
-                    fs.open opts.filename, 'w', (err, _fd) =>
-                        fd = _fd
-                        cb(err)
+                    misc_node.ensure_containing_directory_exists process.cwd()+'/'+opts.filename, (err) =>
+                        if err
+                            cb(err)
+                        else
+                            fs.open opts.filename, 'w', (err, _fd) =>
+                                fd = _fd
+                                cb(err)
                 else
                     cb()
             (cb) =>

    sync_get: (opts) =>
        opts = defaults opts,
            path   : required
            delete : false
            cb     : undefined
        dbg = (m) => @dbg('sync_get', opts.path, m)

    # First copy any files from the database to path, and any from path (not in db) back to the database,
    # so that the same files (the union) are in both.

    # Files with the same name and size are considered equal (for our application
    # this is fine).

    sync_diff: (opts) =>
        opts = defaults opts,
            path : required,
            cb   : required     # cb(err, {local_only:local_only_files, db_only:db_only_files})

        dbg = (m) => @dbg('sync_diff', opts.path, m)
        db_files    = {}  # map filename:size with keys the db files
        local_files = {}  # map filename:size with keys the local files
        async.series([
            (cb) =>
                dbg("files in database")
                @ls
                    cb : (err, x) =>
                        if err
