From b386489b1a1fb752c68b4011eba3c5f9c6686ae9 Mon Sep 17 00:00:00 2001 From: Pagran <67878280+pagran@users.noreply.github.com> Date: Tue, 20 Oct 2020 01:05:43 +0300 Subject: [PATCH] Add test Add comment Debug dir cleanup --- import_obfuscation.go | 80 ++++++++++++++++++++--------------- main.go | 9 ++-- testdata/scripts/debugdir.txt | 10 +++++ 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/import_obfuscation.go b/import_obfuscation.go index ea1aa30..7fddad6 100644 --- a/import_obfuscation.go +++ b/import_obfuscation.go @@ -71,56 +71,66 @@ func extractDebugObfSrc(pkgPath string, pkg *goobj2.Package) error { if envGarbleDebugDir == "" { return nil } + + var archiveMember *goobj2.ArchiveMember for _, member := range pkg.ArchiveMembers { - if member.ArchiveHeader.Name != garbleSrcHeaderName { - continue + if member.ArchiveHeader.Name == garbleSrcHeaderName { + archiveMember = &member + break } + } - osPkgPath := filepath.FromSlash(pkgPath) - pkgDebugDir := filepath.Join(envGarbleDebugDir, osPkgPath) - if err := os.MkdirAll(pkgDebugDir, 0o755); err != nil { - return err - } + if archiveMember == nil { + return nil + } - archiveSize, err := strconv.Atoi(member.ArchiveHeader.Date) + osPkgPath := filepath.FromSlash(pkgPath) + pkgDebugDir := filepath.Join(envGarbleDebugDir, osPkgPath) + if err := os.MkdirAll(pkgDebugDir, 0o755); err != nil { + return err + } + + archiveSize, err := strconv.Atoi(archiveMember.ArchiveHeader.Date) + if err != nil { + return err + } + + archiveBytes := archiveMember.ArchiveHeader.Data[:archiveSize] + + archive := bytes.NewBuffer(archiveBytes) + gzipReader, err := gzip.NewReader(archive) + if err != nil { + return err + } + defer gzipReader.Close() + tarReader := tar.NewReader(gzipReader) + + for { + header, err := tarReader.Next() + if err == io.EOF { + return nil + } if err != nil { return err } - archiveBytes := member.ArchiveHeader.Data[:archiveSize] - - archive := bytes.NewBuffer(archiveBytes) - gzipReader, err := gzip.NewReader(archive) + debugFilePath := filepath.Join(pkgDebugDir, header.Name) + debugFile, err := os.Create(debugFilePath) if err != nil { return err } - defer gzipReader.Close() - tarReader := tar.NewReader(gzipReader) - - for { - header, err := tarReader.Next() - if err == io.EOF { - return nil - } - if err != nil { - return err - } + if _, err := io.Copy(debugFile, tarReader); err != nil { + return err + } + debugFile.Close() - debugFilePath := filepath.Join(pkgDebugDir, header.Name) - debugFile, err := os.Create(debugFilePath) - if err != nil { - return err - } - if _, err := io.Copy(debugFile, tarReader); err != nil { - return err - } - debugFile.Close() + obfuscationTime := header.ModTime.Local() - obfuscationTime := header.ModTime.Local() - os.Chtimes(debugFilePath, obfuscationTime, obfuscationTime) // Restore obfuscation time + // Restore the actual source obfuscation time so as not to mislead the user. + if err := os.Chtimes(debugFilePath, obfuscationTime, obfuscationTime); err != nil { + return err } } - return nil } // obfuscateImports does all the necessary work to replace the import paths of diff --git a/main.go b/main.go index c6cd566..63d85b2 100644 --- a/main.go +++ b/main.go @@ -359,15 +359,13 @@ func mainErr(args []string) error { flagDebugDir = filepath.Join(wd, flagDebugDir) } - if info, err := os.Stat(flagDebugDir); os.IsNotExist(err) { + if err := os.RemoveAll(flagDebugDir); err == nil || os.IsNotExist(err) { err := os.MkdirAll(flagDebugDir, 0o755) if err != nil { return err } - } else if err != nil { + } else { return fmt.Errorf("debugdir error: %v", err) - } else if !info.IsDir() { - return fmt.Errorf("debugdir exists, but is a file not a directory") } } @@ -781,7 +779,8 @@ func transformCompile(args []string) ([]string, error) { ArchiveHeader: goobj2.ArchiveHeader{ Name: garbleSrcHeaderName, Size: int64(obfSrcArchive.Len()), - Date: strconv.Itoa(obfSrcArchive.Len()), // Zero byte bug bypass + // Work around https://github.com/Binject/debug/issues/14 + Date: strconv.Itoa(obfSrcArchive.Len()), Data: obfSrcArchive.Bytes(), }, }) diff --git a/testdata/scripts/debugdir.txt b/testdata/scripts/debugdir.txt index 03a0c34..164d3ad 100644 --- a/testdata/scripts/debugdir.txt +++ b/testdata/scripts/debugdir.txt @@ -1,4 +1,6 @@ env GOPRIVATE=test/main +# Isolate go cache for test reproducibility +env GOCACHE=$WORK/.temp-cache garble -debugdir ./test1 build exists 'test1/test/main/imported/imported.go' 'test1/main/main.go' @@ -6,6 +8,14 @@ exists 'test1/test/main/imported/imported.go' 'test1/main/main.go' ! grep ImportedFunc $WORK/test1/main/main.go ! grep 'some comment' $WORK/test1/main/main.go +[short] stop + +cp $WORK/test1/main/main.go $WORK/test1/some_file_from_prev_build.go + +garble -debugdir ./test1 build -v +! stderr 'test/main' +! exists $WORK/test1/some_file_from_prev_build.go + -- go.mod -- module test/main -- main.go --