mirror of
https://github.com/EnumeratedDev/bpm.git
synced 2026-09-16 10:36:12 +00:00
Fix directories not being created with correct permissions and improve debug messages
This commit is contained in:
+39
-20
@@ -16,6 +16,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BPMPackage struct {
|
type BPMPackage struct {
|
||||||
@@ -584,14 +585,25 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string)
|
|||||||
extractFilename := path.Join(rootDir, header.Name)
|
extractFilename := path.Join(rootDir, header.Name)
|
||||||
switch header.Typeflag {
|
switch header.Typeflag {
|
||||||
case tar.TypeDir:
|
case tar.TypeDir:
|
||||||
if err := os.Mkdir(extractFilename, 0755); err != nil {
|
if _, err := os.Stat(extractFilename); err == nil {
|
||||||
if !os.IsExist(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Creating Directory: " + extractFilename)
|
fmt.Printf("Skipping Directory: %s (Directory already exists)\n", extractFilename)
|
||||||
}
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Mkdir(extractFilename, 0755); err != nil && !os.IsExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Using syscall instead of os.Chmod because it seems to strip the setuid, setgid and sticky bits
|
||||||
|
err := syscall.Chmod(extractFilename, uint32(header.Mode))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if verbose {
|
||||||
|
fmt.Printf("Created directory %s (%o)\n", extractFilename, header.Mode)
|
||||||
}
|
}
|
||||||
case tar.TypeReg:
|
case tar.TypeReg:
|
||||||
skip := false
|
skip := false
|
||||||
@@ -600,7 +612,7 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string)
|
|||||||
if strings.HasSuffix(k, "/") {
|
if strings.HasSuffix(k, "/") {
|
||||||
if strings.HasPrefix(header.Name, k) {
|
if strings.HasPrefix(header.Name, k) {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Skipping File: " + extractFilename + " (Containing directory is set to be kept during reinstalls/updates)")
|
fmt.Printf("Skipping File: %s (Containing directory is set to be kept during reinstalls/updates)\n", extractFilename)
|
||||||
}
|
}
|
||||||
skip = true
|
skip = true
|
||||||
continue
|
continue
|
||||||
@@ -608,7 +620,7 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string)
|
|||||||
} else {
|
} else {
|
||||||
if header.Name == k {
|
if header.Name == k {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Skipping File: " + extractFilename + " (File is configured to be kept during reinstalls/updates)")
|
fmt.Printf("Skipping File: %s (File is configured to be kept during reinstalls/updates)\n", extractFilename)
|
||||||
}
|
}
|
||||||
skip = true
|
skip = true
|
||||||
continue
|
continue
|
||||||
@@ -624,34 +636,40 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
outFile, err := os.Create(extractFilename)
|
outFile, err := os.Create(extractFilename)
|
||||||
if verbose {
|
|
||||||
fmt.Println("Creating File: " + extractFilename)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := io.Copy(outFile, packageFilesReader); err != nil {
|
if _, err := io.Copy(outFile, packageFilesReader); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := os.Chmod(extractFilename, header.FileInfo().Mode()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = outFile.Close()
|
err = outFile.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case tar.TypeSymlink:
|
|
||||||
if verbose {
|
// Using syscall instead of os.Chmod because it seems to strip the setuid, setgid and sticky bits
|
||||||
fmt.Println("Creating Symlink: " + extractFilename + " -> " + header.Linkname)
|
err = syscall.Chmod(extractFilename, uint32(header.Mode))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if verbose {
|
||||||
|
fmt.Printf("Created File: %s (%o)\n", extractFilename, header.Mode)
|
||||||
|
}
|
||||||
|
case tar.TypeSymlink:
|
||||||
err := os.Remove(extractFilename)
|
err := os.Remove(extractFilename)
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Symlink(header.Linkname, extractFilename)
|
err = os.Symlink(header.Linkname, extractFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if verbose {
|
||||||
|
fmt.Println("Created Symlink: " + extractFilename + " -> " + header.Linkname)
|
||||||
|
}
|
||||||
case tar.TypeLink:
|
case tar.TypeLink:
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Detected Hard Link: " + extractFilename + " -> " + path.Join(rootDir, strings.TrimPrefix(header.Linkname, "files/")))
|
fmt.Println("Detected Hard Link: " + extractFilename + " -> " + path.Join(rootDir, strings.TrimPrefix(header.Linkname, "files/")))
|
||||||
@@ -666,13 +684,14 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for extractFilename, destination := range seenHardlinks {
|
for extractFilename, destination := range seenHardlinks {
|
||||||
if verbose {
|
|
||||||
fmt.Println("Creating Hard Link: " + extractFilename + " -> " + path.Join(rootDir, destination))
|
|
||||||
}
|
|
||||||
err := os.Link(path.Join(rootDir, destination), extractFilename)
|
err := os.Link(path.Join(rootDir, destination), extractFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if verbose {
|
||||||
|
fmt.Println("Created Hard Link: " + extractFilename + " -> " + path.Join(rootDir, destination))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer archive.Close()
|
defer archive.Close()
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|||||||
Reference in New Issue
Block a user