Skip to content

fix: Enable getNode to follow directory links (fixes #248) - #318

Merged
erikian merged 2 commits into
electron:mainfrom
AndreasFranek:248-getNode-follow-links
Nov 5, 2024
Merged

erikian merged 2 commits into
electron:mainfrom
AndreasFranek:248-getNode-follow-links

Conversation

@AndreasFranek

Copy link
Copy Markdown
Contributor

Fixes #248

As reported in the issue, soft links (or symlinks/symbolic links) can cause a TypeError to occur in asar.stat. This happens when a directory below the stat'ed path is actually a link.

This commit adds a test demonstrating the problem and fixes the problem by introducing a followLinks variable in getNode and resolving directory links, if it is set.

This is my first PR in this project so any feedback is welcome. I was not sure whether to implement the fix imperatively or recursively, for example. I.e., it is possible to follow the link like this instead:

  getNode (p, followLinks) {
    followLinks = typeof followLinks === 'undefined' ? true : followLinks
    var node = this.searchNodeFromDirectory(path.dirname(p))
    while (node.link && followLinks) {
      node = this.searchNodeFromDirectory(node.link)
    }
    const name = path.basename(p)
    if (name) {
      return node.files[name]
    } else {
      return node
    }
  }

@AndreasFranek
AndreasFranek requested a review from a team as a code owner August 12, 2024 08:51
@AndreasFranek

Copy link
Copy Markdown
Contributor Author

Follow-up question, why is the followLinks parameter not defaulted to true?
I.e., why is it

  getFile (p, followLinks) {
    followLinks = typeof followLinks === 'undefined' ? true : followLinks

and not

  getFile (p, followLinks = true) {

?

@AndreasFranek AndreasFranek changed the title Enable getNode to follow directory links (fixes #248) fix: Enable getNode to follow directory links (fixes #248) Aug 12, 2024
@MarshallOfSound

Copy link
Copy Markdown
Member

Should probably land after #320

@AndreasFranek

Copy link
Copy Markdown
Contributor Author

Rebased.

@AndreasFranek

Copy link
Copy Markdown
Contributor Author

Any further input on this?

Comment thread test/api-spec.js Outdated
});
it('should stat a symlinked file', async () => {
const stats = asar.statFile('test/input/stat-symlink.asar', 'real.txt', true);
return assert.strictEqual(stats.size, 19);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we assert that this is not a link instead of checking the size? I think it makes the intent clearer:

Suggested change
return assert.strictEqual(stats.size, 19);
return assert.strictEqual(stats.link, undefined);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Done.

When called from stat, getNode should follow directory links to
ensure that the correct node is returned. Otherwise, node.files[name]
will be undefined, causing a TypeError.
@erikian
erikian merged commit f30b93d into electron:main Nov 5, 2024
@continuous-auth

Copy link
Copy Markdown

🎉 This PR is included in version 3.2.16 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

There is an error that seems to be caused by soft links

3 participants