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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# bare-fs

Native file system operations for Bare. The API closely follows that of the Node.js `fs` module.

```
npm i bare-fs
```

## Usage

```js
const fs = require('bare-fs')

const fd = await fs.open('hello.txt')

const buffer = Buffer.alloc(1024)

try {
  const length = await fs.read(fd, buffer)

  console.log('Read', length, 'bytes')
} finally {
  await fs.close(fd)
}
```

## API

#### `const fd = await fs.open(filepath[, flags[, mode]])`

Open a file, returning a file descriptor. `flags` defaults to `'r'` and `mode` defaults to `0o666`. `flags` may be a string such as `'r'`, `'w'`, `'a'`, `'r+'`, etc., or a numeric combination of `fs.constants` flags.

#### `fs.open(filepath[, flags[, mode]], callback)`

Callback version of `fs.open()`.

#### `const fd = fs.openSync(filepath[, flags[, mode]])`

Synchronous version of `fs.open()`.

#### `await fs.close(fd)`

Close a file descriptor.

#### `fs.close(fd, callback)`

Callback version of `fs.close()`.

#### `fs.closeSync(fd)`

Synchronous version of `fs.close()`.

#### `await fs.access(filepath[, mode])`

Check whether the file at `filepath` is accessible. `mode` defaults to `fs.constants.F_OK`.

#### `fs.access(filepath[, mode], callback)`

Callback version of `fs.access()`.

#### `fs.accessSync(filepath[, mode])`

Synchronous version of `fs.access()`.

#### `const exists = await fs.exists(filepath)`

Check whether a file exists at `filepath`. Returns `true` if the file is accessible, `false` otherwise.

#### `fs.exists(filepath, callback)`

Callback version of `fs.exists()`.

#### `const exists = fs.existsSync(filepath)`

Synchronous version of `fs.exists()`.

#### `const bytesRead = await fs.read(fd, buffer[, offset[, len[, pos]]])`

Read from a file descriptor into `buffer`. `offset` defaults to `0`, `len` defaults to `buffer.byteLength - offset`, and `pos` defaults to `-1` (current position). Returns the number of bytes read.

#### `fs.read(fd, buffer[, offset[, len[, pos]]], callback)`

Callback version of `fs.read()`.

#### `const bytesRead = fs.readSync(fd, buffer[, offset[, len[, pos]]])`

Synchronous version of `fs.read()`.

#### `const bytesRead = await fs.readv(fd, buffers[, pos])`

Read from a file descriptor into an array of `buffers`. `pos` defaults to `-1`.

#### `fs.readv(fd, buffers[, pos], callback)`

Callback version of `fs.readv()`.

#### `const bytesRead = fs.readvSync(fd, buffers[, pos])`

Synchronous version of `fs.readv()`.

#### `const bytesWritten = await fs.write(fd, data[, offset[, len[, pos]]])`

Write `data` to a file descriptor. When `data` is a string, the signature is `fs.write(fd, data[, pos[, encoding]])` where `encoding` defaults to `'utf8'`. Returns the number of bytes written.

#### `fs.write(fd, data[, offset[, len[, pos]]], callback)`

Callback version of `fs.write()`.

#### `const bytesWritten = fs.writeSync(fd, data[, offset[, len[, pos]]])`

Synchronous version of `fs.write()`.

#### `const bytesWritten = await fs.writev(fd, buffers[, pos])`

Write an array of `buffers` to a file descriptor. `pos` defaults to `-1`.

#### `fs.writev(fd, buffers[, pos], callback)`

Callback version of `fs.writev()`.

#### `const bytesWritten = fs.writevSync(fd, buffers[, pos])`

Synchronous version of `fs.writev()`.

#### `const stats = await fs.stat(filepath)`

Get the status of a file. Returns a `Stats` object.

#### `fs.stat(filepath, callback)`

Callback version of `fs.stat()`.

#### `const stats = fs.statSync(filepath)`

Synchronous version of `fs.stat()`.

#### `const stats = await fs.lstat(filepath)`

Like `fs.stat()`, but if `filepath` is a symbolic link, the link itself is statted, not the file it refers to.

#### `fs.lstat(filepath, callback)`

Callback version of `fs.lstat()`.

#### `const stats = fs.lstatSync(filepath)`

Synchronous version of `fs.lstat()`.

#### `const stats = await fs.fstat(fd)`

Get the status of a file by its file descriptor. Returns a `Stats` object.

#### `fs.fstat(fd, callback)`

Callback version of `fs.fstat()`.

#### `const stats = fs.fstatSync(fd)`

Synchronous version of `fs.fstat()`.

#### `const stats = await fs.statfs(filepath)`

Get filesystem statistics. Returns a `StatFs` object.

#### `fs.statfs(filepath, callback)`

Callback version of `fs.statfs()`.

#### `const stats = fs.statfsSync(filepath)`

Synchronous version of `fs.statfs()`.

#### `await fs.ftruncate(fd[, len])`

Truncate a file to `len` bytes. `len` defaults to `0`.

#### `fs.ftruncate(fd[, len], callback)`

Callback version of `fs.ftruncate()`.

#### `fs.ftruncateSync(fd[, len])`

Synchronous version of `fs.ftruncate()`.

#### `await fs.chmod(filepath, mode)`

Change the permissions of a file. `mode` may be a numeric mode or a string that will be parsed as octal.

#### `fs.chmod(filepath, mode, callback)`

Callback version of `fs.chmod()`.

#### `fs.chmodSync(filepath, mode)`

Synchronous version of `fs.chmod()`.

#### `await fs.fchmod(fd, mode)`

Change the permissions of a file by its file descriptor.

#### `fs.fchmod(fd, mode, callback)`

Callback version of `fs.fchmod()`.

#### `fs.fchmodSync(fd, mode)`

Synchronous version of `fs.fchmod()`.

#### `await fs.chown(filepath, uid, gid)`

Change the owner and group of a file.

**NOTE**: The `chown` functions are not implemented on Windows.

#### `fs.chown(filepath,  uid, gid, callback)`

Callback version of `fs.chown()`.

#### `await fs.chownSync(filepath, uid, gid)`

Synchronous version of `fs.chown()`.

#### `await fs.lchown(filepath, uid, gid)`

Change the owner and group of a file, but if `filepath` is a symbolic link, the changes are applied only to the link, not the file it refers to.

#### `fs.lchown(filepath,  uid, gid, callback)`

Callback version of `fs.lchown()`.

#### `fs.lchownSync(filepath, uid, gid)`

Synchronous version of `fs.lchown()`.

#### `await fs.fchown(filepath, uid, gid)`

Change the owner and group of a file by its file descriptor.

#### `fs.fchown(filepath,  uid, gid, callback)`

Callback version of `fs.fchown()`.

#### `fs.fchownSync(filepath, uid, gid)`

Synchronous version of `fs.fchown()`.

#### `await fs.utimes(filepath, atime, mtime)`

Change the access and modification times of a file. Times may be numbers (seconds since epoch) or `Date` objects.

#### `fs.utimes(filepath, atime, mtime, callback)`

Callback version of `fs.utimes()`.

#### `fs.utimesSync(filepath, atime, mtime)`

Synchronous version of `fs.utimes()`.

#### `await fs.lutimes(filepath, atime, mtime)`

Like `fs.utimes()`, but if `filepath` is a symbolic link, the timestamps of the link is changed, not the file it refers to.

#### `fs.lutimes(filepath, atime, mtime, callback)`

Callback version of `fs.lutimes()`.

#### `fs.lutimesSync(filepath, atime, mtime)`

Synchronous version of `fs.lutimes()`.

#### `await fs.futimes(fd, atime, mtime)`

Change the access and modification times of a file by its file descriptor. Times may be numbers (seconds since epoch) or `Date` objects.

#### `fs.futimes(fd, atime, mtime, callback)`

Callback version of `fs.futimes()`.

#### `fs.futimesSync(fd, atime, mtime)`

Synchronous version of `fs.futimes()`.

#### `await fs.link(src, dst)`

Creates a new link (also known as a hard link) to an existing file.

#### `fs.link(src, dst, callback)`

Callback version of `fs.link()`.

#### `fs.linkSync(src, dst)`

Synchronous version of `fs.link()`.

#### `await fs.mkdir(filepath[, opts])`

Create a directory at `filepath`.

Options include:

```js
options = {
  mode: 0o777,
  recursive: false
}
```

If `opts` is a number, it is treated as the `mode`. When `recursive` is `true`, parent directories are created as needed.

#### `fs.mkdir(filepath[, opts], callback)`

Callback version of `fs.mkdir()`.

#### `fs.mkdirSync(filepath[, opts])`

Synchronous version of `fs.mkdir()`.

#### `const path = await fs.mkdtemp(prefix)`

Create a unique temporary directory.

#### `fs.mkdtemp(prefix, callback)`

Callback version of `fs.mkdtemp()`.

#### `const path = fs.mkdtempSync(prefix)`

Synchronous version of `fs.mkdtemp()`.

#### `await fs.rmdir(filepath)`

Remove an empty directory.

#### `fs.rmdir(filepath, callback)`

Callback version of `fs.rmdir()`.

#### `fs.rmdirSync(filepath)`

Synchronous version of `fs.rmdir()`.

#### `await fs.rm(filepath[, opts])`

Remove a file or directory at `filepath`.

Options include:

```js
options = {
  force: false,
  recursive: false
}
```

When `recursive` is `true`, directories are removed along with their contents. When `force` is `true`, no error is thrown if `filepath` does not exist.

#### `fs.rm(filepath[, opts], callback)`

Callback version of `fs.rm()`.

#### `fs.rmSync(filepath[, opts])`

Synchronous version of `fs.rm()`.

#### `await fs.unlink(filepath)`

Remove a file.

#### `fs.unlink(filepath, callback)`

Callback version of `fs.unlink()`.

#### `fs.unlinkSync(filepath)`

Synchronous version of `fs.unlink()`.

#### `await fs.rename(src, dst)`

Rename a file from `src` to `dst`.

#### `fs.rename(src, dst, callback)`

Callback version of `fs.rename()`.

#### `fs.renameSync(src, dst)`

Synchronous version of `fs.rename()`.

#### `await fs.copyFile(src, dst[, mode])`

Copy a file from `src` to `dst`. `mode` is an optional bitmask created from `fs.constants.COPYFILE_EXCL`, `fs.constants.COPYFILE_FICLONE`, or `fs.constants.COPYFILE_FICLONE_FORCE`.

#### `fs.copyFile(src, dst[, mode], callback)`

Callback version of `fs.copyFile()`.

#### `fs.copyFileSync(src, dst[, mode])`

Synchronous version of `fs.copyFile()`.

#### `await fs.cp(src, dst[, opts])`

Copy a file or directory from `src` to `dst`.

Options include:

```js
options = {
  recursive: false
}
```

Set `recursive` to `true` to copy directories and their contents. Files are copied preserving their permissions.

#### `fs.cp(src, dst[, opts], callback)`

Callback version of `fs.cp()`.

#### `fs.cpSync(src, dst[, opts])`

Synchronous version of `fs.cp()`.

#### `const resolved = await fs.realpath(filepath[, opts])`

Resolve the real path of `filepath`, expanding all symbolic links.

Options include:

```js
options = {
  encoding: 'utf8'
}
```

Set `encoding` to `'buffer'` to receive the result as a `Buffer`.

#### `fs.realpath(filepath[, opts], callback)`

Callback version of `fs.realpath()`.

#### `const resolved = fs.realpathSync(filepath[, opts])`

Synchronous version of `fs.realpath()`.

#### `const target = await fs.readlink(filepath[, opts])`

Read the target of a symbolic link.

Options include:

```js
options = {
  encoding: 'utf8'
}
```

#### `fs.readlink(filepath[, opts], callback)`

Callback version of `fs.readlink()`.

#### `const target = fs.readlinkSync(filepath[, opts])`

Synchronous version of `fs.readlink()`.

#### `await fs.truncate(filename[, len])`

Truncate the file at `filename` to `len` bytes. `len` defaults to `0`.

#### `fs.truncate(filename[, len], callback)`

Callback version of `fs.truncate()`.

#### `fs.truncateSync(filename[, len])`

Synchronous version of `fs.truncate()`.

#### `await fs.symlink(target, filepath[, type])`

Create a symbolic link at `filepath` pointing to `target`. `type` may be `'file'`, `'dir'`, or `'junction'` (Windows only) or a numeric flag. On Windows, if `type` is not provided, it is inferred from the target.

#### `fs.symlink(target, filepath[, type], callback)`

Callback version of `fs.symlink()`.

#### `fs.symlinkSync(target, filepath[, type])`

Synchronous version of `fs.symlink()`.

#### `await fs.fsync(fd)`

Flush all modified in-core data of the file referred by its file descriptor to the disk device.

#### `fs.fsync(fs, callback)`

Callback version of `fs.fsync()`.

#### `fs.fsyncSync(fd)`

Synchronous version of `fs.fsync()`.

#### `await fs.fdatasync(fd)`

Similar to `fsync`, but does not flush modified metadata unless necessary.

#### `fs.fdatasync(fs, callback)`

Callback version of `fs.fdatasync()`.

#### `fs.fdatasyncSync(fd)`

Synchronous version of `fs.fdatasync()`.

#### `const dir = await fs.opendir(filepath[, opts])`

Open a directory for iteration. Returns a `Dir` object.

Options include:

```js
options = {
  encoding: 'utf8',
  bufferSize: 32
}
```

#### `fs.opendir(filepath[, opts], callback)`

Callback version of `fs.opendir()`.

#### `const dir = fs.opendirSync(filepath[, opts])`

Synchronous version of `fs.opendir()`.

#### `const entries = await fs.readdir(filepath[, opts])`

Read the contents of a directory. Returns an array of filenames or, if `withFileTypes` is `true`, an array of `Dirent` objects.

Options include:

```js
options = {
  encoding: 'utf8',
  withFileTypes: false,
  recursive: false
}
```

#### `fs.readdir(filepath[, opts], callback)`

Callback version of `fs.readdir()`.

#### `const entries = fs.readdirSync(filepath[, opts])`

Synchronous version of `fs.readdir()`.

#### `const data = await fs.readFile(filepath[, opts])`

Read the entire contents of a file. Returns a `Buffer` by default, or a string if an `encoding` is specified.

Options include:

```js
options = {
  encoding: 'buffer',
  flag: 'r'
}
```

#### `fs.readFile(filepath[, opts], callback)`

Callback version of `fs.readFile()`.

#### `const data = fs.readFileSync(filepath[, opts])`

Synchronous version of `fs.readFile()`.

#### `await fs.writeFile(filepath, data[, opts])`

Write `data` to a file, replacing it if it already exists.

Options include:

```js
options = {
  encoding: 'utf8',
  flag: 'w',
  mode: 0o666
}
```

#### `fs.writeFile(filepath, data[, opts], callback)`

Callback version of `fs.writeFile()`.

#### `fs.writeFileSync(filepath, data[, opts])`

Synchronous version of `fs.writeFile()`.

#### `await fs.appendFile(filepath, data[, opts])`

Append `data` to a file, creating it if it does not exist. Accepts the same options as `fs.writeFile()` but defaults to the `'a'` flag.

#### `fs.appendFile(filepath, data[, opts], callback)`

Callback version of `fs.appendFile()`.

#### `fs.appendFileSync(filepath, data[, opts])`

Synchronous version of `fs.appendFile()`.

#### `const watcher = fs.watch(filepath[, opts], callback)`

Watch a file or directory for changes. Returns a `Watcher` object. The `callback`, if provided, is called with `(eventType, filename)` on each change.

Options include:

```js
options = {
  persistent: true,
  recursive: false,
  encoding: 'utf8'
}
```

#### `const stream = fs.createReadStream(path[, opts])`

Create a readable stream for a file. Returns a `ReadStream`.

Options include:

```js
options = {
  fd: -1,
  flags: 'r',
  mode: 0o666,
  start: 0,
  end: Infinity
}
```

If `fd` is provided, `path` may be `null` and the stream reads from the given file descriptor.

#### `const stream = fs.createWriteStream(path[, opts])`

Create a writable stream for a file. Returns a `WriteStream`.

Options include:

```js
options = {
  fd: -1,
  flags: 'w',
  mode: 0o666
}
```

If `fd` is provided, `path` may be `null` and the stream writes to the given file descriptor.

#### `fs.constants`

An object containing file system constants. See `fs/constants` for the full list. Commonly used constants include:

- `fs.constants.O_RDONLY`, `fs.constants.O_WRONLY`, `fs.constants.O_RDWR` β€” file access flags
- `fs.constants.O_CREAT`, `fs.constants.O_TRUNC`, `fs.constants.O_APPEND` β€” file creation flags
- `fs.constants.F_OK`, `fs.constants.R_OK`, `fs.constants.W_OK`, `fs.constants.X_OK` β€” file accessibility flags
- `fs.constants.S_IFMT`, `fs.constants.S_IFREG`, `fs.constants.S_IFDIR`, `fs.constants.S_IFLNK` β€” file type flags
- `fs.constants.COPYFILE_EXCL`, `fs.constants.COPYFILE_FICLONE`, `fs.constants.COPYFILE_FICLONE_FORCE` β€” copy flags

### `Stats`

Returned by `fs.stat()`, `fs.lstat()`, and `fs.fstat()`.

#### `stats.dev`

The device identifier.

#### `stats.mode`

The file mode (type and permissions).

#### `stats.nlink`

The number of hard links.

#### `stats.uid`

The user identifier of the file owner.

#### `stats.gid`

The group identifier of the file owner.

#### `stats.rdev`

The device identifier for special files.

#### `stats.blksize`

The file system block size for I/O operations.

#### `stats.ino`

The inode number.

#### `stats.size`

The size of the file in bytes.

#### `stats.blocks`

The number of 512-byte blocks allocated.

#### `stats.atimeMs`

The access time in milliseconds since the epoch.

#### `stats.mtimeMs`

The modification time in milliseconds since the epoch.

#### `stats.ctimeMs`

The change time in milliseconds since the epoch.

#### `stats.birthtimeMs`

The creation time in milliseconds since the epoch.

#### `stats.atime`

The access time as a `Date` object.

#### `stats.mtime`

The modification time as a `Date` object.

#### `stats.ctime`

The change time as a `Date` object.

#### `stats.birthtime`

The creation time as a `Date` object.

#### `stats.isDirectory()`

Returns `true` if the file is a directory.

#### `stats.isFile()`

Returns `true` if the file is a regular file.

#### `stats.isBlockDevice()`

Returns `true` if the file is a block device.

#### `stats.isCharacterDevice()`

Returns `true` if the file is a character device.

#### `stats.isFIFO()`

Returns `true` if the file is a FIFO (named pipe).

#### `stats.isSymbolicLink()`

Returns `true` if the file is a symbolic link. Only meaningful when using `fs.lstat()`.

#### `stats.isSocket()`

Returns `true` if the file is a socket.

### `Dir`

Returned by `fs.opendir()`. Supports both synchronous and asynchronous iteration.

#### `dir.path`

The path of the directory.

#### `const dirent = await dir.read()`

Read the next directory entry. Returns a `Dirent` or `null` when all entries have been read.

#### `dir.read(callback)`

Callback version of `dir.read()`.

#### `const dirent = dir.readSync()`

Synchronous version of `dir.read()`.

#### `await dir.close()`

Close the directory handle.

#### `dir.close(callback)`

Callback version of `dir.close()`.

#### `dir.closeSync()`

Synchronous version of `dir.close()`.

### `Dirent`

Represents a directory entry, returned when iterating a `Dir` or using `fs.readdir()` with `withFileTypes: true`.

#### `dirent.parentPath`

The path of the parent directory.

#### `dirent.name`

The name of the directory entry, as a string or `Buffer` depending on the encoding.

#### `dirent.type`

The numeric type of the directory entry.

#### `dirent.isFile()`

Returns `true` if the entry is a regular file.

#### `dirent.isDirectory()`

Returns `true` if the entry is a directory.

#### `dirent.isSymbolicLink()`

Returns `true` if the entry is a symbolic link.

#### `dirent.isFIFO()`

Returns `true` if the entry is a FIFO.

#### `dirent.isSocket()`

Returns `true` if the entry is a socket.

#### `dirent.isCharacterDevice()`

Returns `true` if the entry is a character device.

#### `dirent.isBlockDevice()`

Returns `true` if the entry is a block device.

### `ReadStream`

A readable stream for file data, created by `fs.createReadStream()`. Extends `Readable` from <https://github.com/holepunchto/bare-stream>.

#### `stream.path`

The file path, or `null` if opened by file descriptor.

#### `stream.fd`

The underlying file descriptor.

#### `stream.flags`

The flags the file was opened with.

#### `stream.mode`

The mode the file was opened with.

### `WriteStream`

A writable stream for file data, created by `fs.createWriteStream()`. Extends `Writable` from <https://github.com/holepunchto/bare-stream>.

#### `stream.path`

The file path, or `null` if opened by file descriptor.

#### `stream.fd`

The underlying file descriptor.

#### `stream.flags`

The flags the file was opened with.

#### `stream.mode`

The mode the file was opened with.

### `Watcher`

Watches for file system changes, created by `fs.watch()`. Extends `EventEmitter` from <https://github.com/holepunchto/bare-events>.

#### `watcher.close()`

Stop watching for changes.

#### `watcher.ref()`

Prevent the event loop from exiting while the watcher is active.

#### `watcher.unref()`

Allow the event loop to exit even if the watcher is still active.

#### `event: 'change'`

Emitted with `(eventType, filename)` when a change is detected. `eventType` is either `'rename'` or `'change'`.

#### `event: 'error'`

Emitted with `(err)` when an error occurs.

#### `event: 'close'`

Emitted when the watcher is closed.

### `FileHandle`

Returned by `require('bare-fs/promises').open()`. Provides an object-oriented API for working with file descriptors.

#### `await handle.close()`

Close the file handle.

#### `const { bytesRead, buffer } = await handle.read(buffer[, offset[, len[, pos]]])`

Read from the file into `buffer`.

#### `const { bytesRead, buffers } = await handle.readv(buffers[, pos])`

Read from the file into an array of `buffers`.

#### `const { bytesWritten, buffer } = await handle.write(data[, offset[, len[, pos]]])`

Write `data` to the file.

#### `const { bytesWritten, buffers } = await handle.writev(buffers[, pos])`

Write an array of `buffers` to the file.

#### `const stats = await handle.stat()`

Get the status of the file.

#### `await handle.chmod(mode)`

Change the permissions of the file.

#### `await handle.chown(uid, gid)`

Change the owner and group of the file.

**NOTE**: This function is not implemented on Windows.

#### `await handle.datasync()`

Similar to `fsync`, but does not flush modified metadata unless necessary.

#### `await handle.sync()`

Flush all modified in-core data of the file.

#### `await handle.truncate(len)`

Truncate the file.

#### `await handle.utimes(mode)`

Change the access and modification times of the file.

#### `const stream = handle.createReadStream([opts])`

Create a readable stream for the file.

#### `const stream = handle.createWriteStream([opts])`

Create a writable stream for the file.

#### `handle.fd`

The file descriptor number.

#### `event: 'close'`

Emitted when the file handle is closed.

## License

Apache-2.0