How to use the link function from graceful-fs

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

graceful-fs.link is a function that creates a new hard link from the existingPath to the newPath in a graceful way, supporting multiple platforms and edge cases.

5
6
7
8
9
10
11
12
13
14
15
const mkdir = require('../mkdirs')
const pathExists = require('../path-exists').pathExists


function createLink (srcpath, dstpath, callback) {
  function makeLink (srcpath, dstpath) {
    fs.link(srcpath, dstpath, err => {
      if (err) return callback(err)
      callback(null)
    })
  }
fork icon0
star icon0
watch icon1

+ 5 other calls in file

1
2
3
4
5
6
7
8
9
10
11
var fs = require('graceful-fs')
var mkdir = require('../mkdirs')


function createLink (srcpath, dstpath, callback) {
  function makeLink (srcpath, dstpath) {
    fs.link(srcpath, dstpath, function (err) {
      if (err) return callback(err)
      callback(null)
    })
  }
fork icon0
star icon0
watch icon1

+ 9 other calls in file

How does graceful-fs.link work?

graceful-fs.link is a function provided by the graceful-fs module which creates a new hard link from an existing file to a new file. It takes two arguments: the existingPath and the newPath. The existingPath is the path to the file that the hard link will point to, and the newPath is the path to the new hard link file that will be created. When called, graceful-fs.link first checks if the file at existingPath exists and is a file. If not, it will throw an error. If the file exists and is a file, it will attempt to create a new hard link at newPath. If the file at newPath already exists, it will be replaced by the new hard link. graceful-fs.link also handles certain edge cases gracefully. For example, on some platforms, creating a hard link from a directory is not allowed, and on others, creating a hard link across different file systems may not be supported. graceful-fs.link will handle these cases and report errors appropriately. Overall, graceful-fs.link provides a reliable and cross-platform way to create hard links between files, with error handling for various edge cases.

Ai Example

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

const existingPath = "/path/to/existing/file";
const newPath = "/path/to/new/hardlink";

fs.link(existingPath, newPath, (err) => {
  if (err) {
    console.error(err);
  } else {
    console.log(`Hard link created from ${existingPath} to ${newPath}`);
  }
});

This code creates a new hard link at /path/to/new/hardlink that points to the file at /path/to/existing/file. If the operation is successful, it will log a message to the console saying that the hard link was created. If there is an error, it will log the error message to the console.