如何使用 C/++ 在 linux 中正确写入磁带驱动器?

如何解决如何使用 C/++ 在 linux 中正确写入磁带驱动器?

我目前正在尝试通过 C++ 写入 LTO 磁带设备。我的问题是,随后的写入请求将导致“无效参数”错误,而第一个调用似乎工作正常。

我已将磁带驱动器设置为使用 64k 的固定块大小(这意味着它一次只能写入 64k 或 64k 倍数的块)。
现在尝试通过 fwrite() 对其进行写入时,第一次调用将成功(或至少返回写入的请求的条目数量),但后续调用(具有相同参数)将导致没有数据被写入( fwrite 返回 0) 和“无效参数”错误
以下是一个用于重现该问题的小型示例应用程序。

#include <iostream>
#include <unistd.h>

#define BLOCK_SIZE (64 * 1024)
#define DATA_BLOCKS 32
#define byte unsigned char

int main(int argc,char *argv[]) {
    if (argc <= 1) {
        std::cout << "Missing first parameter: Target file\n";
        return 1;
    }

    // Create file handle to access tape device in RW mode
    FILE* handle;
    const char* targetFile = argv[1];
    if (access(targetFile,R_OK | W_OK) == 0) {
        handle = fopen(targetFile,"r+");
        printf("Create handle OK: %s\n",targetFile);
    } else {
        printf("Could not access %s: Missing rad or write permission\n",targetFile);
        return 1;
    }

    // Create an byte array with data for DATA_BLOCKS blocks
    byte *data = new byte[BLOCK_SIZE * DATA_BLOCKS];

    // Initialize with some data
    for (int i = 0; i < BLOCK_SIZE * DATA_BLOCKS; i++) {
        data[i] = '5';
    }

    // Write data in BLOCK_SIZE chunks,blocksToWrite times per fwrite() call,in numberOfWriteCalls fwrite() calls
    size_t blocksToWrite = 4;
    int numberOfWriteCalls = 5;

    size_t written;
    for (int i = 0; i < numberOfWriteCalls; i++) {
        written = fwrite(data,BLOCK_SIZE,blocksToWrite,handle);
        printf("Round %d: Wrote %d entries of expected %d entries\n",i+1,(int) written,(int) blocksToWrite);

        // Check if there was an error and if so,print error info to stderr
        if (ferror(handle)) {
            printf("Error while writing:\n");
            perror("");
            clearerr(handle);
        }

        /* Start modification: Added flushing,as pointed out by user7860670 */
        fflush(handle);

        // Check if there was an error and if so,print error info to stderr
        if (ferror(handle)) {
            printf("Error while flushing:\n");
            perror("");
            clearerr(handle);
        }
        /* End modification: Added flushing,as pointed out by user7860670 */
    }

    delete[] data;

    // Close file handle
    fclose(handle);

    return 0;
}

这段代码调用了 fwrite 函数 5 次,每次尝试写入 4 个 64k 数据块。 这适用于普通文件,但在我的所有磁带设备上返回以下输出

Create handle OK: /dev/nst0
Round 1: Wrote 4 entries of expected 4 entries
Round 2: Wrote 0 entries of expected 4 entries
Invalid argument
Round 3: Wrote 0 entries of expected 4 entries
Invalid argument
Round 4: Wrote 0 entries of expected 4 entries
Invalid argument
Round 5: Wrote 0 entries of expected 4 entries
Invalid argument

可以看出,第一个调用的反应如预期:fwrite 返回已经写入了 4 个数据块。但是所有后续调用,即使使用相同的参数,也会返回 0 个写入块和“无效参数”错误
尝试写入我不知道的磁带文件时是否有任何“特殊情况”,或者我是否可能以错误的方式使用 fwrite 功能

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?