@@ -56,24 +56,30 @@ const (
5656type  IgnoreListEntry  struct  {
5757	Path             string 
5858	PrefixMatchOnly  bool 
59+ 	// AllowedPaths specifies **exact matches** to allow, even if they are under 
60+ 	// Path. 
61+ 	AllowedPaths  []string 
5962}
6063
6164var  defaultIgnoreList  =  []IgnoreListEntry {
6265	{
6366		Path :            filepath .Clean (config .KanikoDir ),
6467		PrefixMatchOnly : false ,
68+ 		AllowedPaths :    nil ,
6569	},
6670	{
6771		// similarly, we ignore /etc/mtab, since there is no way to know if the file was mounted or came 
6872		// from the base image 
6973		Path :            "/etc/mtab" ,
7074		PrefixMatchOnly : false ,
75+ 		AllowedPaths :    nil ,
7176	},
7277	{
7378		// we ingore /tmp/apt-key-gpghome, since the apt keys are added temporarily in this directory. 
7479		// from the base image 
7580		Path :            "/tmp/apt-key-gpghome" ,
7681		PrefixMatchOnly : true ,
82+ 		AllowedPaths :    nil ,
7783	},
7884}
7985
@@ -111,16 +117,29 @@ func AddToIgnoreList(entry IgnoreListEntry) {
111117	ignorelist  =  append (ignorelist , IgnoreListEntry {
112118		Path :            filepath .Clean (entry .Path ),
113119		PrefixMatchOnly : entry .PrefixMatchOnly ,
120+ 		AllowedPaths :    nil ,
114121	})
115122}
116123
117124func  AddToDefaultIgnoreList (entry  IgnoreListEntry ) {
118125	defaultIgnoreList  =  append (defaultIgnoreList , IgnoreListEntry {
119126		Path :            filepath .Clean (entry .Path ),
120127		PrefixMatchOnly : entry .PrefixMatchOnly ,
128+ 		AllowedPaths :    nil ,
121129	})
122130}
123131
132+ func  AddAllowedPathToDefaultIgnoreList (allowPath  string ) error  {
133+ 	for  _ , ile  :=  range  defaultIgnoreList  {
134+ 		if  ! strings .HasPrefix (allowPath , ile .Path ) {
135+ 			continue 
136+ 		}
137+ 		ile .AllowedPaths  =  append (ile .AllowedPaths , allowPath )
138+ 		return  nil 
139+ 	}
140+ 	return  fmt .Errorf ("path %q is not covered by any current entry in ignore list" , allowPath )
141+ }
142+ 
124143func  IncludeWhiteout () FSOpt  {
125144	return  func (opts  * FSConfig ) {
126145		opts .includeWhiteout  =  true 
@@ -500,6 +519,11 @@ func IsInIgnoreList(path string) bool {
500519
501520func  CheckCleanedPathAgainstProvidedIgnoreList (path  string , wl  []IgnoreListEntry ) bool  {
502521	for  _ , wl  :=  range  ignorelist  {
522+ 		for  _ , ap  :=  range  wl .AllowedPaths  {
523+ 			if  ap  ==  path  {
524+ 				return  false 
525+ 			}
526+ 		}
503527		if  hasCleanedFilepathPrefix (path , wl .Path , wl .PrefixMatchOnly ) {
504528			return  true 
505529		}
@@ -556,6 +580,7 @@ func DetectFilesystemIgnoreList(path string) error {
556580			AddToIgnoreList (IgnoreListEntry {
557581				Path :            lineArr [4 ],
558582				PrefixMatchOnly : false ,
583+ 				AllowedPaths :    nil ,
559584			})
560585		}
561586		if  err  ==  io .EOF  {
@@ -668,14 +693,30 @@ func CreateFile(path string, reader io.Reader, perm os.FileMode, uid uint32, gid
668693		}
669694	}
670695
696+ 	var  renamed  string 
671697	dest , err  :=  os .Create (path )
672698	if  err  !=  nil  {
673- 		return  errors .Wrap (err , "creating file" )
699+ 		if  ! errors .Is (err , syscall .ETXTBSY ) {
700+ 			return  errors .Wrap (err , "creating file" )
701+ 		}
702+ 		// If the file is busy, just write to a temp file and 
703+ 		// move to dest. 
704+ 		dest , err  =  os .CreateTemp (os .TempDir (), "" )
705+ 		if  err  !=  nil  {
706+ 			return  errors .Wrap (err , "create temp file" )
707+ 		}
708+ 		defer  os .Remove (dest .Name ())
709+ 		renamed  =  dest .Name ()
674710	}
675711	defer  dest .Close ()
676712	if  _ , err  :=  io .Copy (dest , reader ); err  !=  nil  {
677713		return  errors .Wrap (err , "copying file" )
678714	}
715+ 	if  renamed  !=  ""  {
716+ 		if  err  :=  os .Rename (renamed , path ); err  !=  nil  {
717+ 			return  errors .Wrap (err , "rename temp file" )
718+ 		}
719+ 	}
679720	return  setFilePermissions (path , perm , int (uid ), int (gid ))
680721}
681722
@@ -685,6 +726,7 @@ func AddVolumePathToIgnoreList(path string) {
685726	AddToIgnoreList (IgnoreListEntry {
686727		Path :            path ,
687728		PrefixMatchOnly : true ,
729+ 		AllowedPaths :    nil ,
688730	})
689731	volumes  =  append (volumes , path )
690732}
0 commit comments