 
     write_to_disk: (cb) =>
         @_stop_watching_file()
-        fs.writeFile @path, @live, (err) =>
-            @_start_watching_file()
-            if not err
-                fs.exists @_backup_file, (exists) =>
-                    fs.unlink(@_backup_file)
-            cb?(err)
+        ensure_containing_directory_exists @path, (err) =>
+            if err
+                cb?(err); return
+            fs.writeFile @path, @live, (err) =>
+                @_start_watching_file()
+                if not err
+                    fs.exists @_backup_file, (exists) =>
+                        fs.unlink(@_backup_file)
+                cb?(err)
 
     write_backup: (cb) =>
         if @cm_session.content != @_last_backup

                    @init(doc:data.toString().replace(/\r/g,''), id:"file_server")
                    # winston.debug("got new file contents = '#{@live}'")
                    @_start_watching_file()
                    cb(err, @live)

    kill: () =>
        if @_autosave?

                    else
                        # backup is newer
                        file = @_backup_file
                    cb()
            (cb) =>
                fs.readFile file, (err, data) =>
                    if err
                        cb(err); return
