Skip to content

[Filesystem] Add feature to create hardlinks for files #15458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

andrerom
Copy link
Contributor

@andrerom andrerom commented Aug 5, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Todo:

  • Tests
  • Doc
  • Getting someone to test on Windows as exception might differ from symlink functionality

Why

Symlinks are good for directories, but for files hardlinks are sometime a better match when needing to represent same file in several locations.

One use case for this feature is for multi tagging of Http Cache when running against FileSystem. Multi tagging is used in FoSHttpCache and supported when running Varnish, but not with Symfony HTTPCache, yet.. With hardlinks combined with meta information containing tags within the file, lookup and reverse lookup is thus easily possible.

What

Introduces method hardlink() with similar behavior as symlink, difference being:

  • Allowing several targets to be provided to match use case in 'why'
  • Like symlink removes existing target if not the same, but uses fileinode to compare target & source

{
if (!$this->exists($originFile)) {
throw new FileNotFoundException(null, 0, null, $originFile);
} elseif (!is_file($originFile)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you can make it a regular if condition.

@andrerom andrerom force-pushed the hard_links branch 2 times, most recently from b43aa9c to 029faeb Compare August 6, 2015 19:04
@andrerom
Copy link
Contributor Author

andrerom commented Aug 6, 2015

@hhamon fixed those + suggestions from fabbot.io in 029faeb

@andrerom
Copy link
Contributor Author

Open question: Would it be ok to aim at backporting this for 2.3 and up? I have seen similar small useful features being backported before which is why I ask. For FOSHttpCache that would be helpful to not have to require 2.8.

@stof
Copy link
Member

stof commented Aug 11, 2015

@andrerom no it is not. New features are never backported, unless it is necessary to fix a security issue (or a PHP compatibility issue)

@andrerom
Copy link
Contributor Author

ok, clear.

}

if (!is_file($originFile)) {
throw new FileNotFoundException("Origin file '{$originFile}' is not a file");
Copy link
Member

Choose a reason for hiding this comment

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

please use sprintf rather than interpolation, as mandated by our coding standards: https://symfony.com/doc/current/contributing/code/standards.html#structure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 0af9468

@stof
Copy link
Member

stof commented Aug 11, 2015

Symlinks are good for directories, but for files hardlinks are a better match when needing to represent same file in several locations.

This actually depends on the use case. Saying that hardlinks is better is misleading.

For instance, if you use hardlinks on a PHP file, __DIR__ will be related to the hardlink location, not to the original location AFAIK (I may be wrong though), which could actually break it.

@andrerom andrerom force-pushed the hard_links branch 2 times, most recently from 8a35fee to 0af9468 Compare August 11, 2015 11:34
@andrerom
Copy link
Contributor Author

This actually depends on the use case. Saying that hardlinks is better is misleading.

Right, corrected that statement, I was actually never considering the use case of linking executable php files with actual code as opposed to cache files or binary files. I would have expected it to report __DIR__ for the link, but the gotcha is maybe more that __FILE__ will most likely point to origin?

@andrerom andrerom force-pushed the hard_links branch 6 times, most recently from fae8b93 to 89e46ad Compare September 14, 2015 08:03
@fabpot
Copy link
Member

fabpot commented Jan 27, 2016

Not sure why this PR staled. @andrerom Can you rebase on current 2.8? I will then be able to merge this into master.

@andrerom
Copy link
Contributor Author

@fabpot done, failure with fabbot.io seems to be unrelated to changes here.

/**
* @var null Flag for hard links on Windows
*/
protected static $linkOnWindows = null;
Copy link
Member

Choose a reason for hiding this comment

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

should be private, and imo there's no need for the docblock here (same below for $symlinkOnWindows)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 584cafe

I did not remove the phpdoc, the other properties have doc so I rather fixed these to say null|bool to give correct hints for IDE's.

@fabpot
Copy link
Member

fabpot commented Jun 13, 2016

Thank you @andrerom.

@fabpot fabpot closed this in 31678c9 Jun 13, 2016
@andrerom andrerom deleted the hard_links branch June 14, 2016 08:40
@fabpot fabpot mentioned this pull request Oct 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants