有没有办法用Go复制tar -h选项?

时间:2017-03-23 18:25:55

标签: unix go

在Unix中,tar实用程序具有-h选项,用于归档链接的目标。我想知道是否有办法用Go来做到这一点。现在,当我尝试将链接的内容复制到tar并且标题名称设置为符号链接文件时,我收到tar: Damaged tar archive错误。以下代码是我目前正在使用的代码。该函数的目标是递归地tar当前目录中的所有内容。

我想也许我所要做的就是将符号链接的大小设置为目标文件的大小,但仍然会出错。

func createTarFile() {
    tarfile, _ := os.Create(buildCtxTar)
    defer tarfile.Close()

    tarball := tar.NewWriter(tarfile)
    defer tarball.Close()

    //get the working directory
    rootDir, _ := os.Getwd()

    //add all the files in the current directory to the tarball
    walkFn := func(path string, info os.FileInfo, err error) error {
        //don't add the tar file to itself, don't add the working directory either
        if info.Name() == buildCtxTar || info.Name() == filepath.Base(rootDir) {
            return nil
        }

        //no absolute paths in the tar file, the "+1" strips the leading file separator
        relativePath := path[len(rootDir)+1:]

        isSymlink := info.Mode()&os.ModeSymlink == os.ModeSymlink
        var size int64 = -1
        //evaluate symbolic links
        if isSymlink {
            path, size, err = handleSymlink(path)
            if err != nil {
                return err
            }
        }

        header, err := tar.FileInfoHeader(info, path)
        if err != nil {
            return err
        }

        //if the OS is windows we need to convert the '\' to '/'
        relativePath = filepath.ToSlash(relativePath)

        //rename the name of the file to its path for a structured tarball
        header.Name = relativePath
        if isSymlink {
            header.Size = size
        }
        if err := tarball.WriteHeader(header); err != nil {
            return err
        }

        //don't attempt copy data from special files (like directories)
        if !isSymlink && !info.Mode().IsRegular() {
            return nil
        }
        f, err := os.Open(path)
        if err != nil {
            return err
        }
        defer f.Close()

        // copy the file data to the tarball
        if _, err := io.Copy(tarball, f); err != nil {
            return err
        }
        return nil
    }

    //walks through all files and subdirectories of the current directory
    err := filepath.Walk(rootDir, walkFn)
}

//get the filepath for the symbolic link
func handleSymlink(path string) (string, int64, error) {
    //read the link
    link, err := os.Readlink(path)
    if err != nil {
        return "", -1, err
    }
    //if the filepath isn't an absolute path we need to
    //reconstruct it so that it is
    if !filepath.IsAbs(link) {
        link = filepath.Join(filepath.Dir(path), link)
        if err != nil {
            return "", -1, err
        }
    }
    fi, err := os.Stat(link)
    if err != nil {
        return "", -1, err
    }
    size := fi.Size()
    return link, size, nil
}

1 个答案:

答案 0 :(得分:0)

您正在创建符号链接tar.Header中的FileInfo,该符号链接仍将包含符号链接的TypeflagMode

从目标文件创建一个新标题,并通过将名称字段更改为符号链接的字段将其“移动”到存档中。