@@ -39,67 +39,72 @@ func (ex *RunExecutor) setupFiles(ctx context.Context) error {
3939 homeDir := ex .workingDir
4040 uid := - 1
4141 gid := - 1
42- if ex .jobSpec .User != nil {
43- if ex .jobSpec .User .HomeDir != "" {
44- homeDir = ex .jobSpec .User .HomeDir
45- }
46- if ex .jobSpec .User .Uid != nil {
47- uid = int (* ex .jobSpec .User .Uid )
48- }
49- if ex .jobSpec .User .Gid != nil {
50- gid = int (* ex .jobSpec .User .Gid )
51- }
42+ // User must be already set
43+ if ex .jobSpec .User .HomeDir != "" {
44+ homeDir = ex .jobSpec .User .HomeDir
45+ }
46+ if ex .jobSpec .User .Uid != nil {
47+ uid = int (* ex .jobSpec .User .Uid )
48+ }
49+ if ex .jobSpec .User .Gid != nil {
50+ gid = int (* ex .jobSpec .User .Gid )
5251 }
5352
5453 for _ , fa := range ex .jobSpec .FileArchives {
55- log .Trace (ctx , "Extracting file archive" , "id" , fa .Id , "path" , fa .Path )
56-
57- p := path .Clean (fa .Path )
58- // `~username[/path/to]` is not supported
59- if p == "~" {
60- p = homeDir
61- } else if rest , found := strings .CutPrefix (p , "~/" ); found {
62- p = path .Join (homeDir , rest )
63- } else if ! path .IsAbs (p ) {
64- p = path .Join (ex .workingDir , p )
65- }
66- dir , root := path .Split (p )
67- if err := mkdirAll (ctx , dir , uid , gid ); err != nil {
54+ archivePath := path .Join (ex .archiveDir , fa .Id )
55+ if err := extractFileArchive (ctx , archivePath , fa .Path , ex .workingDir , uid , gid , homeDir ); err != nil {
6856 return gerrors .Wrap (err )
6957 }
58+ }
7059
71- if err := os .RemoveAll (p ); err != nil {
72- log .Warning (ctx , "Failed to remove" , "path" , p , "err" , err )
73- }
60+ if err := os .RemoveAll (ex . archiveDir ); err != nil {
61+ log .Warning (ctx , "Failed to remove file archives dir " , "path" , ex . archiveDir , "err" , err )
62+ }
7463
75- archivePath := path .Join (ex .archiveDir , fa .Id )
76- archive , err := os .Open (archivePath )
77- if err != nil {
78- return gerrors .Wrap (err )
79- }
80- defer func () {
81- _ = archive .Close ()
82- if err := os .Remove (archivePath ); err != nil {
83- log .Warning (ctx , "Failed to remove archive" , "path" , archivePath , "err" , err )
84- }
85- }()
64+ return nil
65+ }
8666
87- var paths []string
88- repl := fmt .Sprintf ("%s$2" , root )
89- renameAndRemember := func (s string ) string {
90- s = renameRegex .ReplaceAllString (s , repl )
91- paths = append (paths , s )
92- return s
93- }
94- if err := extract .Tar (ctx , archive , dir , renameAndRemember ); err != nil {
95- return gerrors .Wrap (err )
96- }
67+ func extractFileArchive (ctx context.Context , archivePath string , targetPath string , targetRoot string , uid int , gid int , homeDir string ) error {
68+ log .Trace (ctx , "Extracting file archive" , "archive" , archivePath , "target" , targetPath )
9769
98- if uid != - 1 || gid != - 1 {
99- for _ , p := range paths {
100- if err := os .Chown (path .Join (dir , p ), uid , gid ); err != nil {
101- log .Warning (ctx , "Failed to chown" , "path" , p , "err" , err )
102- }
70+ targetPath = path .Clean (targetPath )
71+ // `~username[/path/to]` is not supported
72+ if targetPath == "~" {
73+ targetPath = homeDir
74+ } else if rest , found := strings .CutPrefix (targetPath , "~/" ); found {
75+ targetPath = path .Join (homeDir , rest )
76+ } else if ! path .IsAbs (targetPath ) {
77+ targetPath = path .Join (targetRoot , targetPath )
78+ }
79+ dir , root := path .Split (targetPath )
80+ if err := mkdirAll (ctx , dir , uid , gid ); err != nil {
81+ return gerrors .Wrap (err )
82+ }
83+ if err := os .RemoveAll (targetPath ); err != nil {
84+ log .Warning (ctx , "Failed to remove" , "path" , targetPath , "err" , err )
85+ }
86+
87+ archive , err := os .Open (archivePath )
88+ if err != nil {
89+ return gerrors .Wrap (err )
90+ }
91+ defer archive .Close ()
92+
93+ var paths []string
94+ repl := fmt .Sprintf ("%s$2" , root )
95+ renameAndRemember := func (s string ) string {
96+ s = renameRegex .ReplaceAllString (s , repl )
97+ paths = append (paths , s )
98+ return s
99+ }
100+ if err := extract .Tar (ctx , archive , dir , renameAndRemember ); err != nil {
101+ return gerrors .Wrap (err )
102+ }
103+
104+ if uid != - 1 || gid != - 1 {
105+ for _ , p := range paths {
106+ if err := os .Chown (path .Join (dir , p ), uid , gid ); err != nil {
107+ log .Warning (ctx , "Failed to chown" , "path" , p , "err" , err )
103108 }
104109 }
105110 }
0 commit comments