如何在磁带上写 EOF 标记 - Python

How to write EOF markers on Magnetic Tape - Python

我在网上搜索了很多,但找不到在 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 here 但我不确定如何通过 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