44package  unittest
55
66import  (
7- 	"io" 
87	"os" 
9- 	"path" 
8+ 	"path/filepath " 
109	"strings" 
1110
1211	"code.gitea.io/gitea/modules/util" 
@@ -31,27 +30,7 @@ func Copy(src, dest string) error {
3130		return  os .Symlink (target , dest )
3231	}
3332
34- 	sr , err  :=  os .Open (src )
35- 	if  err  !=  nil  {
36- 		return  err 
37- 	}
38- 	defer  sr .Close ()
39- 
40- 	dw , err  :=  os .Create (dest )
41- 	if  err  !=  nil  {
42- 		return  err 
43- 	}
44- 	defer  dw .Close ()
45- 
46- 	if  _ , err  =  io .Copy (dw , sr ); err  !=  nil  {
47- 		return  err 
48- 	}
49- 
50- 	// Set back file information. 
51- 	if  err  =  os .Chtimes (dest , si .ModTime (), si .ModTime ()); err  !=  nil  {
52- 		return  err 
53- 	}
54- 	return  os .Chmod (dest , si .Mode ())
33+ 	return  util .CopyFile (src , dest )
5534}
5635
5736// Sync synchronizes the two files. This is skipped if both files 
@@ -89,18 +68,17 @@ func SyncDirs(srcPath, destPath string) error {
8968	}
9069
9170	// find and delete all untracked files 
92- 	infos , err  :=  util .StatDir (destPath , true )
71+ 	files , err  :=  util .StatDir (destPath , true )
9372	if  err  !=  nil  {
9473		return  err 
9574	}
9675
97- 	for  _ , info  :=  range  infos  {
98- 		curPath  :=  path .Join (destPath , info )
99- 
100- 		if  _ , err  :=  os .Stat (path .Join (srcPath , info )); err  !=  nil  {
76+ 	for  _ , file  :=  range  files  {
77+ 		destFilePath  :=  filepath .Join (destPath , file )
78+ 		if  _ , err  =  os .Stat (filepath .Join (srcPath , file )); err  !=  nil  {
10179			if  os .IsNotExist (err ) {
102- 				// Delete  
103- 				if  err  :=  os .RemoveAll (curPath ); err  !=  nil  {
80+ 				// TODO: why delete? it should happen because the file list is just queried above, why not exist?  
81+ 				if  err  :=  os .RemoveAll (destFilePath ); err  !=  nil  {
10482					return  err 
10583				}
10684			} else  {
@@ -110,17 +88,17 @@ func SyncDirs(srcPath, destPath string) error {
11088	}
11189
11290	// Gather directory info. 
113- 	infos , err  =  util .StatDir (srcPath , true )
91+ 	files , err  =  util .StatDir (srcPath , true )
11492	if  err  !=  nil  {
11593		return  err 
11694	}
11795
118- 	for  _ , info  :=  range  infos  {
119- 		curPath  :=  path .Join (destPath , info )
120- 		if  strings .HasSuffix (info , "/" ) {
121- 			err  =  os .MkdirAll (curPath , os .ModePerm )
96+ 	for  _ , file  :=  range  files  {
97+ 		destFilePath  :=  filepath .Join (destPath , file )
98+ 		if  strings .HasSuffix (file , "/" ) {
99+ 			err  =  os .MkdirAll (destFilePath , os .ModePerm )
122100		} else  {
123- 			err  =  Sync (path .Join (srcPath , info ), curPath )
101+ 			err  =  Sync (filepath .Join (srcPath , file ), destFilePath )
124102		}
125103		if  err  !=  nil  {
126104			return  err 
0 commit comments