符号链接到文件的一部分?

符号链接到文件的一部分?

是否可以创建仅是另一个文件的字节子序列的文件(例如符号链接),但仅引用文件的一部分?

答案1

是的,至少在 Linux 上(在某种程度上)是可能的,但有一些限制。

该方法是创建一个映射到文件子集的读写循环设备。

例如:

# Write some data to a single file
for ((i=0;i<10000;i++)); do
    printf "%7d\n" $i
done >/var/tmp/file

# Use losetup to manage a new loopback device
# The --find option without any arguments to find an available loop device
# The --show will echo what that found device is to stdout
# We capture this to ensure the rest of the script refers to the correct device
NEW_DEV_LOOP=$(sudo losetup --show --verbose --find --show --offset 512 --sizelimit 512 /var/tmp/file)

# This will be something like /dev/loop3 depending on what devices are available
echo "NEW_DEV_LOOP = $NEW_DEV_LOOP"

# Print status
losetup -a

# Show the contents of the file and loop device
head -2 /var/tmp/file
echo ...
tail -2 /var/tmp/file
echo "==="
sudo head -2 "$NEW_DEV_LOOP"
echo ...
sudo tail -2 "$NEW_DEV_LOOP"

# Write to the loop device and show impact on the file
sudo sh -c "printf 'I was here' > '$NEW_DEV_LOOP'"
grep here /var/tmp/file

# Detatch the example loop device
sudo losetup -d "$NEW_DEV_LOOP"

输出:

NEW_DEV_LOOP=/dev/loop0
loop device: /dev/loop0
/dev/loop0: [0808]:136392 (/var/tmp/file), offset 512, size 512
      0
      1
...
   9998
   9999
===
     64
     65
...
    126
    127
I was here   65

我相信偏移量和大小都必须是块大小(512 字节)的倍数。

您可能需要成为 root 才能创建和访问循环设备。

如果需要符号链接,可以创建一个指向循环设备的符号链接。

答案2

不可以。您必须引用整个文件,或者必须复制您感兴趣的文件部分。

答案3

在实现了以下功能的文件系统上有可能(创建指向文件一部分的正常符号链接)“文件即目录”的想法,除了其他作者之外,还出现在reiserfs 的计划,但是 reiserfs 的实现不被喜欢针对实施过程中出现的问题。

相关内容