Add a --symlinks argument to soong_zip

Add a --symlinks argument that defaults to true to soong_zip.
Passing --symlinks=false will cause it to follow symlinks instead
of storing them in the zip file.

Relands I4deb98daa9d4ba9f94e3d7670c117fe00381d2ba with tests.

Bug: 112843624
Test: glob_test.go
Test: zip_test.go
Test: m checkbuild
Change-Id: I0eff9c1f2dba79e873fda381ff585df55d5aaaad
diff --git a/zip/zip.go b/zip/zip.go
index 7eebf06..d8507df 100644
--- a/zip/zip.go
+++ b/zip/zip.go
@@ -188,6 +188,8 @@
 	compressorPool sync.Pool
 	compLevel      int
 
+	followSymlinks pathtools.ShouldFollowSymlinks
+
 	fs pathtools.FileSystem
 }
 
@@ -212,7 +214,9 @@
 	NumParallelJobs          int
 	NonDeflatedFiles         map[string]bool
 	WriteIfChanged           bool
-	Filesystem               pathtools.FileSystem
+	StoreSymlinks            bool
+
+	Filesystem pathtools.FileSystem
 }
 
 const NOQUOTE = '\x00'
@@ -263,13 +267,17 @@
 		args.AddDirectoryEntriesToZip = true
 	}
 
+	// Have Glob follow symlinks if they are not being stored as symlinks in the zip file.
+	followSymlinks := pathtools.ShouldFollowSymlinks(!args.StoreSymlinks)
+
 	z := &ZipWriter{
-		time:         jar.DefaultTime,
-		createdDirs:  make(map[string]string),
-		createdFiles: make(map[string]string),
-		directories:  args.AddDirectoryEntriesToZip,
-		compLevel:    args.CompressionLevel,
-		fs:           args.Filesystem,
+		time:           jar.DefaultTime,
+		createdDirs:    make(map[string]string),
+		createdFiles:   make(map[string]string),
+		directories:    args.AddDirectoryEntriesToZip,
+		compLevel:      args.CompressionLevel,
+		followSymlinks: followSymlinks,
+		fs:             args.Filesystem,
 	}
 
 	if z.fs == nil {
@@ -288,7 +296,7 @@
 				continue
 			}
 
-			globbed, _, err := z.fs.Glob(s, nil, pathtools.DontFollowSymlinks)
+			globbed, _, err := z.fs.Glob(s, nil, followSymlinks)
 			if err != nil {
 				return err
 			}
@@ -317,7 +325,7 @@
 					Err:  syscall.ENOTDIR,
 				}
 			}
-			globbed, _, err := z.fs.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, pathtools.DontFollowSymlinks)
+			globbed, _, err := z.fs.Glob(filepath.Join(fa.GlobDir, "**/*"), nil, followSymlinks)
 			if err != nil {
 				return err
 			}
@@ -559,7 +567,15 @@
 	var fileSize int64
 	var executable bool
 
-	if s, err := z.fs.Lstat(src); err != nil {
+	var s os.FileInfo
+	var err error
+	if z.followSymlinks {
+		s, err = z.fs.Stat(src)
+	} else {
+		s, err = z.fs.Lstat(src)
+	}
+
+	if err != nil {
 		return err
 	} else if s.IsDir() {
 		if z.directories {