Skip to content

Commit

Permalink
file watch and stat
Browse files Browse the repository at this point in the history
  • Loading branch information
garrensmith authored and tj committed Nov 8, 2010
1 parent d067898 commit a5b50e0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
44 changes: 35 additions & 9 deletions chapters/fs.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@

# File System

The 'fs' module is the library to work with the filesystem. The commands follows the unix operations to work with the filesystem.
The 'fs' module is the library to work with the filesystem. The commands follows the UNIX operations to work with the filesystem.
Most methods support an asynchronous and synchronous method call.

//However using the synchronous call will cause the whole process to halt
//and wait for this operation to complete. It is therefore to be used on in special cases.

## Working with the filesystem

A basic example of working with the filesystem using chainable callbacks:
Expand All @@ -27,10 +24,11 @@
});
});

When working with the asychronous methods, each call must be inside the previous method call as there is no gaurantee that the
previous call will complete before the next one. This could cause operations to happen in the wrong order.

This can also be done using a synchronous approach:
When working with the asynchronous methods, the following operation on a file should be inside the callback of the previous operation.
This is because there is no guarantee that the operations will be completed in the order that they are created. This could lead to
unpredictable behavior.

The above example can also be done using a synchronous approach:

fs.mkdirSync('./helloDirSync',0777);
fs.writeFileSync('./helloDirSync/message.txt', 'Hello Node');
Expand All @@ -44,7 +42,8 @@
## File information

The fs.Stats object contains information about a particular file or directory. This can be used to determine what type of object we
are working with. In this example we are getting all the file objects in a directory and display whether they are a file or an object.
are working with. In this example we are getting all the file objects in a directory and displaying whether they are a file or a
directory object.

var fs = require('fs');

Expand All @@ -66,6 +65,33 @@
});
});


## Watching files

The fs.watchfile monitors a file and will fire the event whenever the file is changed.

var fs = require('fs');

fs.watchFile('./testFile.txt', function (curr, prev) {
console.log('the current mtime is: ' + curr.mtime);
console.log('the previous mtime was: ' + prev.mtime);
});

fs.writeFile('./testFile.txt', "changed", function (err) {
if (err) throw err;

console.log("file write complete");
});

A file can also be unwatched using the fs.unwatchFile method call. This is used once watching of a file is no longer required.


## Nodejs Docs

The node api [docs](http://nodejs.org/api.html#file-system-106) are very detailed and list all the possible filesystem commands
possible from Nodejs.





Expand Down
8 changes: 4 additions & 4 deletions src/fs/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ fs.readdir('/etc/', function (err, files) {
console.log("%s is file", file);
}
else if (stats.isDirectory ()) {
console.log("%s is a directory", file);
console.log("%s is a directory", file);
}
console.log('stats: %s',JSON.stringify(stats));

});
console.log('stats: %s',JSON.stringify(stats));

});

});
});


13 changes: 13 additions & 0 deletions src/fs/watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var fs = require('fs');

fs.watchFile('./testFile.txt', function (curr, prev) {
console.log('the current mtime is: ' + curr.mtime);
console.log('the previous mtime was: ' + prev.mtime);
});

fs.writeFile('./testFile.txt', "changed", function (err) {
if (err) throw err;

console.log("file write complete");
});

0 comments on commit a5b50e0

Please sign in to comment.