@@ -447,42 +447,45 @@ the file in the cache.
447447public def Cache.saveArtifact
448448 (cache : Cache) (file : FilePath) (ext := "art" ) (text exe useLocalFile := false )
449449: IO Artifact := do
450- if text then
451- let contents ← IO.FS.readFile file
452- let normalized := contents.crlfToLf
453- let hash := Hash.ofString normalized
454- let descr := artifactWithExt hash ext
455- let cacheFile := cache.artifactDir / descr.relPath
456- -- make the local file unwritable where possible to discourage users from
457- -- writing to such paths as this can corrupt the cache if the file was hard linked
458- let r := {read := true , write := false , execution := false }
459- IO.setAccessRights file ⟨r, r, r⟩
460- unless (← cacheFile.pathExists) do
461- createParentDirs cacheFile
462- IO.FS.writeFile cacheFile normalized
463- IO.setAccessRights cacheFile ⟨r, r, r⟩
464- writeFileHash file hash
465- let mtime := (← getMTime cacheFile |>.toBaseIO).toOption.getD 0
466- let path := if useLocalFile then file else cacheFile
467- return {descr, name := file.toString, path, mtime}
468- else
469- let contents ← IO.FS.readBinFile file
470- let hash := Hash.ofByteArray contents
471- let descr := artifactWithExt hash ext
472- let cacheFile := cache.artifactDir / descr.relPath
473- -- make the local file unwritable where possible to discourage users from
474- -- writing to such paths as this can corrupt the cache if the file is hard linked
475- let r := {read := true , write := false , execution := exe}
476- IO.setAccessRights file ⟨r, r, r⟩
477- unless (← cacheFile.pathExists) do
478- createParentDirs cacheFile
479- if let .error _ ← (IO.FS.hardLink file cacheFile).toBaseIO then
480- IO.FS.writeBinFile cacheFile contents
450+ try
451+ if text then
452+ let contents ← IO.FS.readFile file
453+ let normalized := contents.crlfToLf
454+ let hash := Hash.ofString normalized
455+ let descr := artifactWithExt hash ext
456+ let cacheFile := cache.artifactDir / descr.relPath
457+ -- make the local file unwritable where possible to discourage users from
458+ -- writing to such paths as this can corrupt the cache if the file was hard linked
459+ let r := {read := true , write := false , execution := false }
460+ IO.setAccessRights file ⟨r, r, r⟩
461+ unless (← cacheFile.pathExists) do
462+ createParentDirs cacheFile
463+ IO.FS.writeFile cacheFile normalized
481464 IO.setAccessRights cacheFile ⟨r, r, r⟩
482- writeFileHash file hash
483- let mtime := (← getMTime cacheFile |>.toBaseIO).toOption.getD 0
484- let path := if useLocalFile then file else cacheFile
485- return {descr, name := file.toString, path, mtime}
465+ writeFileHash file hash
466+ let mtime := (← getMTime cacheFile |>.toBaseIO).toOption.getD 0
467+ let path := if useLocalFile then file else cacheFile
468+ return {descr, name := file.toString, path, mtime}
469+ else
470+ let contents ← IO.FS.readBinFile file
471+ let hash := Hash.ofByteArray contents
472+ let descr := artifactWithExt hash ext
473+ let cacheFile := cache.artifactDir / descr.relPath
474+ -- make the local file unwritable where possible to discourage users from
475+ -- writing to such paths as this can corrupt the cache if the file is hard linked
476+ let r := {read := true , write := false , execution := exe}
477+ IO.setAccessRights file ⟨r, r, r⟩
478+ unless (← cacheFile.pathExists) do
479+ createParentDirs cacheFile
480+ if let .error _ ← (IO.FS.hardLink file cacheFile).toBaseIO then
481+ IO.FS.writeBinFile cacheFile contents
482+ IO.setAccessRights cacheFile ⟨r, r, r⟩
483+ writeFileHash file hash
484+ let mtime := (← getMTime cacheFile |>.toBaseIO).toOption.getD 0
485+ let path := if useLocalFile then file else cacheFile
486+ return {descr, name := file.toString, path, mtime}
487+ catch e =>
488+ error s! "failed to cache artifact: { e} "
486489
487490@ [inline, inherit_doc Cache.saveArtifact]
488491public def cacheArtifact
0 commit comments