src/filesystem/implementation.js
function ensure_root_directory(context, callback) {
  var superNode;
  var directoryNode;
  var directoryData;

  function ensure_super_node(error, existingNode) {
    if(!error && existingNode) {
      // Another instance has beat us and already created the super node.
      callback();
    } else if(error && !(error instanceof Errors.ENOENT)) {
      callback(error);
    } else {
      SuperNode.create({guid: context.guid}, function(error, result) {
        if(error) {
          callback(error);
          return;
        }
        superNode = result;
        context.putObject(superNode.id, superNode, write_directory_node);
      });
    }
  }

  function write_directory_node(error) {
    if(error) {
      callback(error);
    } else {
      Node.create({
        guid: context.guid,
        id: superNode.rnode,
        type: NODE_TYPE_DIRECTORY
      }, function(error, result) {
        if(error) {
          callback(error);
          return;
        }
        directoryNode = result;
        directoryNode.nlinks += 1;
        context.putObject(directoryNode.id, directoryNode, write_directory_data);
      });
    }
  }

  function write_directory_data(error) {
    if(error) {
      callback(error);
    } else {
      directoryData = {};
      context.putObject(directoryNode.data, directoryData, callback);
    }
  }

  context.getObject(SUPER_NODE_ID, ensure_super_node);
}
function ensure_root_directory(context, callback) {
  var superNode;
  var directoryNode;
  var directoryData;

  function ensure_super_node(error, existingNode) {
    if(!error && existingNode) {
      // Another instance has beat us and already created the super node.
      callback();
    } else if(error && !(error instanceof Errors.ENOENT)) {
      callback(error);
    } else {
      SuperNode.create({guid: context.guid}, function(error, result) {
        if(error) {
          callback(error);
          return;
        }
        superNode = result;
        context.putObject(superNode.id, superNode, write_directory_node);
      });
    }
  }

  function write_directory_node(error) {
    if(error) {
      callback(error);
    } else {
      Node.create({
        guid: context.guid,
        id: superNode.rnode,
        type: NODE_TYPE_DIRECTORY
      }, function(error, result) {
        if(error) {
          callback(error);
          return;
        }
        directoryNode = result;
        directoryNode.nlinks += 1;
        context.putObject(directoryNode.id, directoryNode, write_directory_data);
      });
    }
  }

  function write_directory_data(error) {
    if(error) {
      callback(error);
    } else {
      directoryData = {};
      context.putObject(directoryNode.data, directoryData, callback);
    }
  }

  context.getObject(SUPER_NODE_ID, ensure_super_node);
}
function ensure_root_directory ( context , callback ) { var superNode ; var directoryNode ; var directoryData ; function ensure_super_node ( error , existingNode ) { if ( ! error && existingNode ) { // Another instance has beat us and already created the super node. callback ( ) ; } else if ( error && ! ( error instanceof Errors . ENOENT ) ) { callback ( error ) ; } else { SuperNode . create ( { guid : context . guid } , function ( error , result ) { if ( error ) { callback ( error ) ; return ; } superNode = result ; context . putObject ( superNode . id , superNode , write_directory_node ) ; } ) ; } } function write_directory_node ( error ) { if ( error ) { callback ( error ) ; } else { Node . create ( { guid : context . guid , id : superNode . rnode , type : NODE_TYPE_DIRECTORY } , function ( error , result ) { if ( error ) { callback ( error ) ; return ; } directoryNode = result ; directoryNode . nlinks += 1 ; context . putObject ( directoryNode . id , directoryNode , write_directory_data ) ; } ) ; } } function write_directory_data ( error ) { if ( error ) { callback ( error ) ; } else { directoryData = { } ; context . putObject ( directoryNode . data , directoryData , callback ) ; } } context . getObject ( SUPER_NODE_ID , ensure_super_node ) ; }
ensure_root_directory. Creates a root node if necessary.

Note: this should only be invoked when formatting a new file system.
Multiple invocations of this by separate instances will still result
in only a single super node.
ensure_root_directory . Creates a root node if necessary .