@@ -41,31 +41,38 @@ func (fs *Filesystem) CompressFiles(dir string, name string, paths []string) (uf
4141 return nil , fmt .Errorf ("no valid files to compress" )
4242 }
4343
44+ dirfd , _ , closeFd , err := fs .unixFS .SafePath (dir )
45+ defer closeFd ()
46+ if err != nil {
47+ return nil , err
48+ }
49+
4450 a := & Archive {Filesystem : fs , BaseDirectory : dir , Files : validPaths }
45- d := path .Join (
46- dir ,
47- fmt .Sprintf ("%s.%s" ,
48- func () string {
49- if name != "" {
50- return name
51- }
52- return fmt .Sprintf ("archive-%s" , strings .ReplaceAll (time .Now ().Format (time .RFC3339 ), ":" , "" ))
53- }(),
54- ".tar.gz" ,
55- ))
56- f , err := fs .unixFS .OpenFile (d , ufs .O_WRONLY | ufs .O_CREATE , 0o644 )
51+ if name == "" {
52+ name = fmt .Sprintf ("archive-%s.tar.gz" , strings .ReplaceAll (time .Now ().Format (time .RFC3339 ), ":" , "" ))
53+ } else {
54+ name , err = fs .findCopySuffix (dirfd , name , ".tar.gz" )
55+ if err != nil {
56+ return nil , err
57+ }
58+ }
59+
60+ f , err := fs .unixFS .OpenFileat (dirfd , name , ufs .O_WRONLY | ufs .O_CREATE , 0o644 )
5761 if err != nil {
5862 return nil , err
5963 }
6064 defer f .Close ()
65+
6166 cw := ufs .NewCountedWriter (f )
6267 if err := a .Stream (context .Background (), cw ); err != nil {
6368 return nil , err
6469 }
70+
6571 if ! fs .unixFS .CanFit (cw .BytesWritten ()) {
66- _ = fs .unixFS .Remove (d )
72+ _ = fs .unixFS .Remove (path . Join ( dir , name ) )
6773 return nil , newFilesystemError (ErrCodeDiskSpace , nil )
6874 }
75+
6976 fs .unixFS .Add (cw .BytesWritten ())
7077 return f .Stat ()
7178}
0 commit comments