@@ -197,7 +197,7 @@ func (ic *ContainerEngine) QuadletInstall(ctx context.Context, pathsOrURLs []str
197197 installReport .QuadletErrors [toInstall ] = fmt .Errorf ("populating temporary file: %w" , err )
198198 continue
199199 }
200- installedPath , err := ic .installQuadlet (ctx , tmpFile .Name (), quadletFileName , installDir , assetFile , validateQuadletFile )
200+ installedPath , err := ic .installQuadlet (ctx , tmpFile .Name (), quadletFileName , installDir , assetFile , validateQuadletFile , options . Replace )
201201 if err != nil {
202202 installReport .QuadletErrors [toInstall ] = err
203203 continue
@@ -210,7 +210,7 @@ func (ic *ContainerEngine) QuadletInstall(ctx context.Context, pathsOrURLs []str
210210 continue
211211 }
212212 // If toInstall is a single file, execute the original logic
213- installedPath , err := ic .installQuadlet (ctx , toInstall , "" , installDir , assetFile , validateQuadletFile )
213+ installedPath , err := ic .installQuadlet (ctx , toInstall , "" , installDir , assetFile , validateQuadletFile , options . Replace )
214214 if err != nil {
215215 installReport .QuadletErrors [toInstall ] = err
216216 continue
@@ -254,7 +254,7 @@ func getFileName(resp *http.Response, fileURL string) (string, error) {
254254// Perform some minimal validation, but not much.
255255// We can't know about a lot of problems without running the Quadlet binary, which we
256256// only want to do once.
257- func (ic * ContainerEngine ) installQuadlet (_ context.Context , path , destName , installDir , assetFile string , isQuadletFile bool ) (string , error ) {
257+ func (ic * ContainerEngine ) installQuadlet (_ context.Context , path , destName , installDir , assetFile string , isQuadletFile , replace bool ) (string , error ) {
258258 // First, validate that the source path exists and is a file
259259 stat , err := os .Stat (path )
260260 if err != nil {
@@ -274,9 +274,15 @@ func (ic *ContainerEngine) installQuadlet(_ context.Context, path, destName, ins
274274 return "" , fmt .Errorf ("%q is not a supported Quadlet file type" , filepath .Ext (finalPath ))
275275 }
276276
277- file , err := os .OpenFile (finalPath , os .O_CREATE | os .O_EXCL | os .O_WRONLY , 0644 )
277+ var osFlags = os .O_CREATE | os .O_WRONLY
278+
279+ if ! replace {
280+ osFlags |= os .O_EXCL
281+ }
282+
283+ file , err := os .OpenFile (finalPath , osFlags , 0644 )
278284 if err != nil {
279- if errors .Is (err , fs .ErrExist ) {
285+ if errors .Is (err , fs .ErrExist ) && ! replace {
280286 return "" , fmt .Errorf ("a Quadlet with name %s already exists, refusing to overwrite" , filepath .Base (finalPath ))
281287 }
282288 return "" , fmt .Errorf ("unable to open file %s: %w" , filepath .Base (finalPath ), err )
0 commit comments