forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
osutil, interfaces/apparmor: add and use of osutil.UnlinkMany
This adds `osutil.UnlinkMany`, which is mostly just a convenience function to remove multiple files form a single directory. It also uses it from interfaces/apparmor.
- Loading branch information
Showing
5 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2018 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package osutil | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// UnlinkMany removes multiple files from a single directory. | ||
// | ||
// If dirname is not a directory, this will fail. | ||
// | ||
// This will abort at the first removal error (but ENOENT is ignored). | ||
// | ||
// Filenames must refer to files. They don't necessarily have to be | ||
// relative paths to the given dirname, but if they aren't why are you | ||
// using this function? | ||
// | ||
// Errors are *os.PathError, for convenience | ||
func UnlinkMany(dirname string, filenames []string) error { | ||
dirfd, err := syscall.Open(dirname, syscall.O_RDONLY|syscall.O_CLOEXEC|syscall.O_DIRECTORY|0x200000, 0) | ||
if err != nil { | ||
return &os.PathError{ | ||
Op: "open", | ||
Path: dirname, | ||
Err: err, | ||
} | ||
} | ||
defer syscall.Close(dirfd) | ||
|
||
for _, filename := range filenames { | ||
if err = sysUnlinkat(dirfd, filename); err != nil && err != syscall.ENOENT { | ||
return &os.PathError{ | ||
Op: "remove", | ||
Path: filename, | ||
Err: err, | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2018 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package osutil | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// sys/unix _does_ expose flags, so we need to wrap it | ||
func sysUnlinkat(dirfd int, path string) error { | ||
return unix.Unlinkat(dirfd, path, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2018 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package osutil | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
// syscall.Unlinkat calls unlinkat(2) _without_ AT_REMOVEDIR, which is exactly what we want | ||
var sysUnlinkat = syscall.Unlinkat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2018 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package osutil_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
|
||
"gopkg.in/check.v1" | ||
|
||
"github.com/snapcore/snapd/osutil" | ||
) | ||
|
||
type unlinkSuite struct { | ||
d string | ||
} | ||
|
||
var _ = check.Suite(&unlinkSuite{}) | ||
|
||
func (s *unlinkSuite) checkDirnames(c *check.C, names []string) { | ||
dir, err := os.Open(s.d) | ||
c.Assert(err, check.IsNil) | ||
defer dir.Close() | ||
found, err := dir.Readdirnames(-1) | ||
c.Assert(err, check.IsNil) | ||
sort.Strings(names) | ||
sort.Strings(found) | ||
c.Check(found, check.DeepEquals, names) | ||
} | ||
|
||
func (s *unlinkSuite) SetUpTest(c *check.C) { | ||
s.d = c.MkDir() | ||
s.mkFixture(c) | ||
} | ||
|
||
func (s *unlinkSuite) mkFixture(c *check.C) { | ||
for _, fname := range []string{"foo", "bar", "baz", "quux"} { | ||
f, err := os.Create(filepath.Join(s.d, fname)) | ||
if err == nil { | ||
f.Close() | ||
} else if !os.IsExist(err) { | ||
c.Fatal(err) | ||
} | ||
} | ||
if err := os.Mkdir(filepath.Join(s.d, "dir"), 0700); err != nil && !os.IsExist(err) { | ||
c.Fatal(err) | ||
} | ||
} | ||
|
||
func (s *unlinkSuite) TestUnlinkMany(c *check.C) { | ||
c.Assert(osutil.UnlinkMany(s.d, []string{"bar", "does-not-exist", "baz"}), check.IsNil) | ||
|
||
s.checkDirnames(c, []string{"foo", "quux", "dir"}) | ||
} | ||
|
||
func (s *unlinkSuite) TestUnlinkManyFails(c *check.C) { | ||
type T struct { | ||
dirname string | ||
filenames []string | ||
expected string | ||
} | ||
tests := []T{ | ||
{ | ||
dirname: filepath.Join(s.d, "does-not-exist"), | ||
filenames: []string{"bar", "baz"}, | ||
expected: `open /tmp/.*/does-not-exist: no such file or directory`, | ||
}, { | ||
dirname: filepath.Join(s.d, "foo"), | ||
filenames: []string{"bar", "baz"}, | ||
expected: `open /tmp/.*/foo: not a directory`, | ||
}, { | ||
dirname: s.d, | ||
filenames: []string{"bar", "dir", "baz"}, | ||
expected: `remove dir: is a directory`, | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
c.Check(osutil.UnlinkMany(test.dirname, test.filenames), check.ErrorMatches, test.expected, check.Commentf("%d", i)) | ||
s.mkFixture(c) | ||
} | ||
} |