How to use the openSync function from graceful-fs

Find comprehensive JavaScript graceful-fs.openSync code examples handpicked from public code repositorys.

graceful-fs.openSync is a synchronous version of the fs.open method, which opens a file and returns its file descriptor.

137
138
139
140
141
142
143
144
145
146
147
var F_WRLCK = 1;
var SEEK_SET = 0;


exports.createPidFile = createPidFile;
function createPidFile(path) {
  var fd = fs.openSync(path, 'a+', 0600);
  if (!posix.acquireRecordLock(fd, F_WRLCK, SEEK_SET, 0, 0)) {
    return false;
  }

fork icon291
star icon672
watch icon0

483
484
485
486
487
488
489
490
491
492
493
494
exports.mkdirs = mkdirs;
exports.mkdirsSync = mkdirsSync;


// open
exports.open = Promise.promisify(fs.open);
exports.openSync = fs.openSync;


// symlink
exports.symlink = Promise.promisify(fs.symlink);
exports.symlinkSync = fs.symlinkSync;
fork icon9
star icon48
watch icon0

+ 7 other calls in file

How does graceful-fs.openSync work?

graceful-fs.openSync is a synchronous method in the graceful-fs library that opens a file in the specified mode and returns a file descriptor, which is a unique identifier for the file opened by the operating system. This method is similar to the built-in fs.openSync() method but provides additional error handling and retries in case of certain errors.

9
10
11
12
13
14
15
16
17
18
19
  tmpfile = path.join(os.tmpdir(), tmpfile)


  // 550 millis past UNIX epoch
  const d = new Date(1435410243862)
  fs.writeFileSync(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141')
  const fd = fs.openSync(tmpfile, 'r+')
  fs.futimesSync(fd, d, d)
  fs.closeSync(fd)
  return fs.statSync(tmpfile).mtime > 1435410243000
}
fork icon1
star icon0
watch icon0

62
63
64
65
66
67
68
69
70
const BUF_LENGTH = 64 * 1024
const _buff = buffer(BUF_LENGTH)

const flags = overwrite ? 'w' : 'wx'

const fdr = fs.openSync(src, 'r')
const stat = fs.fstatSync(fdr)
const fdw = fs.openSync(dest, flags, stat.mode)
let pos = 0
fork icon0
star icon1
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
const fs = require("graceful-fs");

try {
  const fd = fs.openSync("example.txt", "r");
  console.log("File opened successfully.");
  fs.closeSync(fd);
} catch (err) {
  console.error("Error occurred while opening file:", err);
}

In this example, fs.openSync is used to open the file example.txt in read mode ('r') synchronously. If the file opening is successful, a success message is logged to the console, and the file descriptor returned by fs.openSync is used to close the file synchronously using fs.closeSync. If an error occurs during the file opening process, an error message is logged to the console.

336
337
338
339
340
341
342
343
344
345
 */
function findAccessibleSync (logprefix, dir, candidates) {
  for (var next = 0; next < candidates.length; next++) {
    var candidate = path.resolve(dir, candidates[next])
    try {
      var fd = fs.openSync(candidate, 'r')
    } catch (e) {
      // this candidate was not found or not readable, do nothing
      log.silly(logprefix, 'Could not open %s: %s', candidate, e.message)
      continue
fork icon0
star icon0
watch icon1

18
19
20
21
22
23
24
25
26
27
  }
}

var fdr = fs.openSync(srcFile, 'r')
var stat = fs.fstatSync(fdr)
var fdw = fs.openSync(destFile, 'w', stat.mode)
var bytesRead = 1
var pos = 0

while (bytesRead > 0) {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

17
18
19
20
21
22
23
24
25
26
  } else return
}

const fdr = fs.openSync(srcFile, 'r')
const stat = fs.fstatSync(fdr)
const fdw = fs.openSync(destFile, 'w', stat.mode)
let bytesRead = 1
let pos = 0

while (bytesRead > 0) {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

22
23
24
25
26
27
28
29
30
31
32
33
function file_size(fd) {
	return fs.fstatSync(fd).size;
}


function file_open(path) {
	return fs.openSync(path, "r+");
}


function file_read(fd, offset, len) {
	var res = Buffer.alloc(len);
fork icon0
star icon0
watch icon0

62
63
64
65
66
67
68
69
70
71
72
    })
  })
}


function utimesMillisSync (path, atime, mtime) {
  const fd = fs.openSync(path, 'r+')
  fs.futimesSync(fd, atime, mtime)
  return fs.closeSync(fd)
}

fork icon0
star icon0
watch icon0

+ 5 other calls in file

77
78
79
80
81
82
83
84
85
86
87
88
function copyFileFallback (srcStat, src, dest, opts) {
  const BUF_LENGTH = 64 * 1024
  const _buff = require('../util/buffer')(BUF_LENGTH)


  const fdr = fs.openSync(src, 'r')
  const fdw = fs.openSync(dest, 'w', srcStat.mode)
  let pos = 0


  while (pos < srcStat.size) {
    const bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
fork icon0
star icon0
watch icon0

+ 5 other calls in file

206
207
208
209
210
211
212
213
214
215
var fd
var cleanup = cleanupOnExit(tmpfile)
var removeOnExitHandler = onExit(cleanup)

try {
  fd = fs.openSync(tmpfile, 'w', options.mode)
  if (Buffer.isBuffer(data)) {
    fs.writeSync(fd, data, 0, data.length, 0)
  } else if (data != null) {
    fs.writeSync(fd, String(data), 0, String(options.encoding || 'utf8'))
fork icon0
star icon0
watch icon0

+ 3 other calls in file

197
198
199
200
201
202
203
204
205
206
  }
}

var cleanup = cleanupOnExit(tmpfile)
var removeOnExitHandler = onExit(cleanup)
var fd = fs.openSync(tmpfile, 'w', options.mode)
if (Buffer.isBuffer(data)) {
  fs.writeSync(fd, data, 0, data.length, 0)
} else if (data != null) {
  fs.writeSync(fd, String(data), 0, String(options.encoding || 'utf8'))
fork icon0
star icon0
watch icon0

+ 3 other calls in file

44
45
46
47
48
49
50
51
52
53
54
	throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
});


exports.openSync = (path, flags, mode) => {
	try {
		return fs.openSync(path, flags, mode);
	} catch (error) {
		if (flags.includes('w')) {
			throw new CpFileError(`Cannot write to \`${path}\`: ${error.message}`, error);
		}
fork icon0
star icon0
watch icon0