【问题标题】:Sending Sparse Files发送稀疏文件
【发布时间】:2023-03-08 22:58:02
【问题描述】:

我一直在搞乱 Python 中的套接字,我希望能够将稀疏图像文件从一台机器发送到另一台机器。正如预期的那样,通过 python 套接字发送稀疏文件不会保留文件的稀疏性。我想做一个稀疏的 tar 并以这种方式发送,但我就是想不通。

tarfile 模块说它支持读取 GNU 格式的稀疏文件,这对我创建它们没有帮助……但是 python 文档说 Pax 格式“几乎没有限制”。我不确定这是否意味着我可以创建存档并保留稀疏文件或不使用 pax 格式...我一直在尝试,但我不知道它是如何工作的。

如果这个解决方案不是一个选项,还有其他方法可以通过套接字发送稀疏文件吗?我讨厌不得不通过我的应用程序中的系统命令调用“tar -xSf”...

谢谢,

服务器

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.bind((socket.gethostname(), 50001))
s.listen(1)

img = open('test.img', 'rb')

client, addr = s.accept()
l = img.read(8192)

while(l):
        client.send(l)
        l = img.read(8192)

img.close()
s.close()

客户

host = ''
port = 50001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.connect((host, port))

img = open('./newimg.img', 'wb')

l = s.recv(8192)

while(l):
    img.write(l)
    l = s.recv(8192)

img.close()
s.close()

在服务器上,我新建一个稀疏文件:truncate -s 1G test.img

du -h 显示:0 test.img

我运行我的服务器和客户端。这是传输文件上的du -h:1.0G newimg.img

如你所见,它扩展了文件,不再稀疏。

【问题讨论】:

  • “不保留文件的稀疏性”是什么意思?文件发送后是否更改?如果是这样,您可能实施了错误的发送,文件不应该只是神奇地改变。你能展示一些代码,预期和实际的输出/输入吗?
  • 与 netcat、scp、rsync 等相同。当您发送稀疏文件而不将其放入保留稀疏性并在另一端提取它的 tar 存档中时,不会保留稀疏性并且它扩展到全尺寸。试试看。我的代码非常简单,我只是使用套接字并遍历我的发送和接收,直到文件完全传输。如果我从一台机器发送完全空的 10GB 稀疏文件,它会在接收端扩展到完整的 10GB 大小。我真正的问题是如何使用 tarfile 模块处理稀疏文件,或者我还能如何完成这个
  • @syntonym 我添加了一个例子。也请参阅我上面的评论(不是针对 TesselatingHeckler 的评论)
  • 有两种方法可以做到这一点 - 1) 提出一个有线协议,发送组(偏移量、大小、数据)来表示不稀疏的文件块,并重新创建文件在接收端适当地发送,或者 2) 继续按原样发送整个文件,但让接收器检测到长时间运行的零并且不费心将它们写入输出文件。这两种方法都不是微不足道的,但它们也不应该太难。检查源以获取类似rsync 的信息...
  • @user165222 你能说明你在使用 tar 时遇到了什么问题吗? twalberg 是对的,但简单地使用 tar 可能会更容易。

标签: python linux python-2.7 sockets


【解决方案1】:

如果您写入文件的开头,通常会在文件中创建空洞,然后搜索到结尾并在那里写入。如果您读取文件,即使文件中有漏洞,您也会读取零。当您发送文件时,会发送文字字节,当然也会读取。然后,当您写入字节时,所有字节都将被写入,并且不会发生文件系统创建孔的情况。

为了缓解这种情况,您可以先查找文件中的漏洞,发送到它们所在的位置,然后发送文件的其余部分。

以下内容并未完善,但应该为您提供一个起点。

import os

f = open(path, "b")
fd = f.fileno()

end = os.stat(fd).st_size
holes = []
offset = os.lseek(fd, 0, os.SEEK_HOLE)
while offset != end:
    end_hole = os.lseek(fd, offset, os.SEEK_DATA)
    holes.append((offset, end_hole))
    offset = end_hole

[open socket and stuff]

# send the holes

socket.write(json.dumps(holes)) # encode appropriately

# send file

f.seek(0)
total = 0
for hole in holes:
    while total < hole[0]:
        l = f.read(8192)
        if len(l) + total > hole[0]:
            socket.write(l[:len(l) + total - hole[0]])
            l.seek(hole[1])
            total += len(1) + total - hole[0]
        else:
            socket.write(l)
            total += len(l)

然后在客户端:

still_json = True
a = []
l = s.recv(8192)

while(still_json):
    a.append(l)
    if check_json_end(l):
        still_json = False
    else:
        l = s.recv(8192)

holes = parse_json(a) # the last chunk can contain something that is not json
# I asume that a still contains the bytes that are not json

fout = open(outfile, "wb")
total = 0

fout.write(a[0]) # handle the case where the first rest after the json in a is already after a hole

total += len(a[0]) 

for hole in holes:
    while total < hole[0]:
        l = socket.recv(8192)
        if len(l) + total > hole[0]:
            fout.write(l[:len(l) + total - hole[0]])
            fout.seek(hole[1])
            fout.write(l[len(l) + total - hole[0]:])
        else:
            fout.write(l)
        total += len(l)

其中可能有很多错误,您应该重新考虑每一行,但一般原则应该没问题。 JSON当然是任意选择的,在这种情况下可能还有其他更好的协议。您也可以创建自己的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-12
    • 2014-10-03
    • 1970-01-01
    • 2018-04-13
    • 2012-04-08
    • 1970-01-01
    • 2019-04-08
    • 2011-03-13
    相关资源
    最近更新 更多