Add 'force' to LinkFromPool method: overwrite file even if exists and different content. #90

This commit is contained in:
Andrey Smirnov
2014-08-05 14:43:23 +04:00
parent 625a38c578
commit da6d5b7cf8
6 changed files with 99 additions and 10 deletions
+13 -4
View File
@@ -79,7 +79,8 @@ func (storage *PublishedStorage) RemoveDirs(path string, progress aptly.Progress
// sourcePath is filepath to package file in package pool
//
// LinkFromPool returns relative path for the published file to be included in package index
func (storage *PublishedStorage) LinkFromPool(publishedDirectory string, sourcePool aptly.PackagePool, sourcePath, sourceMD5 string) error {
func (storage *PublishedStorage) LinkFromPool(publishedDirectory string, sourcePool aptly.PackagePool,
sourcePath, sourceMD5 string, force bool) error {
// verify that package pool is local pool is filesystem pool
_ = sourcePool.(*PackagePool)
@@ -105,10 +106,18 @@ func (storage *PublishedStorage) LinkFromPool(publishedDirectory string, sourceP
srcSys := srcStat.Sys().(*syscall.Stat_t)
dstSys := dstStat.Sys().(*syscall.Stat_t)
if srcSys.Ino != dstSys.Ino {
return fmt.Errorf("error linking file to %s: file already exists and is different", filepath.Join(poolPath, baseName))
if srcSys.Ino == dstSys.Ino {
return nil
}
if !force {
return fmt.Errorf("error linking file to %s: file already exists and is different", filepath.Join(poolPath, baseName))
} else {
err = os.Remove(filepath.Join(poolPath, baseName))
if err != nil {
return err
}
}
return nil
}
return os.Link(sourcePath, filepath.Join(poolPath, baseName))
+18 -2
View File
@@ -153,7 +153,7 @@ func (s *PublishedStorageSuite) TestLinkFromPool(c *C) {
err = ioutil.WriteFile(t.sourcePath, []byte("Contents"), 0644)
c.Assert(err, IsNil)
err = s.storage.LinkFromPool(filepath.Join(t.prefix, "pool", t.component, t.poolDirectory), pool, t.sourcePath, "")
err = s.storage.LinkFromPool(filepath.Join(t.prefix, "pool", t.component, t.poolDirectory), pool, t.sourcePath, "", false)
c.Assert(err, IsNil)
st, err := os.Stat(filepath.Join(s.storage.rootPath, t.prefix, t.expectedFilename))
@@ -171,6 +171,22 @@ func (s *PublishedStorageSuite) TestLinkFromPool(c *C) {
err = ioutil.WriteFile(sourcePath, []byte("Contents"), 0644)
c.Assert(err, IsNil)
err = s.storage.LinkFromPool(filepath.Join("", "pool", "main", "m/mars-invaders"), pool, sourcePath, "")
err = s.storage.LinkFromPool(filepath.Join("", "pool", "main", "m/mars-invaders"), pool, sourcePath, "", false)
c.Check(err, ErrorMatches, ".*file already exists and is different")
st, err := os.Stat(sourcePath)
c.Assert(err, IsNil)
info := st.Sys().(*syscall.Stat_t)
c.Check(int(info.Nlink), Equals, 1)
// linking with force
err = s.storage.LinkFromPool(filepath.Join("", "pool", "main", "m/mars-invaders"), pool, sourcePath, "", true)
c.Check(err, IsNil)
st, err = os.Stat(sourcePath)
c.Assert(err, IsNil)
info = st.Sys().(*syscall.Stat_t)
c.Check(int(info.Nlink), Equals, 2)
}