发送稀疏文件

时间:2016-07-06 15:29:49

标签: python linux python-2.7 sockets

我一直在使用Python中的套接字,我希望能够将稀疏的图像文件从一台机器发送到另一台机器。正如所料,通过python套接字发送稀疏文件不会保留文件的稀疏性。我想做一个稀疏的焦油并以这种方式发送,但我无法理解它。

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

正如您所看到的,它会扩展文件并且不再稀疏。

1 个答案:

答案 0 :(得分: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当然是任意选择的,在这种情况下可能还有其他协议更好。你也可以创建自己的。