Skip to content

Commit 691c82c

Browse files
morphisniemeyer
authored andcommitted
many: fix review comments from PR canonical#3177 (canonical#3244)
1 parent ae8edbe commit 691c82c

File tree

7 files changed

+38
-50
lines changed

7 files changed

+38
-50
lines changed

Diff for: cmd/snap/cmd_run.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func migrateXauthority(info *snap.Info) (string, error) {
272272
// it point to exactly the file we just opened (no symlinks,
273273
// no funny "../.." etc)
274274
if fin.Name() != xauthPathCan {
275-
logger.Noticef("WARNING: %s != %s\n", fin.Name(), xauthPathCan)
275+
logger.Noticef("WARNING: XAUTHORITY environment value is not a clean path: %q", xauthPathCan)
276276
return "", nil
277277
}
278278

@@ -297,7 +297,7 @@ func migrateXauthority(info *snap.Info) (string, error) {
297297
}
298298
sys := fi.Sys()
299299
if sys == nil {
300-
return "", fmt.Errorf(i18n.G("Can't validate file owner"))
300+
return "", fmt.Errorf(i18n.G("cannot validate owner of file %s"), fin.Name())
301301
}
302302
// cheap comparison as the current uid is only available as a string
303303
// but it is better to convert the uid from the stat result to a
@@ -318,14 +318,13 @@ func migrateXauthority(info *snap.Info) (string, error) {
318318
if fout, err = os.Open(targetPath); err != nil {
319319
return "", err
320320
}
321-
if osutil.FileStreamsEqual(fin, fout) {
321+
if osutil.StreamsEqual(fin, fout) {
322322
fout.Close()
323323
return targetPath, nil
324324
}
325325

326326
fout.Close()
327327
if err := os.Remove(targetPath); err != nil {
328-
logger.Noticef("WARNING: failed to remove existing file %s", targetPath)
329328
return "", err
330329
}
331330

@@ -338,9 +337,8 @@ func migrateXauthority(info *snap.Info) (string, error) {
338337
// To guard against setting XAUTHORITY to non-xauth files, check
339338
// that we have a valid Xauthority. Specifically, the file must be
340339
// parseable as an Xauthority file and not be empty.
341-
if err := x11.ValidateXauthorityFromFile(fin); err != nil {
342-
logger.Noticef("WARNING: invalid Xauthority file: %s", err)
343-
return "", nil
340+
if err := x11.ValidateXauthority(fin); err != nil {
341+
return "", err
344342
}
345343

346344
// Read data from the beginning of the file
@@ -357,9 +355,9 @@ func migrateXauthority(info *snap.Info) (string, error) {
357355
// Read and write validated Xauthority file to its right location
358356
if _, err = io.Copy(fout, fin); err != nil {
359357
if err := os.Remove(targetPath); err != nil {
360-
logger.Noticef("WARNING: failed to remove file at %s", targetPath)
358+
logger.Noticef("WARNING: cannot remove file at %s: %s", targetPath, err)
361359
}
362-
return "", fmt.Errorf(i18n.G("Failed to write new Xauthority file at %s"), targetPath)
360+
return "", fmt.Errorf(i18n.G("cannot write new Xauthority file at %s: %s"), targetPath, err)
363361
}
364362

365363
return targetPath, nil

Diff for: cmd/snap/cmd_run_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,6 @@ func (s *SnapSuite) TestSnapRunXauthorityMigration(c *check.C) {
563563
c.Assert(err, check.IsNil)
564564
c.Assert(info.Mode().Perm(), check.Equals, os.FileMode(0600))
565565

566-
err = x11.ValidateXauthority(expectedXauthPath)
566+
err = x11.ValidateXauthorityFile(expectedXauthPath)
567567
c.Assert(err, check.IsNil)
568568
}

Diff for: osutil/cmp.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ func FilesAreEqual(a, b string) bool {
5858
return false
5959
}
6060

61-
return FileStreamsEqual(fa, fb)
61+
return StreamsEqual(fa, fb)
6262
}
6363

64-
// FileStreamsEqual compares two file streams and returns true if both
64+
// StreamsEqual compares two streams and returns true if both
6565
// have the same content.
66-
func FileStreamsEqual(fa, fb io.Reader) bool {
66+
func StreamsEqual(a, b io.Reader) bool {
6767
bufa := make([]byte, bufsz)
6868
bufb := make([]byte, bufsz)
6969
for {
70-
ra, erra := io.ReadAtLeast(fa, bufa, bufsz)
71-
rb, errb := io.ReadAtLeast(fb, bufb, bufsz)
70+
ra, erra := io.ReadAtLeast(a, bufa, bufsz)
71+
rb, errb := io.ReadAtLeast(b, bufb, bufsz)
7272
if erra == io.EOF && errb == io.EOF {
7373
return true
7474
}

Diff for: osutil/cmp_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ func (ts *CmpTestSuite) TestCmpStreams(c *C) {
9696
{"hello", "world", false},
9797
{"hello", "hell", false},
9898
} {
99-
c.Assert(FileStreamsEqual(strings.NewReader(x.a), strings.NewReader(x.b)), Equals, x.r)
99+
c.Assert(StreamsEqual(strings.NewReader(x.a), strings.NewReader(x.b)), Equals, x.r)
100100
}
101101
}

Diff for: snap/snapenv/snapenv.go

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import (
3434
//
3535
// It merges it with the existing os.Environ() and ensures the SNAP_*
3636
// overrides the any pre-existing environment variables.
37+
//
38+
// With the extra parameter additional environment variables can be
39+
// supplied which will be set in the execution environment.
3740
func ExecEnv(info *snap.Info, extra map[string]string) []string {
3841
// merge environment and the snap environment, note that the
3942
// snap environment overrides pre-existing env entries

Diff for: x11/xauth.go

+16-29
Original file line numberDiff line numberDiff line change
@@ -38,61 +38,48 @@ type xauth struct {
3838
Data []byte
3939
}
4040

41-
func readBytes(f *os.File, data []byte) error {
42-
n, err := f.Read(data)
43-
if err != nil {
44-
return err
45-
}
46-
47-
if n != len(data) {
48-
return fmt.Errorf("Could not read enough bytes")
49-
}
50-
51-
return nil
52-
}
53-
54-
func readChunk(f *os.File) ([]byte, error) {
55-
// A chunk consists of a length encoded by two bytes and
56-
// additional data which is the real value of the item
57-
// we reading here from the file.
41+
func readChunk(r io.Reader) ([]byte, error) {
42+
// A chunk consists of a length encoded by two bytes (so max 64K)
43+
// and additional data which is the real value of the item we're
44+
// reading here from the file.
5845

5946
b := [2]byte{}
60-
if err := readBytes(f, b[:]); err != nil {
47+
if _, err := io.ReadFull(r, b[:]); err != nil {
6148
return nil, err
6249
}
6350

6451
size := int(binary.BigEndian.Uint16(b[:]))
6552
chunk := make([]byte, size)
66-
if err := readBytes(f, chunk); err != nil {
53+
if _, err := io.ReadFull(r, chunk); err != nil {
6754
return nil, err
6855
}
6956

7057
return chunk, nil
7158
}
7259

73-
func (xa *xauth) readFromFile(f *os.File) error {
60+
func (xa *xauth) readFromFile(r io.Reader) error {
7461
b := [2]byte{}
75-
if err := readBytes(f, b[:]); err != nil {
62+
if _, err := io.ReadFull(r, b[:]); err != nil {
7663
return err
7764
}
7865
// The family field consists of two bytes
7966
xa.Family = binary.BigEndian.Uint16(b[:])
8067

8168
var err error
8269

83-
if xa.Address, err = readChunk(f); err != nil {
70+
if xa.Address, err = readChunk(r); err != nil {
8471
return err
8572
}
8673

87-
if xa.Number, err = readChunk(f); err != nil {
74+
if xa.Number, err = readChunk(r); err != nil {
8875
return err
8976
}
9077

91-
if xa.Name, err = readChunk(f); err != nil {
78+
if xa.Name, err = readChunk(r); err != nil {
9279
return err
9380
}
9481

95-
if xa.Data, err = readChunk(f); err != nil {
82+
if xa.Data, err = readChunk(r); err != nil {
9683
return err
9784
}
9885

@@ -101,22 +88,22 @@ func (xa *xauth) readFromFile(f *os.File) error {
10188

10289
// ValidateXauthority validates a given Xauthority file. The file is valid
10390
// if it can be parsed and contains at least one cookie.
104-
func ValidateXauthority(path string) error {
91+
func ValidateXauthorityFile(path string) error {
10592
f, err := os.Open(path)
10693
if err != nil {
10794
return err
10895
}
10996
defer f.Close()
110-
return ValidateXauthorityFromFile(f)
97+
return ValidateXauthority(f)
11198
}
11299

113100
// ValidateXauthority validates a given Xauthority file. The file is valid
114101
// if it can be parsed and contains at least one cookie.
115-
func ValidateXauthorityFromFile(f *os.File) error {
102+
func ValidateXauthority(r io.Reader) error {
116103
cookies := 0
117104
for {
118105
xa := &xauth{}
119-
err := xa.readFromFile(f)
106+
err := xa.readFromFile(r)
120107
if err == io.EOF {
121108
break
122109
} else if err != nil {

Diff for: x11/xauth_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type xauthTestSuite struct{}
3737
var _ = Suite(&xauthTestSuite{})
3838

3939
func (s *xauthTestSuite) TestXauthFileNotAvailable(c *C) {
40-
err := x11.ValidateXauthority("/does/not/exist")
40+
err := x11.ValidateXauthorityFile("/does/not/exist")
4141
c.Assert(err, NotNil)
4242
}
4343

@@ -46,7 +46,7 @@ func (s *xauthTestSuite) TestXauthFileExistsButIsEmpty(c *C) {
4646
c.Assert(err, IsNil)
4747
defer os.Remove(xauthPath)
4848

49-
err = x11.ValidateXauthority(xauthPath)
49+
err = x11.ValidateXauthorityFile(xauthPath)
5050
c.Assert(err, ErrorMatches, "Xauthority file is invalid")
5151
}
5252

@@ -60,15 +60,15 @@ func (s *xauthTestSuite) TestXauthFileExistsButHasInvalidContent(c *C) {
6060
c.Assert(err, IsNil)
6161
c.Assert(n, Equals, len(data))
6262

63-
err = x11.ValidateXauthority(f.Name())
64-
c.Assert(err, ErrorMatches, "Could not read enough bytes")
63+
err = x11.ValidateXauthorityFile(f.Name())
64+
c.Assert(err, ErrorMatches, "unexpected EOF")
6565
}
6666

6767
func (s *xauthTestSuite) TestValidXauthFile(c *C) {
6868
for _, n := range []int{1, 2, 4} {
6969
path, err := x11.MockXauthority(n)
7070
c.Assert(err, IsNil)
71-
err = x11.ValidateXauthority(path)
71+
err = x11.ValidateXauthorityFile(path)
7272
c.Assert(err, IsNil)
7373
}
7474
}

0 commit comments

Comments
 (0)