Jasen Betts <
ja...@xnet.co.nz> writes:
> On 2016-07-05, marrgol <mar...@address.invalid> wrote:
>> On 2016-07-05 10:17, Jasen Betts wrote:
>>>>>> My question is how to relink a filehandle into a filesystem. :) What
>>>>>> system call, or other magic? :)
>>>>>
>>>>> The question still stands
>>>>
>>>> I don't think you can "relink a filehandle into a filesystem", so
>>>> just some other magic then: ;-)
>>>
>>> You can eg: if you can access a reference to the deleted file in
>>> /proc/{NUMBER}/fd/ you can undelete it by creating a hard link
>>
>> Hard link to what exactly? And how do you create a cross-device
>> or cross-filesystem hard link?
>
> I'm not sure... I can't make it work today:
>
> echo test > /tmp/gon ## create a file
> tail -f /tmp/gon & ## open the file
> rm /tmp/gon ## unlink it
>
> ## try to link the inode
> ln /proc/$!/fd/3 /tmp/bak ## doesn't work
>
> ln -L /proc/$!/fd/3 /tmp/bak ## also doesn't work
link() does not follow symbolic links. If you try to hardlink a name
which resolves to a symbolic link you just get another name for the same
symbolic link. Notice the inode numbers and counts below:
$ touch a
$ ln -s a b
$ ln b c
$ ls -ldi a b c
2359598 -rw-rw-r-- 1 richard richard 0 Jul 6 09:29 a
2360014 lrwxrwxrwx 2 richard richard 1 Jul 6 09:29 b -> a
2360014 lrwxrwxrwx 2 richard richard 1 Jul 6 09:29 c -> a
If you try to hard link to a psuedo-file in /proc/fd/... then you’re
attempting to hard-link across filesystems, which does not work.
You can play some daft games with bind mounts though:
$ echo whatever > a
$ cat a
whatever
$ sleep 10000 < a
^Z
[1]+ Stopped sleep 10000 < a
$ %&
[1]+ sleep 10000 < a &
$ rm a
$ ls -l /proc/9429/fd/0
lr-x------ 1 richard richard 64 Jul 6 09:33 /proc/9429/fd/0 -> /home/richard/a (deleted)
$ touch b
$ cat b
$ sudo mount -o bind /proc/9429/fd/0 b
$ cat b
whatever
$ kill 9429
[1]+ Terminated sleep 10000 < a
$ ls -l /proc/9429/fd/0
ls: cannot access ‘/proc/9429/fd/0’: No such file or directory
$ cat b
whatever
$ sudo umount b
$ cat b
$
A solution to the OPs original problem, as I understand it, is: write
the partial uploads to temporary files. When upload completes rename
them into their required name. Use a cronjob or something similar that
periodically cleans up stale temporary files. Put the PID of the
uploading process in the name so you can avoid deleting live ones, or
maybe just assume anything older than (for instance) a day is stale.
--
http://www.greenend.org.uk/rjk/