395
409
fs.lstatSync = function(path) {
396
return binding.lstat(path);
410
return binding.lstat(pathModule._makeLong(path));
399
413
fs.statSync = function(path) {
400
return binding.stat(path);
414
return binding.stat(pathModule._makeLong(path));
403
417
fs.readlink = function(path, callback) {
404
binding.readlink(path, callback || noop);
418
binding.readlink(pathModule._makeLong(path), callback || noop);
407
421
fs.readlinkSync = function(path) {
408
return binding.readlink(path);
411
fs.symlink = function(destination, path, callback) {
412
binding.symlink(destination, path, callback || noop);
415
fs.symlinkSync = function(destination, path) {
416
return binding.symlink(destination, path);
422
return binding.readlink(pathModule._makeLong(path));
425
fs.symlink = function(destination, path, type_, callback) {
426
var type = (typeof(type_) == 'string' ? type_ : null);
427
var callback_ = arguments[arguments.length - 1];
428
callback = (typeof(callback_) == 'function' ? callback_ : null);
429
binding.symlink(pathModule._makeLong(destination),
430
pathModule._makeLong(path), type, callback);
433
fs.symlinkSync = function(destination, path, type) {
434
return binding.symlink(pathModule._makeLong(destination),
435
pathModule._makeLong(path), type);
419
438
fs.link = function(srcpath, dstpath, callback) {
420
binding.link(srcpath, dstpath, callback || noop);
439
binding.link(pathModule._makeLong(srcpath), pathModule._makeLong(dstpath),
423
443
fs.linkSync = function(srcpath, dstpath) {
424
return binding.link(srcpath, dstpath);
444
return binding.link(pathModule._makeLong(srcpath),
445
pathModule._makeLong(dstpath));
427
448
fs.unlink = function(path, callback) {
428
binding.unlink(path, callback || noop);
449
binding.unlink(pathModule._makeLong(path), callback || noop);
431
452
fs.unlinkSync = function(path) {
432
return binding.unlink(path);
453
return binding.unlink(pathModule._makeLong(path));
456
fs.fchmod = function(fd, mode, callback) {
457
binding.fchmod(fd, modeNum(mode), callback || noop);
460
fs.fchmodSync = function(fd, mode) {
461
return binding.fchmod(fd, modeNum(mode));
464
if (constants.hasOwnProperty('O_SYMLINK')) {
465
fs.lchmod = function(path, mode, callback) {
466
callback = callback || noop;
467
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
472
// prefer to return the chmod error, if one occurs,
473
// but still try to close, and report closing errors if they occur.
474
fs.fchmod(fd, mode, function(err) {
475
fs.close(fd, function(err2) {
476
callback(err || err2);
482
fs.lchmodSync = function(path, mode) {
483
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
485
// prefer to return the chmod error, if one occurs,
486
// but still try to close, and report closing errors if they occur.
489
var ret = fs.fchmodSync(fd, mode);
498
if (err || err2) throw (err || err2);
435
504
fs.chmod = function(path, mode, callback) {
436
binding.chmod(path, modeNum(mode), callback || noop);
505
binding.chmod(pathModule._makeLong(path), modeNum(mode), callback || noop);
439
508
fs.chmodSync = function(path, mode) {
440
return binding.chmod(path, modeNum(mode));
509
return binding.chmod(pathModule._makeLong(path), modeNum(mode));
512
if (constants.hasOwnProperty('O_SYMLINK')) {
513
fs.lchown = function(path, uid, gid, callback) {
514
callback = callback || noop;
515
fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
520
fs.fchown(fd, uid, gid, callback);
524
fs.lchownSync = function(path, uid, gid) {
525
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
526
return fs.fchownSync(fd, uid, gid);
530
fs.fchown = function(fd, uid, gid, callback) {
531
binding.fchown(fd, uid, gid, callback || noop);
534
fs.fchownSync = function(fd, uid, gid) {
535
return binding.fchown(fd, uid, gid);
443
538
fs.chown = function(path, uid, gid, callback) {
444
binding.chown(path, uid, gid, callback || noop);
539
binding.chown(pathModule._makeLong(path), uid, gid, callback || noop);
447
542
fs.chownSync = function(path, uid, gid) {
448
return binding.chown(path, uid, gid);
543
return binding.chown(pathModule._makeLong(path), uid, gid);
546
// converts Date or number to a fractional UNIX timestamp
547
function toUnixTimestamp(time) {
548
if (typeof time == 'number') {
551
if (time instanceof Date) {
552
// convert to 123.456 UNIX timestamp
553
return time.getTime() / 1000;
555
throw new Error('Cannot parse time: ' + time);
558
// exported for unit tests, not for public consumption
559
fs._toUnixTimestamp = toUnixTimestamp;
561
fs.utimes = function(path, atime, mtime, callback) {
562
atime = toUnixTimestamp(atime);
563
mtime = toUnixTimestamp(mtime);
564
binding.utimes(pathModule._makeLong(path), atime, mtime, callback || noop);
567
fs.utimesSync = function(path, atime, mtime) {
568
atime = toUnixTimestamp(atime);
569
mtime = toUnixTimestamp(mtime);
570
binding.utimes(pathModule._makeLong(path), atime, mtime);
573
fs.futimes = function(fd, atime, mtime, callback) {
574
atime = toUnixTimestamp(atime);
575
mtime = toUnixTimestamp(mtime);
576
binding.futimes(fd, atime, mtime, callback || noop);
579
fs.futimesSync = function(fd, atime, mtime) {
580
atime = toUnixTimestamp(atime);
581
mtime = toUnixTimestamp(mtime);
582
binding.futimes(fd, atime, mtime);
451
585
function writeAll(fd, buffer, offset, length, callback) {
493
628
fs.closeSync(fd);
632
function errnoException(errorno, syscall) {
633
// TODO make this more compatible with ErrnoException from src/node.cc
634
// Once all of Node is using this function the ErrnoException from
635
// src/node.cc should be removed.
636
var e = new Error(syscall + ' ' + errorno);
637
e.errno = e.code = errorno;
643
function FSWatcher() {
645
var FSEvent = process.binding('fs_event_wrap').FSEvent;
646
this._handle = new FSEvent();
648
this._handle.onchange = function(status, event, filename) {
650
self.emit('error', errnoException(errno, 'watch'));
652
self.emit('change', event, filename);
656
util.inherits(FSWatcher, EventEmitter);
658
FSWatcher.prototype.start = function(filename, persistent) {
659
var r = this._handle.start(pathModule._makeLong(filename), persistent);
662
this._handle.close();
663
throw errnoException(errno, 'watch');
667
FSWatcher.prototype.close = function() {
668
this._handle.close();
671
fs.watch = function(filename) {
676
if ('object' == typeof arguments[1]) {
677
options = arguments[1];
678
listener = arguments[2];
681
listener = arguments[1];
685
throw new Error('watch requires a listener function');
688
if (options.persistent === undefined) options.persistent = true;
690
watcher = new FSWatcher();
691
watcher.start(filename, options.persistent);
693
watcher.addListener('change', listener);
496
698
// Stat Change Watchers
700
function StatWatcher() {
702
this._handle = new binding.StatWatcher();
704
this._handle.onchange = function(current, previous) {
705
self.emit('change', current, previous);
708
this._handle.onstop = function() {
712
util.inherits(StatWatcher, EventEmitter);
715
StatWatcher.prototype.start = function(filename, persistent, interval) {
716
this._handle.start(pathModule._makeLong(filename), persistent, interval);
720
StatWatcher.prototype.stop = function() {
498
725
var statWatchers = {};
726
function inStatWatchers(filename) {
727
return Object.prototype.hasOwnProperty.call(statWatchers, filename) &&
728
statWatchers[filename];
500
732
fs.watchFile = function(filename) {
734
throw new Error('use fs.watch api instead');