MCPcopy Create free account
hub / github.com/apache/openwhisk-cli / unpackTar

Function unpackTar

commands/util.go:811–891  ·  view source on GitHub ↗
(inpath string)

Source from the content-addressed store, hash-verified

809}
810
811func unpackTar(inpath string) error {
812 exists, err := FileExists(inpath)
813
814 if err != nil {
815 return err
816 }
817
818 if !exists {
819 errMsg := wski18n.T("File '{{.name}}' is not a valid file or it does not exist",
820 map[string]interface{}{
821 "name": inpath,
822 })
823 whiskErr := whisk.MakeWskErrorFromWskError(errors.New(errMsg), err, whisk.EXIT_CODE_ERR_USAGE,
824 whisk.DISPLAY_MSG, whisk.DISPLAY_USAGE)
825
826 return whiskErr
827 }
828
829 tarFileReader, err := os.Open(inpath)
830 if err != nil {
831 whisk.Debug(whisk.DbgError, "os.Open(%s) failed: %s\n", inpath, err)
832 errStr := wski18n.T("Error opening tar file '{{.name}}': {{.err}}",
833 map[string]interface{}{"name": inpath, "err": err})
834 werr := whisk.MakeWskError(errors.New(errStr), whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
835 return werr
836 }
837 defer tarFileReader.Close()
838
839 // Loop through the files in the tarfile
840 tReader := tar.NewReader(tarFileReader)
841 for {
842 item, err := tReader.Next()
843 if err == io.EOF {
844 whisk.Debug(whisk.DbgError, "EOF reach during untar\n")
845 break // end of tar
846 }
847 if err != nil {
848 whisk.Debug(whisk.DbgError, "tReader.Next() failed: %s\n", err)
849 errStr := wski18n.T("Error reading tar file '{{.name}}': {{.err}}",
850 map[string]interface{}{"name": inpath, "err": err})
851 werr := whisk.MakeWskError(errors.New(errStr), whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
852 return werr
853 }
854
855 whisk.Debug(whisk.DbgInfo, "tar file item - %#v\n", item)
856 switch item.Typeflag {
857 case tar.TypeDir:
858 if err := os.MkdirAll(item.Name, os.FileMode(item.Mode)); err != nil {
859 whisk.Debug(whisk.DbgError, "os.MkdirAll(%s, %d) failed: %s\n", item.Name, item.Mode, err)
860 errStr := wski18n.T("Unable to create directory '{{.dir}}' while untarring '{{.name}}': {{.err}}",
861 map[string]interface{}{"dir": item.Name, "name": inpath, "err": err})
862 werr := whisk.MakeWskError(errors.New(errStr), whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
863 return werr
864 }
865 case tar.TypeReg:
866 untarFile, err := os.OpenFile(item.Name, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(item.Mode))
867 defer untarFile.Close()
868 if err != nil {

Callers 1

sdkInstallFunction · 0.85

Calls 1

FileExistsFunction · 0.85

Tested by

no test coverage detected