~yolanda.robla/ubuntu/trusty/nodejs/add_distribution

« back to all changes in this revision

Viewing changes to doc/api/fs.html

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!doctype html>
 
2
<html lang="en">
 
3
<head>
 
4
  <meta charset="utf-8">
 
5
  <title>File System Node.js v0.10.15 Manual &amp; Documentation</title>
 
6
  <link rel="stylesheet" href="assets/style.css">
 
7
  <link rel="stylesheet" href="assets/sh.css">
 
8
  <link rel="canonical" href="http://nodejs.org/api/fs.html">
 
9
</head>
 
10
<body class="alt apidoc" id="api-section-fs">
 
11
    <div id="intro" class="interior">
 
12
        <a href="/" title="Go back to the home page">
 
13
            <img id="logo" src="http://nodejs.org/images/logo-light.png" alt="node.js">
 
14
        </a>
 
15
    </div>
 
16
    <div id="content" class="clearfix">
 
17
        <div id="column2" class="interior">
 
18
            <ul>
 
19
                <li><a href="/" class="home">Home</a></li>
 
20
                <li><a href="/download/" class="download">Download</a></li>
 
21
                <li><a href="/about/" class="about">About</a></li>
 
22
                <li><a href="http://npmjs.org/" class="npm">npm Registry</a></li>
 
23
                <li><a href="http://nodejs.org/api/" class="docs current">Docs</a></li>
 
24
                <li><a href="http://blog.nodejs.org" class="blog">Blog</a></li>
 
25
                <li><a href="/community/" class="community">Community</a></li>
 
26
                <li><a href="/logos/" class="logos">Logos</a></li>
 
27
                <li><a href="http://jobs.nodejs.org/" class="jobs">Jobs</a></li>
 
28
            </ul>
 
29
            <p class="twitter"><a href="http://twitter.com/nodejs">@nodejs</a></p>
 
30
        </div>
 
31
 
 
32
        <div id="column1" class="interior">
 
33
          <header>
 
34
            <h1>Node.js v0.10.15 Manual &amp; Documentation</h1>
 
35
            <div id="gtoc">
 
36
              <p>
 
37
                <a href="index.html" name="toc">Index</a> |
 
38
                <a href="all.html">View on single page</a> |
 
39
                <a href="fs.json">View as JSON</a>
 
40
              </p>
 
41
            </div>
 
42
            <hr>
 
43
          </header>
 
44
 
 
45
          <div id="toc">
 
46
            <h2>Table of Contents</h2>
 
47
            <ul>
 
48
<li><a href="#fs_file_system">File System</a><ul>
 
49
<li><a href="#fs_fs_rename_oldpath_newpath_callback">fs.rename(oldPath, newPath, callback)</a></li>
 
50
<li><a href="#fs_fs_renamesync_oldpath_newpath">fs.renameSync(oldPath, newPath)</a></li>
 
51
<li><a href="#fs_fs_ftruncate_fd_len_callback">fs.ftruncate(fd, len, callback)</a></li>
 
52
<li><a href="#fs_fs_ftruncatesync_fd_len">fs.ftruncateSync(fd, len)</a></li>
 
53
<li><a href="#fs_fs_truncate_path_len_callback">fs.truncate(path, len, callback)</a></li>
 
54
<li><a href="#fs_fs_truncatesync_path_len">fs.truncateSync(path, len)</a></li>
 
55
<li><a href="#fs_fs_chown_path_uid_gid_callback">fs.chown(path, uid, gid, callback)</a></li>
 
56
<li><a href="#fs_fs_chownsync_path_uid_gid">fs.chownSync(path, uid, gid)</a></li>
 
57
<li><a href="#fs_fs_fchown_fd_uid_gid_callback">fs.fchown(fd, uid, gid, callback)</a></li>
 
58
<li><a href="#fs_fs_fchownsync_fd_uid_gid">fs.fchownSync(fd, uid, gid)</a></li>
 
59
<li><a href="#fs_fs_lchown_path_uid_gid_callback">fs.lchown(path, uid, gid, callback)</a></li>
 
60
<li><a href="#fs_fs_lchownsync_path_uid_gid">fs.lchownSync(path, uid, gid)</a></li>
 
61
<li><a href="#fs_fs_chmod_path_mode_callback">fs.chmod(path, mode, callback)</a></li>
 
62
<li><a href="#fs_fs_chmodsync_path_mode">fs.chmodSync(path, mode)</a></li>
 
63
<li><a href="#fs_fs_fchmod_fd_mode_callback">fs.fchmod(fd, mode, callback)</a></li>
 
64
<li><a href="#fs_fs_fchmodsync_fd_mode">fs.fchmodSync(fd, mode)</a></li>
 
65
<li><a href="#fs_fs_lchmod_path_mode_callback">fs.lchmod(path, mode, callback)</a></li>
 
66
<li><a href="#fs_fs_lchmodsync_path_mode">fs.lchmodSync(path, mode)</a></li>
 
67
<li><a href="#fs_fs_stat_path_callback">fs.stat(path, callback)</a></li>
 
68
<li><a href="#fs_fs_lstat_path_callback">fs.lstat(path, callback)</a></li>
 
69
<li><a href="#fs_fs_fstat_fd_callback">fs.fstat(fd, callback)</a></li>
 
70
<li><a href="#fs_fs_statsync_path">fs.statSync(path)</a></li>
 
71
<li><a href="#fs_fs_lstatsync_path">fs.lstatSync(path)</a></li>
 
72
<li><a href="#fs_fs_fstatsync_fd">fs.fstatSync(fd)</a></li>
 
73
<li><a href="#fs_fs_link_srcpath_dstpath_callback">fs.link(srcpath, dstpath, callback)</a></li>
 
74
<li><a href="#fs_fs_linksync_srcpath_dstpath">fs.linkSync(srcpath, dstpath)</a></li>
 
75
<li><a href="#fs_fs_symlink_srcpath_dstpath_type_callback">fs.symlink(srcpath, dstpath, [type], callback)</a></li>
 
76
<li><a href="#fs_fs_symlinksync_srcpath_dstpath_type">fs.symlinkSync(srcpath, dstpath, [type])</a></li>
 
77
<li><a href="#fs_fs_readlink_path_callback">fs.readlink(path, callback)</a></li>
 
78
<li><a href="#fs_fs_readlinksync_path">fs.readlinkSync(path)</a></li>
 
79
<li><a href="#fs_fs_realpath_path_cache_callback">fs.realpath(path, [cache], callback)</a></li>
 
80
<li><a href="#fs_fs_realpathsync_path_cache">fs.realpathSync(path, [cache])</a></li>
 
81
<li><a href="#fs_fs_unlink_path_callback">fs.unlink(path, callback)</a></li>
 
82
<li><a href="#fs_fs_unlinksync_path">fs.unlinkSync(path)</a></li>
 
83
<li><a href="#fs_fs_rmdir_path_callback">fs.rmdir(path, callback)</a></li>
 
84
<li><a href="#fs_fs_rmdirsync_path">fs.rmdirSync(path)</a></li>
 
85
<li><a href="#fs_fs_mkdir_path_mode_callback">fs.mkdir(path, [mode], callback)</a></li>
 
86
<li><a href="#fs_fs_mkdirsync_path_mode">fs.mkdirSync(path, [mode])</a></li>
 
87
<li><a href="#fs_fs_readdir_path_callback">fs.readdir(path, callback)</a></li>
 
88
<li><a href="#fs_fs_readdirsync_path">fs.readdirSync(path)</a></li>
 
89
<li><a href="#fs_fs_close_fd_callback">fs.close(fd, callback)</a></li>
 
90
<li><a href="#fs_fs_closesync_fd">fs.closeSync(fd)</a></li>
 
91
<li><a href="#fs_fs_open_path_flags_mode_callback">fs.open(path, flags, [mode], callback)</a></li>
 
92
<li><a href="#fs_fs_opensync_path_flags_mode">fs.openSync(path, flags, [mode])</a></li>
 
93
<li><a href="#fs_fs_utimes_path_atime_mtime_callback">fs.utimes(path, atime, mtime, callback)</a></li>
 
94
<li><a href="#fs_fs_utimessync_path_atime_mtime">fs.utimesSync(path, atime, mtime)</a></li>
 
95
<li><a href="#fs_fs_futimes_fd_atime_mtime_callback">fs.futimes(fd, atime, mtime, callback)</a></li>
 
96
<li><a href="#fs_fs_futimessync_fd_atime_mtime">fs.futimesSync(fd, atime, mtime)</a></li>
 
97
<li><a href="#fs_fs_fsync_fd_callback">fs.fsync(fd, callback)</a></li>
 
98
<li><a href="#fs_fs_fsyncsync_fd">fs.fsyncSync(fd)</a></li>
 
99
<li><a href="#fs_fs_write_fd_buffer_offset_length_position_callback">fs.write(fd, buffer, offset, length, position, callback)</a></li>
 
100
<li><a href="#fs_fs_writesync_fd_buffer_offset_length_position">fs.writeSync(fd, buffer, offset, length, position)</a></li>
 
101
<li><a href="#fs_fs_read_fd_buffer_offset_length_position_callback">fs.read(fd, buffer, offset, length, position, callback)</a></li>
 
102
<li><a href="#fs_fs_readsync_fd_buffer_offset_length_position">fs.readSync(fd, buffer, offset, length, position)</a></li>
 
103
<li><a href="#fs_fs_readfile_filename_options_callback">fs.readFile(filename, [options], callback)</a></li>
 
104
<li><a href="#fs_fs_readfilesync_filename_options">fs.readFileSync(filename, [options])</a></li>
 
105
<li><a href="#fs_fs_writefile_filename_data_options_callback">fs.writeFile(filename, data, [options], callback)</a></li>
 
106
<li><a href="#fs_fs_writefilesync_filename_data_options">fs.writeFileSync(filename, data, [options])</a></li>
 
107
<li><a href="#fs_fs_appendfile_filename_data_options_callback">fs.appendFile(filename, data, [options], callback)</a></li>
 
108
<li><a href="#fs_fs_appendfilesync_filename_data_options">fs.appendFileSync(filename, data, [options])</a></li>
 
109
<li><a href="#fs_fs_watchfile_filename_options_listener">fs.watchFile(filename, [options], listener)</a></li>
 
110
<li><a href="#fs_fs_unwatchfile_filename_listener">fs.unwatchFile(filename, [listener])</a></li>
 
111
<li><a href="#fs_fs_watch_filename_options_listener">fs.watch(filename, [options], [listener])</a><ul>
 
112
<li><a href="#fs_caveats">Caveats</a><ul>
 
113
<li><a href="#fs_availability">Availability</a></li>
 
114
<li><a href="#fs_filename_argument">Filename Argument</a></li>
 
115
</ul>
 
116
</li>
 
117
</ul>
 
118
</li>
 
119
<li><a href="#fs_fs_exists_path_callback">fs.exists(path, callback)</a></li>
 
120
<li><a href="#fs_fs_existssync_path">fs.existsSync(path)</a></li>
 
121
<li><a href="#fs_class_fs_stats">Class: fs.Stats</a></li>
 
122
<li><a href="#fs_fs_createreadstream_path_options">fs.createReadStream(path, [options])</a></li>
 
123
<li><a href="#fs_class_fs_readstream">Class: fs.ReadStream</a><ul>
 
124
<li><a href="#fs_event_open">Event: &#39;open&#39;</a></li>
 
125
</ul>
 
126
</li>
 
127
<li><a href="#fs_fs_createwritestream_path_options">fs.createWriteStream(path, [options])</a></li>
 
128
<li><a href="#fs_class_fs_writestream">Class: fs.WriteStream</a><ul>
 
129
<li><a href="#fs_event_open_1">Event: &#39;open&#39;</a></li>
 
130
<li><a href="#fs_file_byteswritten">file.bytesWritten</a></li>
 
131
</ul>
 
132
</li>
 
133
<li><a href="#fs_class_fs_fswatcher">Class: fs.FSWatcher</a><ul>
 
134
<li><a href="#fs_watcher_close">watcher.close()</a></li>
 
135
<li><a href="#fs_event_change">Event: &#39;change&#39;</a></li>
 
136
<li><a href="#fs_event_error">Event: &#39;error&#39;</a></li>
 
137
</ul>
 
138
</li>
 
139
</ul>
 
140
</li>
 
141
</ul>
 
142
 
 
143
          </div>
 
144
 
 
145
          <div id="apicontent">
 
146
            <h1>File System<span><a class="mark" href="#fs_file_system" id="fs_file_system">#</a></span></h1>
 
147
<pre class="api_stability_3">Stability: 3 - Stable</pre><!--name=fs-->
 
148
 
 
149
<p>File I/O is provided by simple wrappers around standard POSIX functions.  To
 
150
use this module do <code>require(&#39;fs&#39;)</code>. All the methods have asynchronous and
 
151
synchronous forms.
 
152
 
 
153
</p>
 
154
<p>The asynchronous form always take a completion callback as its last argument.
 
155
The arguments passed to the completion callback depend on the method, but the
 
156
first argument is always reserved for an exception. If the operation was
 
157
completed successfully, then the first argument will be <code>null</code> or <code>undefined</code>.
 
158
 
 
159
</p>
 
160
<p>When using the synchronous form any exceptions are immediately thrown.
 
161
You can use try/catch to handle exceptions or allow them to bubble up.
 
162
 
 
163
</p>
 
164
<p>Here is an example of the asynchronous version:
 
165
 
 
166
</p>
 
167
<pre><code>var fs = require(&#39;fs&#39;);
 
168
 
 
169
fs.unlink(&#39;/tmp/hello&#39;, function (err) {
 
170
  if (err) throw err;
 
171
  console.log(&#39;successfully deleted /tmp/hello&#39;);
 
172
});</code></pre>
 
173
<p>Here is the synchronous version:
 
174
 
 
175
</p>
 
176
<pre><code>var fs = require(&#39;fs&#39;);
 
177
 
 
178
fs.unlinkSync(&#39;/tmp/hello&#39;)
 
179
console.log(&#39;successfully deleted /tmp/hello&#39;);</code></pre>
 
180
<p>With the asynchronous methods there is no guaranteed ordering. So the
 
181
following is prone to error:
 
182
 
 
183
</p>
 
184
<pre><code>fs.rename(&#39;/tmp/hello&#39;, &#39;/tmp/world&#39;, function (err) {
 
185
  if (err) throw err;
 
186
  console.log(&#39;renamed complete&#39;);
 
187
});
 
188
fs.stat(&#39;/tmp/world&#39;, function (err, stats) {
 
189
  if (err) throw err;
 
190
  console.log(&#39;stats: &#39; + JSON.stringify(stats));
 
191
});</code></pre>
 
192
<p>It could be that <code>fs.stat</code> is executed before <code>fs.rename</code>.
 
193
The correct way to do this is to chain the callbacks.
 
194
 
 
195
</p>
 
196
<pre><code>fs.rename(&#39;/tmp/hello&#39;, &#39;/tmp/world&#39;, function (err) {
 
197
  if (err) throw err;
 
198
  fs.stat(&#39;/tmp/world&#39;, function (err, stats) {
 
199
    if (err) throw err;
 
200
    console.log(&#39;stats: &#39; + JSON.stringify(stats));
 
201
  });
 
202
});</code></pre>
 
203
<p>In busy processes, the programmer is <em>strongly encouraged</em> to use the
 
204
asynchronous versions of these calls. The synchronous versions will block
 
205
the entire process until they complete--halting all connections.
 
206
 
 
207
</p>
 
208
<p>Relative path to filename can be used, remember however that this path will be
 
209
relative to <code>process.cwd()</code>.
 
210
 
 
211
</p>
 
212
<p>Most fs functions let you omit the callback argument. If you do, a default
 
213
callback is used that ignores errors, but prints a deprecation
 
214
warning.
 
215
 
 
216
</p>
 
217
<p><strong>IMPORTANT</strong>: Omitting the callback is deprecated.  v0.12 will throw the
 
218
errors as exceptions.
 
219
 
 
220
 
 
221
</p>
 
222
<h2>fs.rename(oldPath, newPath, callback)<span><a class="mark" href="#fs_fs_rename_oldpath_newpath_callback" id="fs_fs_rename_oldpath_newpath_callback">#</a></span></h2>
 
223
<p>Asynchronous rename(2). No arguments other than a possible exception are given
 
224
to the completion callback.
 
225
 
 
226
</p>
 
227
<h2>fs.renameSync(oldPath, newPath)<span><a class="mark" href="#fs_fs_renamesync_oldpath_newpath" id="fs_fs_renamesync_oldpath_newpath">#</a></span></h2>
 
228
<p>Synchronous rename(2).
 
229
 
 
230
</p>
 
231
<h2>fs.ftruncate(fd, len, callback)<span><a class="mark" href="#fs_fs_ftruncate_fd_len_callback" id="fs_fs_ftruncate_fd_len_callback">#</a></span></h2>
 
232
<p>Asynchronous ftruncate(2). No arguments other than a possible exception are
 
233
given to the completion callback.
 
234
 
 
235
</p>
 
236
<h2>fs.ftruncateSync(fd, len)<span><a class="mark" href="#fs_fs_ftruncatesync_fd_len" id="fs_fs_ftruncatesync_fd_len">#</a></span></h2>
 
237
<p>Synchronous ftruncate(2).
 
238
 
 
239
</p>
 
240
<h2>fs.truncate(path, len, callback)<span><a class="mark" href="#fs_fs_truncate_path_len_callback" id="fs_fs_truncate_path_len_callback">#</a></span></h2>
 
241
<p>Asynchronous truncate(2). No arguments other than a possible exception are
 
242
given to the completion callback.
 
243
 
 
244
</p>
 
245
<h2>fs.truncateSync(path, len)<span><a class="mark" href="#fs_fs_truncatesync_path_len" id="fs_fs_truncatesync_path_len">#</a></span></h2>
 
246
<p>Synchronous truncate(2).
 
247
 
 
248
</p>
 
249
<h2>fs.chown(path, uid, gid, callback)<span><a class="mark" href="#fs_fs_chown_path_uid_gid_callback" id="fs_fs_chown_path_uid_gid_callback">#</a></span></h2>
 
250
<p>Asynchronous chown(2). No arguments other than a possible exception are given
 
251
to the completion callback.
 
252
 
 
253
</p>
 
254
<h2>fs.chownSync(path, uid, gid)<span><a class="mark" href="#fs_fs_chownsync_path_uid_gid" id="fs_fs_chownsync_path_uid_gid">#</a></span></h2>
 
255
<p>Synchronous chown(2).
 
256
 
 
257
</p>
 
258
<h2>fs.fchown(fd, uid, gid, callback)<span><a class="mark" href="#fs_fs_fchown_fd_uid_gid_callback" id="fs_fs_fchown_fd_uid_gid_callback">#</a></span></h2>
 
259
<p>Asynchronous fchown(2). No arguments other than a possible exception are given
 
260
to the completion callback.
 
261
 
 
262
</p>
 
263
<h2>fs.fchownSync(fd, uid, gid)<span><a class="mark" href="#fs_fs_fchownsync_fd_uid_gid" id="fs_fs_fchownsync_fd_uid_gid">#</a></span></h2>
 
264
<p>Synchronous fchown(2).
 
265
 
 
266
</p>
 
267
<h2>fs.lchown(path, uid, gid, callback)<span><a class="mark" href="#fs_fs_lchown_path_uid_gid_callback" id="fs_fs_lchown_path_uid_gid_callback">#</a></span></h2>
 
268
<p>Asynchronous lchown(2). No arguments other than a possible exception are given
 
269
to the completion callback.
 
270
 
 
271
</p>
 
272
<h2>fs.lchownSync(path, uid, gid)<span><a class="mark" href="#fs_fs_lchownsync_path_uid_gid" id="fs_fs_lchownsync_path_uid_gid">#</a></span></h2>
 
273
<p>Synchronous lchown(2).
 
274
 
 
275
</p>
 
276
<h2>fs.chmod(path, mode, callback)<span><a class="mark" href="#fs_fs_chmod_path_mode_callback" id="fs_fs_chmod_path_mode_callback">#</a></span></h2>
 
277
<p>Asynchronous chmod(2). No arguments other than a possible exception are given
 
278
to the completion callback.
 
279
 
 
280
</p>
 
281
<h2>fs.chmodSync(path, mode)<span><a class="mark" href="#fs_fs_chmodsync_path_mode" id="fs_fs_chmodsync_path_mode">#</a></span></h2>
 
282
<p>Synchronous chmod(2).
 
283
 
 
284
</p>
 
285
<h2>fs.fchmod(fd, mode, callback)<span><a class="mark" href="#fs_fs_fchmod_fd_mode_callback" id="fs_fs_fchmod_fd_mode_callback">#</a></span></h2>
 
286
<p>Asynchronous fchmod(2). No arguments other than a possible exception
 
287
are given to the completion callback.
 
288
 
 
289
</p>
 
290
<h2>fs.fchmodSync(fd, mode)<span><a class="mark" href="#fs_fs_fchmodsync_fd_mode" id="fs_fs_fchmodsync_fd_mode">#</a></span></h2>
 
291
<p>Synchronous fchmod(2).
 
292
 
 
293
</p>
 
294
<h2>fs.lchmod(path, mode, callback)<span><a class="mark" href="#fs_fs_lchmod_path_mode_callback" id="fs_fs_lchmod_path_mode_callback">#</a></span></h2>
 
295
<p>Asynchronous lchmod(2). No arguments other than a possible exception
 
296
are given to the completion callback.
 
297
 
 
298
</p>
 
299
<p>Only available on Mac OS X.
 
300
 
 
301
</p>
 
302
<h2>fs.lchmodSync(path, mode)<span><a class="mark" href="#fs_fs_lchmodsync_path_mode" id="fs_fs_lchmodsync_path_mode">#</a></span></h2>
 
303
<p>Synchronous lchmod(2).
 
304
 
 
305
</p>
 
306
<h2>fs.stat(path, callback)<span><a class="mark" href="#fs_fs_stat_path_callback" id="fs_fs_stat_path_callback">#</a></span></h2>
 
307
<p>Asynchronous stat(2). The callback gets two arguments <code>(err, stats)</code> where
 
308
<code>stats</code> is a <a href="#fs_class_fs_stats">fs.Stats</a> object.  See the <a href="#fs_class_fs_stats">fs.Stats</a>
 
309
section below for more information.
 
310
 
 
311
</p>
 
312
<h2>fs.lstat(path, callback)<span><a class="mark" href="#fs_fs_lstat_path_callback" id="fs_fs_lstat_path_callback">#</a></span></h2>
 
313
<p>Asynchronous lstat(2). The callback gets two arguments <code>(err, stats)</code> where
 
314
<code>stats</code> is a <code>fs.Stats</code> object. <code>lstat()</code> is identical to <code>stat()</code>, except that if
 
315
<code>path</code> is a symbolic link, then the link itself is stat-ed, not the file that it
 
316
refers to.
 
317
 
 
318
</p>
 
319
<h2>fs.fstat(fd, callback)<span><a class="mark" href="#fs_fs_fstat_fd_callback" id="fs_fs_fstat_fd_callback">#</a></span></h2>
 
320
<p>Asynchronous fstat(2). The callback gets two arguments <code>(err, stats)</code> where
 
321
<code>stats</code> is a <code>fs.Stats</code> object. <code>fstat()</code> is identical to <code>stat()</code>, except that
 
322
the file to be stat-ed is specified by the file descriptor <code>fd</code>.
 
323
 
 
324
</p>
 
325
<h2>fs.statSync(path)<span><a class="mark" href="#fs_fs_statsync_path" id="fs_fs_statsync_path">#</a></span></h2>
 
326
<p>Synchronous stat(2). Returns an instance of <code>fs.Stats</code>.
 
327
 
 
328
</p>
 
329
<h2>fs.lstatSync(path)<span><a class="mark" href="#fs_fs_lstatsync_path" id="fs_fs_lstatsync_path">#</a></span></h2>
 
330
<p>Synchronous lstat(2). Returns an instance of <code>fs.Stats</code>.
 
331
 
 
332
</p>
 
333
<h2>fs.fstatSync(fd)<span><a class="mark" href="#fs_fs_fstatsync_fd" id="fs_fs_fstatsync_fd">#</a></span></h2>
 
334
<p>Synchronous fstat(2). Returns an instance of <code>fs.Stats</code>.
 
335
 
 
336
</p>
 
337
<h2>fs.link(srcpath, dstpath, callback)<span><a class="mark" href="#fs_fs_link_srcpath_dstpath_callback" id="fs_fs_link_srcpath_dstpath_callback">#</a></span></h2>
 
338
<p>Asynchronous link(2). No arguments other than a possible exception are given to
 
339
the completion callback.
 
340
 
 
341
</p>
 
342
<h2>fs.linkSync(srcpath, dstpath)<span><a class="mark" href="#fs_fs_linksync_srcpath_dstpath" id="fs_fs_linksync_srcpath_dstpath">#</a></span></h2>
 
343
<p>Synchronous link(2).
 
344
 
 
345
</p>
 
346
<h2>fs.symlink(srcpath, dstpath, [type], callback)<span><a class="mark" href="#fs_fs_symlink_srcpath_dstpath_type_callback" id="fs_fs_symlink_srcpath_dstpath_type_callback">#</a></span></h2>
 
347
<p>Asynchronous symlink(2). No arguments other than a possible exception are given
 
348
to the completion callback.
 
349
<code>type</code> argument can be either <code>&#39;dir&#39;</code>, <code>&#39;file&#39;</code>, or <code>&#39;junction&#39;</code> (default is <code>&#39;file&#39;</code>).  It is only 
 
350
used on Windows (ignored on other platforms).
 
351
Note that Windows junction points require the destination path to be absolute.  When using
 
352
<code>&#39;junction&#39;</code>, the <code>destination</code> argument will automatically be normalized to absolute path.
 
353
 
 
354
</p>
 
355
<h2>fs.symlinkSync(srcpath, dstpath, [type])<span><a class="mark" href="#fs_fs_symlinksync_srcpath_dstpath_type" id="fs_fs_symlinksync_srcpath_dstpath_type">#</a></span></h2>
 
356
<p>Synchronous symlink(2).
 
357
 
 
358
</p>
 
359
<h2>fs.readlink(path, callback)<span><a class="mark" href="#fs_fs_readlink_path_callback" id="fs_fs_readlink_path_callback">#</a></span></h2>
 
360
<p>Asynchronous readlink(2). The callback gets two arguments <code>(err,
 
361
linkString)</code>.
 
362
 
 
363
</p>
 
364
<h2>fs.readlinkSync(path)<span><a class="mark" href="#fs_fs_readlinksync_path" id="fs_fs_readlinksync_path">#</a></span></h2>
 
365
<p>Synchronous readlink(2). Returns the symbolic link&#39;s string value.
 
366
 
 
367
</p>
 
368
<h2>fs.realpath(path, [cache], callback)<span><a class="mark" href="#fs_fs_realpath_path_cache_callback" id="fs_fs_realpath_path_cache_callback">#</a></span></h2>
 
369
<p>Asynchronous realpath(2). The <code>callback</code> gets two arguments <code>(err,
 
370
resolvedPath)</code>. May use <code>process.cwd</code> to resolve relative paths. <code>cache</code> is an
 
371
object literal of mapped paths that can be used to force a specific path
 
372
resolution or avoid additional <code>fs.stat</code> calls for known real paths.
 
373
 
 
374
</p>
 
375
<p>Example:
 
376
 
 
377
</p>
 
378
<pre><code>var cache = {&#39;/etc&#39;:&#39;/private/etc&#39;};
 
379
fs.realpath(&#39;/etc/passwd&#39;, cache, function (err, resolvedPath) {
 
380
  if (err) throw err;
 
381
  console.log(resolvedPath);
 
382
});</code></pre>
 
383
<h2>fs.realpathSync(path, [cache])<span><a class="mark" href="#fs_fs_realpathsync_path_cache" id="fs_fs_realpathsync_path_cache">#</a></span></h2>
 
384
<p>Synchronous realpath(2). Returns the resolved path.
 
385
 
 
386
</p>
 
387
<h2>fs.unlink(path, callback)<span><a class="mark" href="#fs_fs_unlink_path_callback" id="fs_fs_unlink_path_callback">#</a></span></h2>
 
388
<p>Asynchronous unlink(2). No arguments other than a possible exception are given
 
389
to the completion callback.
 
390
 
 
391
</p>
 
392
<h2>fs.unlinkSync(path)<span><a class="mark" href="#fs_fs_unlinksync_path" id="fs_fs_unlinksync_path">#</a></span></h2>
 
393
<p>Synchronous unlink(2).
 
394
 
 
395
</p>
 
396
<h2>fs.rmdir(path, callback)<span><a class="mark" href="#fs_fs_rmdir_path_callback" id="fs_fs_rmdir_path_callback">#</a></span></h2>
 
397
<p>Asynchronous rmdir(2). No arguments other than a possible exception are given
 
398
to the completion callback.
 
399
 
 
400
</p>
 
401
<h2>fs.rmdirSync(path)<span><a class="mark" href="#fs_fs_rmdirsync_path" id="fs_fs_rmdirsync_path">#</a></span></h2>
 
402
<p>Synchronous rmdir(2).
 
403
 
 
404
</p>
 
405
<h2>fs.mkdir(path, [mode], callback)<span><a class="mark" href="#fs_fs_mkdir_path_mode_callback" id="fs_fs_mkdir_path_mode_callback">#</a></span></h2>
 
406
<p>Asynchronous mkdir(2). No arguments other than a possible exception are given
 
407
to the completion callback. <code>mode</code> defaults to <code>0777</code>.
 
408
 
 
409
</p>
 
410
<h2>fs.mkdirSync(path, [mode])<span><a class="mark" href="#fs_fs_mkdirsync_path_mode" id="fs_fs_mkdirsync_path_mode">#</a></span></h2>
 
411
<p>Synchronous mkdir(2).
 
412
 
 
413
</p>
 
414
<h2>fs.readdir(path, callback)<span><a class="mark" href="#fs_fs_readdir_path_callback" id="fs_fs_readdir_path_callback">#</a></span></h2>
 
415
<p>Asynchronous readdir(3).  Reads the contents of a directory.
 
416
The callback gets two arguments <code>(err, files)</code> where <code>files</code> is an array of
 
417
the names of the files in the directory excluding <code>&#39;.&#39;</code> and <code>&#39;..&#39;</code>.
 
418
 
 
419
</p>
 
420
<h2>fs.readdirSync(path)<span><a class="mark" href="#fs_fs_readdirsync_path" id="fs_fs_readdirsync_path">#</a></span></h2>
 
421
<p>Synchronous readdir(3). Returns an array of filenames excluding <code>&#39;.&#39;</code> and
 
422
<code>&#39;..&#39;</code>.
 
423
 
 
424
</p>
 
425
<h2>fs.close(fd, callback)<span><a class="mark" href="#fs_fs_close_fd_callback" id="fs_fs_close_fd_callback">#</a></span></h2>
 
426
<p>Asynchronous close(2).  No arguments other than a possible exception are given
 
427
to the completion callback.
 
428
 
 
429
</p>
 
430
<h2>fs.closeSync(fd)<span><a class="mark" href="#fs_fs_closesync_fd" id="fs_fs_closesync_fd">#</a></span></h2>
 
431
<p>Synchronous close(2).
 
432
 
 
433
</p>
 
434
<h2>fs.open(path, flags, [mode], callback)<span><a class="mark" href="#fs_fs_open_path_flags_mode_callback" id="fs_fs_open_path_flags_mode_callback">#</a></span></h2>
 
435
<p>Asynchronous file open. See open(2). <code>flags</code> can be:
 
436
 
 
437
</p>
 
438
<ul>
 
439
<li><p><code>&#39;r&#39;</code> - Open file for reading.
 
440
An exception occurs if the file does not exist.</p>
 
441
</li>
 
442
<li><p><code>&#39;r+&#39;</code> - Open file for reading and writing.
 
443
An exception occurs if the file does not exist.</p>
 
444
</li>
 
445
<li><p><code>&#39;rs&#39;</code> - Open file for reading in synchronous mode. Instructs the operating
 
446
system to bypass the local file system cache.</p>
 
447
<p>This is primarily useful for opening files on NFS mounts as it allows you to
 
448
skip the potentially stale local cache. It has a very real impact on I/O
 
449
performance so don&#39;t use this mode unless you need it.</p>
 
450
<p>Note that this doesn&#39;t turn <code>fs.open()</code> into a synchronous blocking call.
 
451
If that&#39;s what you want then you should be using <code>fs.openSync()</code></p>
 
452
</li>
 
453
<li><p><code>&#39;rs+&#39;</code> - Open file for reading and writing, telling the OS to open it
 
454
synchronously. See notes for <code>&#39;rs&#39;</code> about using this with caution.</p>
 
455
</li>
 
456
<li><p><code>&#39;w&#39;</code> - Open file for writing.
 
457
The file is created (if it does not exist) or truncated (if it exists).</p>
 
458
</li>
 
459
<li><p><code>&#39;wx&#39;</code> - Like <code>&#39;w&#39;</code> but opens the file in exclusive mode.</p>
 
460
</li>
 
461
<li><p><code>&#39;w+&#39;</code> - Open file for reading and writing.
 
462
The file is created (if it does not exist) or truncated (if it exists).</p>
 
463
</li>
 
464
<li><p><code>&#39;wx+&#39;</code> - Like <code>&#39;w+&#39;</code> but opens the file in exclusive mode.</p>
 
465
</li>
 
466
<li><p><code>&#39;a&#39;</code> - Open file for appending.
 
467
The file is created if it does not exist.</p>
 
468
</li>
 
469
<li><p><code>&#39;ax&#39;</code> - Like <code>&#39;a&#39;</code> but opens the file in exclusive mode.</p>
 
470
</li>
 
471
<li><p><code>&#39;a+&#39;</code> - Open file for reading and appending.
 
472
The file is created if it does not exist.</p>
 
473
</li>
 
474
<li><p><code>&#39;ax+&#39;</code> - Like <code>&#39;a+&#39;</code> but opens the file in exclusive mode.</p>
 
475
</li>
 
476
</ul>
 
477
<p><code>mode</code> defaults to <code>0666</code>. The callback gets two arguments <code>(err, fd)</code>.
 
478
 
 
479
</p>
 
480
<p>Exclusive mode (<code>O_EXCL</code>) ensures that <code>path</code> is newly created. <code>fs.open()</code>
 
481
fails if a file by that name already exists. On POSIX systems, symlinks are
 
482
not followed. Exclusive mode may or may not work with network file systems.
 
483
 
 
484
</p>
 
485
<p>On Linux, positional writes don&#39;t work when the file is opened in append mode.
 
486
The kernel ignores the position argument and always appends the data to
 
487
the end of the file.
 
488
 
 
489
</p>
 
490
<h2>fs.openSync(path, flags, [mode])<span><a class="mark" href="#fs_fs_opensync_path_flags_mode" id="fs_fs_opensync_path_flags_mode">#</a></span></h2>
 
491
<p>Synchronous open(2).
 
492
 
 
493
</p>
 
494
<h2>fs.utimes(path, atime, mtime, callback)<span><a class="mark" href="#fs_fs_utimes_path_atime_mtime_callback" id="fs_fs_utimes_path_atime_mtime_callback">#</a></span></h2>
 
495
<h2>fs.utimesSync(path, atime, mtime)<span><a class="mark" href="#fs_fs_utimessync_path_atime_mtime" id="fs_fs_utimessync_path_atime_mtime">#</a></span></h2>
 
496
<p>Change file timestamps of the file referenced by the supplied path.
 
497
 
 
498
</p>
 
499
<h2>fs.futimes(fd, atime, mtime, callback)<span><a class="mark" href="#fs_fs_futimes_fd_atime_mtime_callback" id="fs_fs_futimes_fd_atime_mtime_callback">#</a></span></h2>
 
500
<h2>fs.futimesSync(fd, atime, mtime)<span><a class="mark" href="#fs_fs_futimessync_fd_atime_mtime" id="fs_fs_futimessync_fd_atime_mtime">#</a></span></h2>
 
501
<p>Change the file timestamps of a file referenced by the supplied file
 
502
descriptor.
 
503
 
 
504
</p>
 
505
<h2>fs.fsync(fd, callback)<span><a class="mark" href="#fs_fs_fsync_fd_callback" id="fs_fs_fsync_fd_callback">#</a></span></h2>
 
506
<p>Asynchronous fsync(2). No arguments other than a possible exception are given
 
507
to the completion callback.
 
508
 
 
509
</p>
 
510
<h2>fs.fsyncSync(fd)<span><a class="mark" href="#fs_fs_fsyncsync_fd" id="fs_fs_fsyncsync_fd">#</a></span></h2>
 
511
<p>Synchronous fsync(2).
 
512
 
 
513
</p>
 
514
<h2>fs.write(fd, buffer, offset, length, position, callback)<span><a class="mark" href="#fs_fs_write_fd_buffer_offset_length_position_callback" id="fs_fs_write_fd_buffer_offset_length_position_callback">#</a></span></h2>
 
515
<p>Write <code>buffer</code> to the file specified by <code>fd</code>.
 
516
 
 
517
</p>
 
518
<p><code>offset</code> and <code>length</code> determine the part of the buffer to be written.
 
519
 
 
520
</p>
 
521
<p><code>position</code> refers to the offset from the beginning of the file where this data
 
522
should be written. If <code>position</code> is <code>null</code>, the data will be written at the
 
523
current position.
 
524
See pwrite(2).
 
525
 
 
526
</p>
 
527
<p>The callback will be given three arguments <code>(err, written, buffer)</code> where <code>written</code>
 
528
specifies how many <em>bytes</em> were written from <code>buffer</code>.
 
529
 
 
530
</p>
 
531
<p>Note that it is unsafe to use <code>fs.write</code> multiple times on the same file
 
532
without waiting for the callback. For this scenario,
 
533
<code>fs.createWriteStream</code> is strongly recommended.
 
534
 
 
535
</p>
 
536
<p>On Linux, positional writes don&#39;t work when the file is opened in append mode.
 
537
The kernel ignores the position argument and always appends the data to
 
538
the end of the file.
 
539
 
 
540
</p>
 
541
<h2>fs.writeSync(fd, buffer, offset, length, position)<span><a class="mark" href="#fs_fs_writesync_fd_buffer_offset_length_position" id="fs_fs_writesync_fd_buffer_offset_length_position">#</a></span></h2>
 
542
<p>Synchronous version of <code>fs.write()</code>. Returns the number of bytes written.
 
543
 
 
544
</p>
 
545
<h2>fs.read(fd, buffer, offset, length, position, callback)<span><a class="mark" href="#fs_fs_read_fd_buffer_offset_length_position_callback" id="fs_fs_read_fd_buffer_offset_length_position_callback">#</a></span></h2>
 
546
<p>Read data from the file specified by <code>fd</code>.
 
547
 
 
548
</p>
 
549
<p><code>buffer</code> is the buffer that the data will be written to.
 
550
 
 
551
</p>
 
552
<p><code>offset</code> is offset within the buffer where reading will start.
 
553
 
 
554
</p>
 
555
<p><code>length</code> is an integer specifying the number of bytes to read.
 
556
 
 
557
</p>
 
558
<p><code>position</code> is an integer specifying where to begin reading from in the file.
 
559
If <code>position</code> is <code>null</code>, data will be read from the current file position.
 
560
 
 
561
</p>
 
562
<p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.
 
563
 
 
564
</p>
 
565
<h2>fs.readSync(fd, buffer, offset, length, position)<span><a class="mark" href="#fs_fs_readsync_fd_buffer_offset_length_position" id="fs_fs_readsync_fd_buffer_offset_length_position">#</a></span></h2>
 
566
<p>Synchronous version of <code>fs.read</code>. Returns the number of <code>bytesRead</code>.
 
567
 
 
568
</p>
 
569
<h2>fs.readFile(filename, [options], callback)<span><a class="mark" href="#fs_fs_readfile_filename_options_callback" id="fs_fs_readfile_filename_options_callback">#</a></span></h2>
 
570
<div class="signature"><ul>
 
571
<li><code>filename</code> <span class="type">String</span></li>
 
572
<li><code>options</code> <span class="type">Object</span><ul>
 
573
<li><code>encoding</code> <span class="type">String | Null</span> default = <code>null</code></li>
 
574
<li><code>flag</code> <span class="type">String</span> default = <code>&#39;r&#39;</code></li>
 
575
</ul>
 
576
</li>
 
577
<li><code>callback</code> <span class="type">Function</span></li>
 
578
</div></ul>
 
579
<p>Asynchronously reads the entire contents of a file. Example:
 
580
 
 
581
</p>
 
582
<pre><code>fs.readFile(&#39;/etc/passwd&#39;, function (err, data) {
 
583
  if (err) throw err;
 
584
  console.log(data);
 
585
});</code></pre>
 
586
<p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the
 
587
contents of the file.
 
588
 
 
589
</p>
 
590
<p>If no encoding is specified, then the raw buffer is returned.
 
591
 
 
592
 
 
593
</p>
 
594
<h2>fs.readFileSync(filename, [options])<span><a class="mark" href="#fs_fs_readfilesync_filename_options" id="fs_fs_readfilesync_filename_options">#</a></span></h2>
 
595
<p>Synchronous version of <code>fs.readFile</code>. Returns the contents of the <code>filename</code>.
 
596
 
 
597
</p>
 
598
<p>If the <code>encoding</code> option is specified then this function returns a
 
599
string. Otherwise it returns a buffer.
 
600
 
 
601
 
 
602
</p>
 
603
<h2>fs.writeFile(filename, data, [options], callback)<span><a class="mark" href="#fs_fs_writefile_filename_data_options_callback" id="fs_fs_writefile_filename_data_options_callback">#</a></span></h2>
 
604
<div class="signature"><ul>
 
605
<li><code>filename</code> <span class="type">String</span></li>
 
606
<li><code>data</code> <span class="type">String | Buffer</span></li>
 
607
<li><code>options</code> <span class="type">Object</span><ul>
 
608
<li><code>encoding</code> <span class="type">String | Null</span> default = <code>&#39;utf8&#39;</code></li>
 
609
<li><code>mode</code> <span class="type">Number</span> default = <code>438</code> (aka <code>0666</code> in Octal)</li>
 
610
<li><code>flag</code> <span class="type">String</span> default = <code>&#39;w&#39;</code></li>
 
611
</ul>
 
612
</li>
 
613
<li><code>callback</code> <span class="type">Function</span></li>
 
614
</div></ul>
 
615
<p>Asynchronously writes data to a file, replacing the file if it already exists.
 
616
<code>data</code> can be a string or a buffer.
 
617
 
 
618
</p>
 
619
<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer. It defaults
 
620
to <code>&#39;utf8&#39;</code>.
 
621
 
 
622
</p>
 
623
<p>Example:
 
624
 
 
625
</p>
 
626
<pre><code>fs.writeFile(&#39;message.txt&#39;, &#39;Hello Node&#39;, function (err) {
 
627
  if (err) throw err;
 
628
  console.log(&#39;It\&#39;s saved!&#39;);
 
629
});</code></pre>
 
630
<h2>fs.writeFileSync(filename, data, [options])<span><a class="mark" href="#fs_fs_writefilesync_filename_data_options" id="fs_fs_writefilesync_filename_data_options">#</a></span></h2>
 
631
<p>The synchronous version of <code>fs.writeFile</code>.
 
632
 
 
633
</p>
 
634
<h2>fs.appendFile(filename, data, [options], callback)<span><a class="mark" href="#fs_fs_appendfile_filename_data_options_callback" id="fs_fs_appendfile_filename_data_options_callback">#</a></span></h2>
 
635
<div class="signature"><ul>
 
636
<li><code>filename</code> <span class="type">String</span></li>
 
637
<li><code>data</code> <span class="type">String | Buffer</span></li>
 
638
<li><code>options</code> <span class="type">Object</span><ul>
 
639
<li><code>encoding</code> <span class="type">String | Null</span> default = <code>&#39;utf8&#39;</code></li>
 
640
<li><code>mode</code> <span class="type">Number</span> default = <code>438</code> (aka <code>0666</code> in Octal)</li>
 
641
<li><code>flag</code> <span class="type">String</span> default = <code>&#39;a&#39;</code></li>
 
642
</ul>
 
643
</li>
 
644
<li><code>callback</code> <span class="type">Function</span></li>
 
645
</div></ul>
 
646
<p>Asynchronously append data to a file, creating the file if it not yet exists.
 
647
<code>data</code> can be a string or a buffer.
 
648
 
 
649
</p>
 
650
<p>Example:
 
651
 
 
652
</p>
 
653
<pre><code>fs.appendFile(&#39;message.txt&#39;, &#39;data to append&#39;, function (err) {
 
654
  if (err) throw err;
 
655
  console.log(&#39;The &quot;data to append&quot; was appended to file!&#39;);
 
656
});</code></pre>
 
657
<h2>fs.appendFileSync(filename, data, [options])<span><a class="mark" href="#fs_fs_appendfilesync_filename_data_options" id="fs_fs_appendfilesync_filename_data_options">#</a></span></h2>
 
658
<p>The synchronous version of <code>fs.appendFile</code>.
 
659
 
 
660
</p>
 
661
<h2>fs.watchFile(filename, [options], listener)<span><a class="mark" href="#fs_fs_watchfile_filename_options_listener" id="fs_fs_watchfile_filename_options_listener">#</a></span></h2>
 
662
<pre class="api_stability_2">Stability: 2 - Unstable.  Use fs.watch instead, if possible.</pre><p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each
 
663
time the file is accessed.
 
664
 
 
665
</p>
 
666
<p>The second argument is optional. The <code>options</code> if provided should be an object
 
667
containing two members a boolean, <code>persistent</code>, and <code>interval</code>. <code>persistent</code>
 
668
indicates whether the process should continue to run as long as files are
 
669
being watched. <code>interval</code> indicates how often the target should be polled,
 
670
in milliseconds. The default is <code>{ persistent: true, interval: 5007 }</code>.
 
671
 
 
672
</p>
 
673
<p>The <code>listener</code> gets two arguments the current stat object and the previous
 
674
stat object:
 
675
 
 
676
</p>
 
677
<pre><code>fs.watchFile(&#39;message.text&#39;, function (curr, prev) {
 
678
  console.log(&#39;the current mtime is: &#39; + curr.mtime);
 
679
  console.log(&#39;the previous mtime was: &#39; + prev.mtime);
 
680
});</code></pre>
 
681
<p>These stat objects are instances of <code>fs.Stat</code>.
 
682
 
 
683
</p>
 
684
<p>If you want to be notified when the file was modified, not just accessed
 
685
you need to compare <code>curr.mtime</code> and <code>prev.mtime</code>.
 
686
 
 
687
</p>
 
688
<h2>fs.unwatchFile(filename, [listener])<span><a class="mark" href="#fs_fs_unwatchfile_filename_listener" id="fs_fs_unwatchfile_filename_listener">#</a></span></h2>
 
689
<pre class="api_stability_2">Stability: 2 - Unstable.  Use fs.watch instead, if possible.</pre><p>Stop watching for changes on <code>filename</code>. If <code>listener</code> is specified, only that
 
690
particular listener is removed. Otherwise, <em>all</em> listeners are removed and you
 
691
have effectively stopped watching <code>filename</code>.
 
692
 
 
693
</p>
 
694
<p>Calling <code>fs.unwatchFile()</code> with a filename that is not being watched is a
 
695
no-op, not an error.
 
696
 
 
697
</p>
 
698
<h2>fs.watch(filename, [options], [listener])<span><a class="mark" href="#fs_fs_watch_filename_options_listener" id="fs_fs_watch_filename_options_listener">#</a></span></h2>
 
699
<pre class="api_stability_2">Stability: 2 - Unstable.</pre><p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a
 
700
directory.  The returned object is a <a href="#fs_class_fs_fswatcher">fs.FSWatcher</a>.
 
701
 
 
702
</p>
 
703
<p>The second argument is optional. The <code>options</code> if provided should be an object
 
704
containing a boolean member <code>persistent</code>, which indicates whether the process
 
705
should continue to run as long as files are being watched. The default is
 
706
<code>{ persistent: true }</code>.
 
707
 
 
708
</p>
 
709
<p>The listener callback gets two arguments <code>(event, filename)</code>.  <code>event</code> is either
 
710
&#39;rename&#39; or &#39;change&#39;, and <code>filename</code> is the name of the file which triggered
 
711
the event.
 
712
 
 
713
</p>
 
714
<h3>Caveats<span><a class="mark" href="#fs_caveats" id="fs_caveats">#</a></span></h3>
 
715
<!--type=misc-->
 
716
 
 
717
<p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is
 
718
unavailable in some situations.
 
719
 
 
720
</p>
 
721
<h4>Availability<span><a class="mark" href="#fs_availability" id="fs_availability">#</a></span></h4>
 
722
<!--type=misc-->
 
723
 
 
724
<p>This feature depends on the underlying operating system providing a way
 
725
to be notified of filesystem changes.
 
726
 
 
727
</p>
 
728
<ul>
 
729
<li>On Linux systems, this uses <code>inotify</code>.</li>
 
730
<li>On BSD systems (including OS X), this uses <code>kqueue</code>.</li>
 
731
<li>On SunOS systems (including Solaris and SmartOS), this uses <code>event ports</code>.</li>
 
732
<li>On Windows systems, this feature depends on <code>ReadDirectoryChangesW</code>.</li>
 
733
</ul>
 
734
<p>If the underlying functionality is not available for some reason, then
 
735
<code>fs.watch</code> will not be able to function.  For example, watching files or
 
736
directories on network file systems (NFS, SMB, etc.) often doesn&#39;t work
 
737
reliably or at all.
 
738
 
 
739
</p>
 
740
<p>You can still use <code>fs.watchFile</code>, which uses stat polling, but it is slower and
 
741
less reliable.
 
742
 
 
743
</p>
 
744
<h4>Filename Argument<span><a class="mark" href="#fs_filename_argument" id="fs_filename_argument">#</a></span></h4>
 
745
<!--type=misc-->
 
746
 
 
747
<p>Providing <code>filename</code> argument in the callback is not supported
 
748
on every platform (currently it&#39;s only supported on Linux and Windows).  Even
 
749
on supported platforms <code>filename</code> is not always guaranteed to be provided.
 
750
Therefore, don&#39;t assume that <code>filename</code> argument is always provided in the
 
751
callback, and have some fallback logic if it is null.
 
752
 
 
753
</p>
 
754
<pre><code>fs.watch(&#39;somedir&#39;, function (event, filename) {
 
755
  console.log(&#39;event is: &#39; + event);
 
756
  if (filename) {
 
757
    console.log(&#39;filename provided: &#39; + filename);
 
758
  } else {
 
759
    console.log(&#39;filename not provided&#39;);
 
760
  }
 
761
});</code></pre>
 
762
<h2>fs.exists(path, callback)<span><a class="mark" href="#fs_fs_exists_path_callback" id="fs_fs_exists_path_callback">#</a></span></h2>
 
763
<p>Test whether or not the given path exists by checking with the file system.
 
764
Then call the <code>callback</code> argument with either true or false.  Example:
 
765
 
 
766
</p>
 
767
<pre><code>fs.exists(&#39;/etc/passwd&#39;, function (exists) {
 
768
  util.debug(exists ? &quot;it&#39;s there&quot; : &quot;no passwd!&quot;);
 
769
});</code></pre>
 
770
<h2>fs.existsSync(path)<span><a class="mark" href="#fs_fs_existssync_path" id="fs_fs_existssync_path">#</a></span></h2>
 
771
<p>Synchronous version of <code>fs.exists</code>.
 
772
 
 
773
</p>
 
774
<h2>Class: fs.Stats<span><a class="mark" href="#fs_class_fs_stats" id="fs_class_fs_stats">#</a></span></h2>
 
775
<p>Objects returned from <code>fs.stat()</code>, <code>fs.lstat()</code> and <code>fs.fstat()</code> and their
 
776
synchronous counterparts are of this type.
 
777
 
 
778
</p>
 
779
<ul>
 
780
<li><code>stats.isFile()</code></li>
 
781
<li><code>stats.isDirectory()</code></li>
 
782
<li><code>stats.isBlockDevice()</code></li>
 
783
<li><code>stats.isCharacterDevice()</code></li>
 
784
<li><code>stats.isSymbolicLink()</code> (only valid with  <code>fs.lstat()</code>)</li>
 
785
<li><code>stats.isFIFO()</code></li>
 
786
<li><code>stats.isSocket()</code></li>
 
787
</ul>
 
788
<p>For a regular file <code>util.inspect(stats)</code> would return a string very
 
789
similar to this:
 
790
 
 
791
</p>
 
792
<pre><code>{ dev: 2114,
 
793
  ino: 48064969,
 
794
  mode: 33188,
 
795
  nlink: 1,
 
796
  uid: 85,
 
797
  gid: 100,
 
798
  rdev: 0,
 
799
  size: 527,
 
800
  blksize: 4096,
 
801
  blocks: 8,
 
802
  atime: Mon, 10 Oct 2011 23:24:11 GMT,
 
803
  mtime: Mon, 10 Oct 2011 23:24:11 GMT,
 
804
  ctime: Mon, 10 Oct 2011 23:24:11 GMT }</code></pre>
 
805
<p>Please note that <code>atime</code>, <code>mtime</code> and <code>ctime</code> are instances
 
806
of <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date">Date</a> object and to compare the values of
 
807
these objects you should use appropriate methods. For most
 
808
general uses <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime">getTime()</a> will return
 
809
the number of milliseconds elapsed since <em>1 January 1970
 
810
00:00:00 UTC</em> and this integer should be sufficient for
 
811
any comparison, however there additional methods which can
 
812
be used for displaying fuzzy information. More details can
 
813
be found in the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date">MDN JavaScript Reference</a> page.
 
814
 
 
815
</p>
 
816
<h2>fs.createReadStream(path, [options])<span><a class="mark" href="#fs_fs_createreadstream_path_options" id="fs_fs_createreadstream_path_options">#</a></span></h2>
 
817
<p>Returns a new ReadStream object (See <code>Readable Stream</code>).
 
818
 
 
819
</p>
 
820
<p><code>options</code> is an object with the following defaults:
 
821
 
 
822
</p>
 
823
<pre><code>{ flags: &#39;r&#39;,
 
824
  encoding: null,
 
825
  fd: null,
 
826
  mode: 0666,
 
827
  autoClose: true
 
828
}</code></pre>
 
829
<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from
 
830
the file instead of the entire file.  Both <code>start</code> and <code>end</code> are inclusive and
 
831
start at 0. The <code>encoding</code> can be <code>&#39;utf8&#39;</code>, <code>&#39;ascii&#39;</code>, or <code>&#39;base64&#39;</code>.
 
832
 
 
833
</p>
 
834
<p>If <code>autoClose</code> is false, then the file descriptor won&#39;t be closed, even if
 
835
there&#39;s an error.  It is your responsiblity to close it and make sure
 
836
there&#39;s no file descriptor leak.  If <code>autoClose</code> is set to true (default
 
837
behavior), on <code>error</code> or <code>end</code> the file descriptor will be closed
 
838
automatically.
 
839
 
 
840
</p>
 
841
<p>An example to read the last 10 bytes of a file which is 100 bytes long:
 
842
 
 
843
</p>
 
844
<pre><code>fs.createReadStream(&#39;sample.txt&#39;, {start: 90, end: 99});</code></pre>
 
845
<h2>Class: fs.ReadStream<span><a class="mark" href="#fs_class_fs_readstream" id="fs_class_fs_readstream">#</a></span></h2>
 
846
<p><code>ReadStream</code> is a <a href="stream.html#stream_class_stream_readable">Readable Stream</a>.
 
847
 
 
848
</p>
 
849
<h3>Event: &#39;open&#39;<span><a class="mark" href="#fs_event_open" id="fs_event_open">#</a></span></h3>
 
850
<div class="signature"><ul>
 
851
<li><code>fd</code> <span class="type">Integer</span> file descriptor used by the ReadStream.</li>
 
852
</div></ul>
 
853
<p>Emitted when the ReadStream&#39;s file is opened.
 
854
 
 
855
 
 
856
</p>
 
857
<h2>fs.createWriteStream(path, [options])<span><a class="mark" href="#fs_fs_createwritestream_path_options" id="fs_fs_createwritestream_path_options">#</a></span></h2>
 
858
<p>Returns a new WriteStream object (See <code>Writable Stream</code>).
 
859
 
 
860
</p>
 
861
<p><code>options</code> is an object with the following defaults:
 
862
 
 
863
</p>
 
864
<pre><code>{ flags: &#39;w&#39;,
 
865
  encoding: null,
 
866
  mode: 0666 }</code></pre>
 
867
<p><code>options</code> may also include a <code>start</code> option to allow writing data at
 
868
some position past the beginning of the file.  Modifying a file rather
 
869
than replacing it may require a <code>flags</code> mode of <code>r+</code> rather than the
 
870
default mode <code>w</code>.
 
871
 
 
872
</p>
 
873
<h2>Class: fs.WriteStream<span><a class="mark" href="#fs_class_fs_writestream" id="fs_class_fs_writestream">#</a></span></h2>
 
874
<p><code>WriteStream</code> is a <a href="stream.html#stream_class_stream_writable">Writable Stream</a>.
 
875
 
 
876
</p>
 
877
<h3>Event: &#39;open&#39;<span><a class="mark" href="#fs_event_open_1" id="fs_event_open_1">#</a></span></h3>
 
878
<div class="signature"><ul>
 
879
<li><code>fd</code> <span class="type">Integer</span> file descriptor used by the WriteStream.</li>
 
880
</div></ul>
 
881
<p>Emitted when the WriteStream&#39;s file is opened.
 
882
 
 
883
</p>
 
884
<h3>file.bytesWritten<span><a class="mark" href="#fs_file_byteswritten" id="fs_file_byteswritten">#</a></span></h3>
 
885
<p>The number of bytes written so far. Does not include data that is still queued
 
886
for writing.
 
887
 
 
888
</p>
 
889
<h2>Class: fs.FSWatcher<span><a class="mark" href="#fs_class_fs_fswatcher" id="fs_class_fs_fswatcher">#</a></span></h2>
 
890
<p>Objects returned from <code>fs.watch()</code> are of this type.
 
891
 
 
892
</p>
 
893
<h3>watcher.close()<span><a class="mark" href="#fs_watcher_close" id="fs_watcher_close">#</a></span></h3>
 
894
<p>Stop watching for changes on the given <code>fs.FSWatcher</code>.
 
895
 
 
896
</p>
 
897
<h3>Event: &#39;change&#39;<span><a class="mark" href="#fs_event_change" id="fs_event_change">#</a></span></h3>
 
898
<div class="signature"><ul>
 
899
<li><code>event</code> <span class="type">String</span> The type of fs change</li>
 
900
<li><code>filename</code> <span class="type">String</span> The filename that changed (if relevant/available)</li>
 
901
</div></ul>
 
902
<p>Emitted when something changes in a watched directory or file.
 
903
See more details in <a href="#fs_fs_watch_filename_options_listener">fs.watch</a>.
 
904
 
 
905
</p>
 
906
<h3>Event: &#39;error&#39;<span><a class="mark" href="#fs_event_error" id="fs_event_error">#</a></span></h3>
 
907
<div class="signature"><ul>
 
908
<li><code>error</code> <span class="type">Error object</span></li>
 
909
</div></ul>
 
910
<p>Emitted when an error occurs.
 
911
</p>
 
912
 
 
913
          </div>
 
914
        </div>
 
915
    </div>
 
916
    <div id="footer">
 
917
        <a href="http://joyent.com" class="joyent-logo">Joyent</a>
 
918
        <ul class="clearfix">
 
919
            <li><a href="/">Node.js</a></li>
 
920
            <li><a href="/download/">Download</a></li>
 
921
            <li><a href="/about/">About</a></li>
 
922
            <li><a href="http://npmjs.org/">npm Registry</a></li>
 
923
            <li><a href="http://nodejs.org/api/">Docs</a></li>
 
924
            <li><a href="http://blog.nodejs.org">Blog</a></li>
 
925
            <li><a href="/community/">Community</a></li>
 
926
            <li><a href="/logos/">Logos</a></li>
 
927
            <li><a href="http://jobs.nodejs.org/">Jobs</a></li>
 
928
            <li><a href="http://twitter.com/nodejs" class="twitter">@nodejs</a></li>
 
929
        </ul>
 
930
 
 
931
        <p>Copyright <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="/trademark-policy.pdf">trademark</a> of Joyent, Inc. View <a href="https://raw.github.com/joyent/node/v0.10.15/LICENSE">license</a>.</p>
 
932
    </div>
 
933
 
 
934
  <script src="../sh_main.js"></script>
 
935
  <script src="../sh_javascript.min.js"></script>
 
936
  <script>highlight(undefined, undefined, 'pre');</script>
 
937
  <script>
 
938
    window._gaq = [['_setAccount', 'UA-10874194-2'], ['_trackPageview']];
 
939
    (function(d, t) {
 
940
      var g = d.createElement(t),
 
941
          s = d.getElementsByTagName(t)[0];
 
942
      g.src = '//www.google-analytics.com/ga.js';
 
943
      s.parentNode.insertBefore(g, s);
 
944
    }(document, 'script'));
 
945
  </script>
 
946
</body>
 
947
</html>
 
948