File tree 1 file changed +20
-8
lines changed
1 file changed +20
-8
lines changed Original file line number Diff line number Diff line change 1
1
package lib
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"path/filepath"
6
7
)
7
8
8
9
func writeLib (libDir , libFullName string , content []byte , versionMatched bool ) error {
9
10
libFullPath := filepath .Join (libDir , libFullName )
10
11
_ , err := os .Stat (libFullPath )
11
- if os .IsNotExist (err ) || ! versionMatched {
12
- err = os .MkdirAll (libDir , 0777 )
12
+ if ! os .IsNotExist (err ) && versionMatched {
13
+ return err
14
+ }
15
+ if err := os .MkdirAll (libDir , 0777 ); err != nil {
16
+ return err
17
+ }
18
+ for pass := 0 ; ; pass ++ {
19
+ tmpFullPath := fmt .Sprintf ("%s~%d" , libFullPath , pass )
20
+ tmpFile , err := os .OpenFile (tmpFullPath , os .O_CREATE | os .O_RDWR | os .O_EXCL , 0755 )
13
21
if err != nil {
22
+ if os .IsExist (err ) {
23
+ continue
24
+ }
14
25
return err
15
26
}
16
- libFile , err := os .Create (libFullPath )
17
27
defer func () {
18
- libFile .Close ()
28
+ tmpFile .Close ()
29
+ _ = os .Remove (tmpFullPath )
19
30
}()
20
- if err != nil {
31
+
32
+ if _ , err = tmpFile .Write (content ); err != nil {
21
33
return err
22
34
}
23
- _ , err = libFile .Write (content )
24
- if err != nil {
35
+ if err := os .Rename (tmpFullPath , libFullPath ); err != nil {
25
36
return err
26
37
}
38
+ break
27
39
}
28
- return err
40
+ return nil
29
41
}
You can’t perform that action at this time.
0 commit comments