【问题标题】:Extract File With LibTar使用 LibTar 提取文件
【发布时间】:2013-06-24 02:49:36
【问题描述】:

我在尝试使用 Libtar 提取文件时遇到了一个小问题。

这是我的代码:

 int htlp_decompress_decompress(char * filename) {

    TAR * tar_file;
    char rootdir[200];
    strcpy(rootdir, "/var/cache/htpackage/");

    if (tar_open(&tar_file, filename, NULL, O_RDONLY, 0, TAR_GNU) == -1) {
        fprintf(stderr, "tar_open(): %s\n", strerror(errno));
        return -1;
    }

    if (tar_extract_all(tar_file, rootdir) != 0) {
        fprintf(stderr, "tar_extract_all(): %s\n", strerror(errno));
        return -1;
    }

    if (tar_close(tar_file) != 0) {
        fprintf(stderr, "tar_close(): %s\n", strerror(errno));
        return -1;
    }

    return 0;
}

问题是我在 tar_extract_all() 函数中收到错误“无效参数”。但我不知道是什么导致了这个错误。

有人知道发生了什么吗?

感谢您的关注。

【问题讨论】:

  • Fixed the error - 怎么样?

标签: c++ c extract tar


【解决方案1】:

根据手册页,函数声明为:

int tar_open(TAR **t, char *pathname, tartype_t *type, int oflags, int mode, int options);

这意味着您将 O_RDONLY 作为 tartype_t *type 参数传递。这是不正确的。也许你的意思是:

tar_open(&tar_file, filename, NULL, O_RDONLY, 0, TAR_GNU)

【讨论】:

  • 我修复了错误,但仍然无法正常工作。 :( 感谢您的帮助!
  • 所以,如果您仍然期待帮助,也许您可​​以提供更多关于“不起作用”意味着什么的信息。 =)
  • 仍然有错误“无效参数” int tar_extract_all() 函数。
  • 好吧,文档很粗略。我见过的其他示例将传递mode 的值。我认为这是在创建存档时使用的,但您可以尝试将其设置为 0644 而不是当前的 0。否则,也许要确保目标目录存在。
  • 目标目录存在。我会尝试在模式参数中设置为 0644。 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 2012-12-13
  • 2018-11-15
  • 2012-07-23
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多