[Tkabber-dev] r1653 - trunk/tkabber

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Sat Feb 14 15:20:33 MSK 2009


Author: sergei
Date: 2009-02-14 15:20:33 +0300 (Sat, 14 Feb 2009)
New Revision: 1653

Modified:
   trunk/tkabber/ChangeLog
   trunk/tkabber/custom.tcl
Log:
	* custom.tcl: Use random filename for temporal customization store.


Modified: trunk/tkabber/ChangeLog
===================================================================
--- trunk/tkabber/ChangeLog	2009-02-13 23:57:23 UTC (rev 1652)
+++ trunk/tkabber/ChangeLog	2009-02-14 12:20:33 UTC (rev 1653)
@@ -3,6 +3,8 @@
 	* chats.tcl, ifacetk/iroster.tcl: Adapted to a new syntax of roster
 	  click procedures.
 
+	* custom.tcl: Use random filename for temporal customization store.
+
 2009-02-13  Sergei Golovan  <sgolovan at nes.ru>
 
 	* muc.tcl: Move focus and scroll down to 'add new JID' field in MUC

Modified: trunk/tkabber/custom.tcl
===================================================================
--- trunk/tkabber/custom.tcl	2009-02-13 23:57:23 UTC (rev 1652)
+++ trunk/tkabber/custom.tcl	2009-02-14 12:20:33 UTC (rev 1653)
@@ -6,7 +6,6 @@
 namespace eval custom {
     # Filename for saving options
     set options(customfile) [file join $::configdir custom.tcl]
-    set options(customfile~) [file join $::configdir custom.tcl~]
 
     # -1: stored values haven't been restored yet (only config changes vars)
     # 0: stored values are being restored now
@@ -716,7 +715,7 @@
     variable saved
     variable options
 
-    set fd [open $options(customfile~) w]
+    lassign [TempFile] tempfile fd
     fconfigure $fd -encoding utf-8
 
     foreach varname [array names saved] {
@@ -733,10 +732,44 @@
     }
 
     close $fd
-    catch {file attributes $options(customfile~) -permissions 00600}
-    file rename -force $options(customfile~) $options(customfile)
+    catch {file attributes $tempfile -permissions 00600}
+    file rename -force $tempfile $options(customfile)
 }
 
+proc ::custom::TempFile {} {
+    set chars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+    set nrand_chars 10
+    set maxtries 10
+    set access [list RDWR CREAT EXCL TRUNC]
+    set permission 0600
+    set fd ""
+    set mypid [pid]
+
+    for {set i 0} {$i < $maxtries} {incr i} {
+        set newname "custom"
+        for {set j 0} {$j < $nrand_chars} {incr j} {
+            append newname \
+                   [string index $chars \
+                           [expr {([clock clicks] ^ $mypid) % 62}]]
+        }
+        set newname [file join $::configdir $newname]
+
+        if {![file exists $newname]} {
+            if {![catch {open $newname $access $permission} fd]} {
+                fconfigure $fd -translation binary
+                return [list $newname $fd]
+            }
+        }
+    }
+    if {$fd eq ""} {
+        return -code error \
+               "failed to find an unused temporary file name"
+    } else {
+        return -code error \
+               [format "failed to open a temporary file: %s" $fd]
+    }
+}
+
 proc custom::store_vars {args} {
     variable saved
 



More information about the Tkabber-dev mailing list