Merge "jiri: cd back to original directory before running jiri binary."
diff --git a/googlesource/googlesource.go b/googlesource/googlesource.go
index cb0e6c7..949f7a2 100644
--- a/googlesource/googlesource.go
+++ b/googlesource/googlesource.go
@@ -82,10 +82,14 @@
 	if err != nil {
 		return cookies
 	}
+	return parseCookieFile(jirix, bytes)
+}
 
+func parseCookieFile(jirix *jiri.X, bytes []byte) (cookies []*http.Cookie) {
 	lines := strings.Split(string(bytes), "\n")
+
 	for _, line := range lines {
-		if strings.TrimSpace(line) == "" {
+		if strings.TrimSpace(line) == "" || line[0] == '#' {
 			continue
 		}
 		cookie, err := parseCookie(line)
@@ -95,7 +99,7 @@
 			cookies = append(cookies, cookie)
 		}
 	}
-	return cookies
+	return
 }
 
 // GetRepoStatuses returns the RepoStatus of all public projects hosted on the
diff --git a/googlesource/googlesource_test.go b/googlesource/googlesource_test.go
index e6e0a05..63860ec 100644
--- a/googlesource/googlesource_test.go
+++ b/googlesource/googlesource_test.go
@@ -66,3 +66,35 @@
 		t.Errorf("expected parseCookie(%q) to return error but it did not", s)
 	}
 }
+
+type cookieFileTests struct {
+	fileContents []byte
+	cookies      []http.Cookie
+}
+
+func TestParseCookieFile(t *testing.T) {
+	tests := []cookieFileTests{{
+		fileContents: []byte(fmt.Sprintf("\n# this is a comment\n%s\t%s\t%s\t%s\t%d\t%s\t%s", ".example.com", "FALSE", "/", "FALSE", 0, "name", "value")),
+		cookies: []http.Cookie{{
+			Domain:  ".example.com",
+			Path:    "/",
+			Secure:  false,
+			Expires: time.Unix(0, 0),
+			Name:    "name",
+			Value:   "value",
+		}},
+	}}
+
+	for testIdx, test := range tests {
+		actual := parseCookieFile(nil, test.fileContents)
+		if len(actual) != len(test.cookies) {
+			t.Errorf("expected to parse %v cookies but got %v on test case %v", len(test.cookies), len(actual), testIdx)
+		}
+		for i, got := range actual {
+			want := test.cookies[i]
+			if !reflect.DeepEqual(*got, want) {
+				t.Errorf("expected test case %v cookie %v to be %#v but got %#v", testIdx, i, want, *got)
+			}
+		}
+	}
+}
diff --git a/profiles/profilesutil/util.go b/profiles/profilesutil/util.go
index 36f8065..384392a 100644
--- a/profiles/profilesutil/util.go
+++ b/profiles/profilesutil/util.go
@@ -153,8 +153,10 @@
 	if err != nil {
 		return err
 	}
-	_, err = s.Copy(file, resp.Body)
-	return err
+	if _, err := s.Copy(file, resp.Body); err != nil {
+		return err
+	}
+	return file.Close()
 }
 
 // Unzip unzips the file in srcFile and puts resulting files in directory dstDir.
diff --git a/runutil/.api b/runutil/.api
index 5e46364..e24cb58 100644
--- a/runutil/.api
+++ b/runutil/.api
@@ -16,7 +16,7 @@
 pkg runutil, method (Sequence) Capture(io.Writer, io.Writer) Sequence
 pkg runutil, method (Sequence) Chdir(string) Sequence
 pkg runutil, method (Sequence) Chmod(string, os.FileMode) Sequence
-pkg runutil, method (Sequence) Copy(*os.File, io.Reader) (int64, error)
+pkg runutil, method (Sequence) Copy(io.Writer, io.Reader) (int64, error)
 pkg runutil, method (Sequence) Create(string) (*os.File, error)
 pkg runutil, method (Sequence) Dir(string) Sequence
 pkg runutil, method (Sequence) Done() error
diff --git a/runutil/sequence.go b/runutil/sequence.go
index 3c62df7..0abd162 100644
--- a/runutil/sequence.go
+++ b/runutil/sequence.go
@@ -828,7 +828,6 @@
 // Create is a wrapper around os.Create that handles options such as "verbose"
 // or "dry run". Create is a terminating function.
 func (s Sequence) Create(name string) (f *os.File, err error) {
-
 	if s.err != nil {
 		return nil, s.Done()
 	}
@@ -888,14 +887,14 @@
 
 // Copy is a wrapper around io.Copy that handles options such as "verbose" or
 // "dry run". Copy is a terminating function.
-func (s Sequence) Copy(dst *os.File, src io.Reader) (n int64, err error) {
+func (s Sequence) Copy(dst io.Writer, src io.Reader) (n int64, err error) {
 	if s.err != nil {
 		return 0, s.Done()
 	}
 	s.r.call(func() error {
 		n, err = io.Copy(dst, src)
 		return err
-	}, fmt.Sprintf("io.copy %q", dst.Name()))
+	}, "io.copy")
 	s.setError(err, fmt.Sprintf("Copy(%s, %s)", dst, src))
 	err = s.Done()
 	return