[uim-commit] r869 - trunk/uim

ekato at freedesktop.org ekato at freedesktop.org
Mon Jun 6 02:39:50 PDT 2005


Author: ekato
Date: 2005-06-06 02:39:47 -0700 (Mon, 06 Jun 2005)
New Revision: 869

Modified:
   trunk/uim/skk-dic.c
Log:
* uim/skk-dic.c : Remove unused included file "uim-helper.h".
(skk_lib_save_personal_dictionary) : Simplify the routine and use
  a temporary file for safe behavior.


Modified: trunk/uim/skk-dic.c
===================================================================
--- trunk/uim/skk-dic.c	2005-06-05 16:11:02 UTC (rev 868)
+++ trunk/uim/skk-dic.c	2005-06-06 09:39:47 UTC (rev 869)
@@ -54,7 +54,6 @@
 #include "uim-scm.h"
 #include "context.h"
 #include "plugin.h"
-#include "uim-helper.h"
 
 /*
  * cand : candidate
@@ -2171,11 +2170,12 @@
 {
   FILE *fp;
   const char *fn = uim_scm_refer_c_str(fn_);
+  char *tmp_fn = NULL;
   struct skk_line *sl;
   struct stat st;
   int lock_fd = -1;
 
-  if (!skk_dic)
+  if (!skk_dic || skk_dic->cache_modified == 0)
     return uim_scm_f();
 
   if (fn) {
@@ -2183,32 +2183,39 @@
       if (st.st_mtime != skk_dic->personal_dic_timestamp)
 	update_personal_dictionary_cache(fn);
     }
-    if (skk_dic->cache_modified == 0) {
-      return uim_scm_f();
-    }
+
     lock_fd = open_lock(fn, F_WRLCK);
-    fp = fopen(fn, "w");
-    if (!fp) {
-      close_lock(lock_fd);
-      return uim_scm_f();
-    }
+    if (!(tmp_fn = malloc(strlen(fn) + 5)))
+      goto error;
+
+    sprintf(tmp_fn, "%s.tmp", fn);
+    fp = fopen(tmp_fn, "w");
+    if (!fp)
+      goto error;
+
   } else {
     fp = stdout;
   }
 
   for (sl = skk_dic->head.next; sl; sl = sl->next) {
-    if (sl->need_save) {
+    if (sl->need_save)
       write_out_line(fp, sl);
-    }
   }
-  fclose(fp);
 
-  if (stat(fn, &st) != -1)
+  if (fclose(fp) != 0)
+    goto error;
+
+  if (remove(fn) != 0 || rename(tmp_fn, fn) != 0)
+    goto error;
+
+  if (stat(fn, &st) != -1) {
     skk_dic->personal_dic_timestamp = st.st_mtime;
+    skk_dic->cache_modified = 0;
+  }
 
+error:
   close_lock(lock_fd);
-  skk_dic->cache_modified = 0;
-
+  free(tmp_fn);
   return uim_scm_f();
 }
 



More information about the uim-commit mailing list