@@ -19,6 +19,8 @@ import (
1919	"time" 
2020
2121	"github.com/Unknwon/com" 
22+ 
23+ 	"github.com/gogits/gogs/modules/log" 
2224)
2325
2426const  (
@@ -99,8 +101,8 @@ func AddPublicKey(key *PublicKey) (err error) {
99101	}
100102
101103	// Calculate fingerprint. 
102- 	tmpPath  :=  filepath .Join (os .TempDir (), fmt .Sprintf ("%d" , time .Now ().Nanosecond ()),
103- 		"id_rsa.pub" )
104+ 	tmpPath  :=  strings . Replace ( filepath .Join (os .TempDir (), fmt .Sprintf ("%d" , time .Now ().Nanosecond ()),
105+ 		"id_rsa.pub" ),  " \\ " ,  "/" ,  - 1 ) 
104106	os .MkdirAll (path .Dir (tmpPath ), os .ModePerm )
105107	if  err  =  ioutil .WriteFile (tmpPath , []byte (key .Content ), os .ModePerm ); err  !=  nil  {
106108		return  err 
@@ -127,25 +129,11 @@ func AddPublicKey(key *PublicKey) (err error) {
127129	return  nil 
128130}
129131
130- // DeletePublicKey deletes SSH key information both in database and authorized_keys file. 
131- func  DeletePublicKey (key  * PublicKey ) (err  error ) {
132- 	// Delete SSH key in database. 
133- 	has , err  :=  orm .Id (key .Id ).Get (key )
134- 	if  err  !=  nil  {
135- 		return  err 
136- 	} else  if  ! has  {
137- 		return  errors .New ("Public key does not exist" )
138- 	}
139- 	if  _ , err  =  orm .Delete (key ); err  !=  nil  {
140- 		return  err 
141- 	}
142- 
132+ func  rewriteAuthorizedKeys (key  * PublicKey , p , tmpP  string ) error  {
143133	// Delete SSH key in SSH key file. 
144134	sshOpLocker .Lock ()
145135	defer  sshOpLocker .Unlock ()
146136
147- 	p  :=  filepath .Join (sshPath , "authorized_keys" )
148- 	tmpP  :=  filepath .Join (sshPath , "authorized_keys.tmp" )
149137	fr , err  :=  os .Open (p )
150138	if  err  !=  nil  {
151139		return  err 
@@ -188,8 +176,29 @@ func DeletePublicKey(key *PublicKey) (err error) {
188176			break 
189177		}
190178	}
179+ 	return  nil 
180+ }
191181
192- 	if  err  =  os .Remove (p ); err  !=  nil  {
182+ // DeletePublicKey deletes SSH key information both in database and authorized_keys file. 
183+ func  DeletePublicKey (key  * PublicKey ) (err  error ) {
184+ 	// Delete SSH key in database. 
185+ 	has , err  :=  orm .Id (key .Id ).Get (key )
186+ 	if  err  !=  nil  {
187+ 		return  err 
188+ 	} else  if  ! has  {
189+ 		return  errors .New ("Public key does not exist" )
190+ 	}
191+ 	if  _ , err  =  orm .Delete (key ); err  !=  nil  {
192+ 		return  err 
193+ 	}
194+ 
195+ 	p  :=  filepath .Join (sshPath , "authorized_keys" )
196+ 	tmpP  :=  filepath .Join (sshPath , "authorized_keys.tmp" )
197+ 	log .Trace ("ssh.DeletePublicKey(authorized_keys): %s" , p )
198+ 
199+ 	if  err  =  rewriteAuthorizedKeys (key , p , tmpP ); err  !=  nil  {
200+ 		return  err 
201+ 	} else  if  err  =  os .Remove (p ); err  !=  nil  {
193202		return  err 
194203	}
195204	return  os .Rename (tmpP , p )
0 commit comments