fileslist: hash the content of symlink, not the file it points to.
IsDir() doesn't handle the case where the file is a symlink to a
directory, which cause fileslist to crash.
The hash is used to validate whether system image is the same, so
hashing the content of symlink makes more sense.
Bug: 36274890
Test: joule builds
Change-Id: I6359418a5b28f8da13f85b01a30a72228fecf4ce
diff --git a/cmd/fileslist/fileslist.go b/cmd/fileslist/fileslist.go
index 1cf948f..56ea66d 100755
--- a/cmd/fileslist/fileslist.go
+++ b/cmd/fileslist/fileslist.go
@@ -64,18 +64,26 @@
n.Size = n.stat.Size()
// Calculate SHA256.
- f, err := os.Open(n.path)
- if err != nil {
- // If the file can't be read, it's probably a symlink to an absolute path...
- // Returns the following to mimic the behavior of fileslist.py.
- n.SHA256 = "----------------------------------------------------------------"
- return true
- }
- defer f.Close()
-
h := sha256.New()
- if _, err := io.Copy(h, f); err != nil {
- panic(err)
+ if n.stat.Mode()&os.ModeSymlink == 0 {
+ f, err := os.Open(n.path)
+ if err != nil {
+ panic(err)
+ }
+ defer f.Close()
+
+ if _, err := io.Copy(h, f); err != nil {
+ panic(err)
+ }
+ } else {
+ // Hash the content of symlink, not the file it points to.
+ s, err := os.Readlink(n.path)
+ if err != nil {
+ panic(err)
+ }
+ if _, err := io.WriteString(h, s); err != nil {
+ panic(err)
+ }
}
n.SHA256 = fmt.Sprintf("%x", h.Sum(nil))
return true