mirror of
https://github.com/mudler/luet.git
synced 2025-08-18 07:16:57 +00:00
parent
df14fe60fc
commit
6a1b64acea
@ -24,6 +24,37 @@ import (
|
|||||||
copy "github.com/otiai10/copy"
|
copy "github.com/otiai10/copy"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func OrderFiles(target string, files []string) ([]string, []string) {
|
||||||
|
|
||||||
|
var newFiles []string
|
||||||
|
var notPresent []string
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
target := filepath.Join(target, f)
|
||||||
|
fi, err := os.Lstat(target)
|
||||||
|
if err != nil {
|
||||||
|
notPresent = append(notPresent, f)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m := fi.Mode(); !m.IsDir() {
|
||||||
|
newFiles = append(newFiles, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
target := filepath.Join(target, f)
|
||||||
|
fi, err := os.Lstat(target)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m := fi.Mode(); m.IsDir() {
|
||||||
|
newFiles = append(newFiles, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newFiles, notPresent
|
||||||
|
}
|
||||||
|
|
||||||
func ListDir(dir string) ([]string, error) {
|
func ListDir(dir string) ([]string, error) {
|
||||||
content := []string{}
|
content := []string{}
|
||||||
|
|
||||||
|
@ -16,6 +16,10 @@
|
|||||||
package helpers_test
|
package helpers_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
. "github.com/mudler/luet/pkg/helpers"
|
. "github.com/mudler/luet/pkg/helpers"
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
@ -28,4 +32,33 @@ var _ = Describe("Helpers", func() {
|
|||||||
Expect(Exists("../../tests/fixtures/buildtree/app-admin/enman/1.4.0/build.yaml.not.exists")).To(BeFalse())
|
Expect(Exists("../../tests/fixtures/buildtree/app-admin/enman/1.4.0/build.yaml.not.exists")).To(BeFalse())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Context("Orders dir and files correctly", func() {
|
||||||
|
It("puts files first and folders at end", func() {
|
||||||
|
testDir, err := ioutil.TempDir(os.TempDir(), "test")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(testDir)
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filepath.Join(testDir, "foo"), []byte("test\n"), 0644)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filepath.Join(testDir, "baz"), []byte("test\n"), 0644)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
err = os.MkdirAll(filepath.Join(testDir, "bar"), 0755)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
err = ioutil.WriteFile(filepath.Join(testDir, "bar", "foo"), []byte("test\n"), 0644)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
err = os.MkdirAll(filepath.Join(testDir, "baz2"), 0755)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
err = ioutil.WriteFile(filepath.Join(testDir, "baz2", "foo"), []byte("test\n"), 0644)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
ordered, notExisting := OrderFiles(testDir, []string{"bar", "baz", "bar/foo", "baz2", "foo", "baz2/foo", "notexisting"})
|
||||||
|
|
||||||
|
Expect(ordered).To(Equal([]string{"baz", "bar/foo", "foo", "baz2/foo", "bar", "baz2"}))
|
||||||
|
Expect(notExisting).To(Equal([]string{"notexisting"}))
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -631,8 +631,10 @@ func (l *LuetInstaller) uninstall(p pkg.Package, s *System) error {
|
|||||||
cp.Map(files)
|
cp.Map(files)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toRemove, notPresent := helpers.OrderFiles(s.Target, files)
|
||||||
|
|
||||||
// Remove from target
|
// Remove from target
|
||||||
for _, f := range files {
|
for _, f := range toRemove {
|
||||||
target := filepath.Join(s.Target, f)
|
target := filepath.Join(s.Target, f)
|
||||||
|
|
||||||
if !config.LuetCfg.ConfigProtectSkip && cp.Protected(f) {
|
if !config.LuetCfg.ConfigProtectSkip && cp.Protected(f) {
|
||||||
@ -650,10 +652,7 @@ func (l *LuetInstaller) uninstall(p pkg.Package, s *System) error {
|
|||||||
|
|
||||||
fi, err := os.Lstat(target)
|
fi, err := os.Lstat(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Warning("File not present in the system target ?", target, err.Error())
|
Warning("File not found (it was before?) ", err.Error())
|
||||||
if err = os.Remove(target); err != nil {
|
|
||||||
Warning("Failed removing file", target, err.Error())
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch mode := fi.Mode(); {
|
switch mode := fi.Mode(); {
|
||||||
@ -663,15 +662,29 @@ func (l *LuetInstaller) uninstall(p pkg.Package, s *System) error {
|
|||||||
Warning("Failed reading folder", target, err.Error())
|
Warning("Failed reading folder", target, err.Error())
|
||||||
}
|
}
|
||||||
if len(files) != 0 {
|
if len(files) != 0 {
|
||||||
Warning("Preserving not-empty folder", target)
|
Debug("Preserving not-empty folder", target)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Remove(target); err != nil {
|
if err = os.Remove(target); err != nil {
|
||||||
Warning("Failed removing file (not present in the system target ?)", target, err.Error())
|
Warning("Failed removing file (maybe not present in the system target anymore ?)", target, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, f := range notPresent {
|
||||||
|
target := filepath.Join(s.Target, f)
|
||||||
|
|
||||||
|
if !config.LuetCfg.ConfigProtectSkip && cp.Protected(f) {
|
||||||
|
Debug("Preserving protected file:", f)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.Remove(target); err != nil {
|
||||||
|
Debug("Failed removing file (not present in the system target)", target, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = s.Database.RemovePackageFiles(p)
|
err = s.Database.RemovePackageFiles(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Failed removing package files from database")
|
return errors.Wrap(err, "Failed removing package files from database")
|
||||||
|
Loading…
Reference in New Issue
Block a user