我在网上搜索了很多,但我找不到用Python在磁带上写EOF标记的方法。
我有下面的代码(通过fcntl.ioctl
使用Python(,它写记录,但在每次os.write
之后,它不写EOF,而是将记录保存在一个文件中。从本质上讲,我想将这些记录拆分为中间有EOF标记的文件?
代码:
import os
import struct
import fcntl
MTIOCTOP = 0x40086d01 # Do a magnetic tape operation
MTSETBLK = 20
TAPEDRIVE = '/dev/st1'
fh = os.open(TAPEDRIVE, os.O_WRONLY )
fcntl.ioctl(fh, MTIOCTOP, struct.pack('hi', MTSETBLK, 0))
os.write(fh, b'a'*1024) #<- Does not add EOF mark after write
fcntl.ioctl(fh, MTIOCTOP, struct.pack('hi', MTSETBLK, 0))
os.write(fh, b'b'*2048)
fcntl.ioctl(fh, MTIOCTOP, struct.pack('hi', MTSETBLK, 0))
os.write(fh, b'c'*1024)
fcntl.ioctl(fh, MTIOCTOP, struct.pack('hi', MTSETBLK, 0))
os.write(fh, b'd'*2048)
os.close(fh)
磁带分析:
Commencing Reading Tape in Drive /dev/st1, blocksize = 32768
1024 2048 1024 2048
End of File Mark after 4 records
End of File Mark after 0 records
End of Tape
Tape Examination Complete, found 2 Files on tape`
我注意到mtio.h
在这里包含MTWEOF
,但我不确定如何通过ioctl
实现这一点?
如有任何帮助,我们将不胜感激。
PS。我知道我可以使用mt -f /dev/st1 weof n#
编写EOF标记,但我更喜欢只在Python中使用。
好的,所以在阅读了mtio.h
手册页之后,我制定了它,希望它能对其他人有所帮助。
import os
import fcntl
import struct
MTIOCTOP = 0x40086d01 # Do a magnetic tape operation refer to mtio.h
#MTSETBLK = 20 # Set a block size?
MTWEOF = 5 # Define EOF mark variable refer to mtio.h
TAPEDRIVE = '/dev/st1' # Tape drive location
fd = os.open(TAPEDRIVE, os.O_WRONLY ) # Open device for write
#fcntl.ioctl(fd, MTIOCTOP, struct.pack('hi', MTSETBLK, 32768)) # Set a block size?
for _ in range(5):
os.write(fd, b'a'*1024) # Write some bytes
fcntl.ioctl(fd, MTIOCTOP, struct.pack('hi', MTWEOF, 1)) # Write end-of-file (1)
fcntl.ioctl(fd, MTIOCTOP, struct.pack('hi', MTWEOF, 2)) # Write end-of-tape (2)
os.close(fd)
磁带分析
Commencing Reading Tape in Drive /dev/st1, blocksize = 32768 1024 1024 1024 1024 1024 End of File Mark after 1 records End of File Mark after 1 records End of File Mark after 1 records End of File Mark after 1 records End of File Mark after 1 records End of Tape Tape Examination Complete, found 5 Files on tape