# Code coverage report for lib/utils.js

## Statements: 100% (10 / 10) Branches: 100% (9 / 9) Functions: 100% (1 / 1) Lines: 100% (10 / 10) Ignored: none

All files » lib/ » utils.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    251

    1
    50
    50

    1

    49

    49
    1

    48
    1

     var fs = require('fs');

    /**
     * Check to make sure the provided path is a writeable directory.  Throw an exception if not.
     */
    module.exports.validate_writeable_directory = function(dir) {
      try {
        var stat = fs.lstatSync(dir);
      } catch(e) {
        throw new Error('directory ' + dir + ' does not exist');
      }

      // Check whether this owner or group or anyone is allowed to write the directory.
      var can_write = ((process.getuid() === stat.uid) && (stat.mode & 00200)) || // User is owner and owner can write.
                      ((process.getgid() === stat.gid) && (stat.mode & 00020)) || // User is in group and group can write.
                      ((stat.mode & 00002)); // Anyone can write.
      if (!can_write) {
        throw new Error(dir + ' is not writeable');
      }

      //console.error(require('util').inspect(stat));
      if (!stat.isDirectory()) {
        throw new Error(dir + ' is not a directory');
      }
    };