how to force a symbolic link?

P

Peng Yu

Hi,

In shell, 'ln' has an option -f. I'm wondering if there is such option
for symlink in perl.

Thinks,
Peng
 
T

Tim Greer

Peng said:
Hi,

In shell, 'ln' has an option -f. I'm wondering if there is such option
for symlink in perl.

Thinks,
Peng

I don't see any mention of such an option. You could just have the perl
script use ln itself (with the option needed) if you want that feature.
Or, you could check if a symlink already exists and points where you
want, and if not, then take the appropriate action... unless some force
option does exist in symlink(), and I'm fairly sure it doesn't.
 
J

John W. Krahn

Peng said:
In shell, 'ln' has an option -f. I'm wondering if there is such option
for symlink in perl.

-e 'oldlink' and unlink 'oldlink';
symlink 'yourfile', 'oldlink' or die "oldlink: $!";


John
 
E

Eric Pozharski

-e 'oldlink' and unlink 'oldlink';
symlink 'yourfile', 'oldlink' or die "oldlink: $!";

If my understanding of unlink(2) and symlink(2) is correct either one
would fail or both. Anyway there's race, so:

unlink 'oldlink;
symlink 'yourfile', 'oldlink' or die $!;

Then what's wrong with that:

#/usr/bin/perl
use strict;
use warnings;
use File::Temp qw|tempfile|;
use File::Basename;
my ($link, $yourfile) = ( @ARGV );
my ($fd, $fn) = ( dirname($link), basename $link );
my($fh), $fn = tempfile "$fn.XXXXXX", DIR => $fd;
unlink $fn or die "unlink: $!";
symlink $yourfile, $fn or die "symlink: die $!";
close $fd;
rename $fn, $link or die "rename: $!";
__END__

Did I really needed to keep I<$fn> open?
 
S

sri

If my understanding of unlink(2) and symlink(2) is correct either one
would fail or both. Anyway there's race, so:

unlink 'oldlink;
symlink 'yourfile', 'oldlink' or die $!;

Then what's wrong with that:

#/usr/bin/perl
use strict;
use warnings;
use File::Temp qw|tempfile|;
use File::Basename;
my ($link, $yourfile) = ( @ARGV );
my ($fd, $fn) = ( dirname($link), basename $link );
my($fh), $fn = tempfile "$fn.XXXXXX", DIR => $fd;
unlink $fn or die "unlink: $!";
symlink $yourfile, $fn or die "symlink: die $!";
close $fd;
rename $fn, $link or die "rename: $!";
__END__

Did I really needed to keep I<$fn> open?


Why are you trying much complex method to create a symbolic link ??

my ($link, $yourfile) = ( @ARGV );
symlink $yourfile, $link or die "symlink: die $!";

I think this will give you the result...
 
T

Tad J McClellan

^^ ^^
^^ ^^^^^^^^^^^^^^
^^^^^^^^^^^^

[ snip code ]
Why are you trying much complex method to create a symbolic link ??


Two reasons.

First, your code does not even do what was asked for. If $link already
exists it will not be replaced like it would with "ln -f".

Second, he is trying to avoid the race condition he mentioned.
 
D

David Combs

Where's the race condition?

Does unlink return before the unlinking has been completed?

Thanks,

David
 
P

Peter J. Holzer

Where's the race condition?

Does unlink return before the unlinking has been completed?

No, but another process could create a new 'oldlink' between the time
unlink returns and the time symlink is called.

(link -f has the same problem, of course)

hp
 
T

Tim Greer

David said:
Where's the race condition?

Does unlink return before the unlinking has been completed?

Thanks,

David

Any time you do a test with something like "if file doesn't exist,
overwrite it", without doing locking (one of many examples), you create
a race condition. If the script has a bug due to that, or if someone
intentionally exploits that small window of time, you could have
problems. Always ask yourself "what happens if something creates that
file between the time it checks if it exists and the time I remove it,
overwrite it or work with it, and what methods you have available to
prevent that.
 
D

David Combs

No, but another process could create a new 'oldlink' between the time
unlink returns and the time symlink is called.

(link -f has the same problem, of course)

hp

Thank you!

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top