Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #370: Fix file permission handling #371

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions internal/provider/tar_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ func (a *TarArchiver) open() error {
return err
}

if a.outputFileMode != "" {
fileMode, err := parseFileMode(a.outputFileMode)
if err != nil {
return fmt.Errorf("error parsing output_file_mode value: %s", err)
}
if err := a.fileWriter.Chmod(fileMode); err != nil {
return fmt.Errorf("error setting file mode on archive: %s", err)
}
}

switch a.compression {
case TarCompressionGz:
a.compressionWriter = gzip.NewWriter(a.fileWriter)
Expand Down Expand Up @@ -269,14 +279,14 @@ func (a *TarArchiver) addFile(filePath string, header *tar.Header) error {
}
defer file.Close()

if a.outputFileMode != "" {
fileMode, err := strconv.ParseInt(a.outputFileMode, 0, 32)
if err != nil {
return fmt.Errorf("error parsing output_file_mode value: %s", a.outputFileMode)
}
header.Mode = fileMode
fileInfo, err := file.Stat()
if err != nil {
return fmt.Errorf("could not get file info for '%s', got error '%w'", filePath, err)
}

header.Mode = int64(fileInfo.Mode())
header.ModTime = fileInfo.ModTime()

err = a.tarWriter.WriteHeader(header)
if err != nil {
return fmt.Errorf("could not write header for file '%s', got error '%w'", filePath, err)
Expand All @@ -295,12 +305,8 @@ func (a *TarArchiver) addContent(content []byte, header *tar.Header) error {
return errors.New("tar.Header is nil")
}

if a.outputFileMode != "" {
filemode, err := strconv.ParseInt(a.outputFileMode, 0, 32)
if err != nil {
return fmt.Errorf("error parsing output_file_mode value: %s", a.outputFileMode)
}
header.Mode = filemode
if header.Mode == 0 {
header.Mode = int64(0644)
}

if err := a.tarWriter.WriteHeader(header); err != nil {
Expand All @@ -314,3 +320,12 @@ func (a *TarArchiver) addContent(content []byte, header *tar.Header) error {

return nil
}

func parseFileMode(modeStr string) (os.FileMode, error) {
modeUint, err := strconv.ParseUint(modeStr, 8, 32)
if err != nil {
return 0, fmt.Errorf("invalid file mode '%s': %w", modeStr, err)
}

return os.FileMode(modeUint), nil
}