summaryrefslogtreecommitdiff
path: root/src/common/lock.c
diff options
context:
space:
mode:
authorDennis Friis <peavey@placid.dk>2008-04-02 15:09:18 +0000
committerDennis Friis <peavey@placid.dk>2008-04-02 15:09:18 +0000
commit97bb21a79949779df76269b087f3bce7ef8179ee (patch)
tree08b3bdee2f3596581dd8d8f051afd37ee648cee7 /src/common/lock.c
parentdafdbbe9f1db3e6943003a79a6e23c00da2f2fe1 (diff)
downloadtmwa-97bb21a79949779df76269b087f3bce7ef8179ee.tar.gz
tmwa-97bb21a79949779df76269b087f3bce7ef8179ee.tar.bz2
tmwa-97bb21a79949779df76269b087f3bce7ef8179ee.tar.xz
tmwa-97bb21a79949779df76269b087f3bce7ef8179ee.zip
initial checkin
Diffstat (limited to 'src/common/lock.c')
-rw-r--r--src/common/lock.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/common/lock.c b/src/common/lock.c
new file mode 100644
index 0000000..9a2205b
--- /dev/null
+++ b/src/common/lock.c
@@ -0,0 +1,37 @@
+
+#include <stdio.h>
+#include "lock.h"
+
+// 書き込みファイルの保護処理
+// (書き込みが終わるまで、旧ファイルを保管しておく)
+
+// 新しいファイルの書き込み開始
+FILE* lock_fopen(const char* filename,int *info) {
+ char newfile[512];
+ FILE *fp;
+ int no = 0;
+
+ // 安全なファイル名を得る(手抜き)
+ do {
+ sprintf(newfile,"%s_%04d.tmp",filename,++no);
+ } while((fp = fopen(newfile,"r")) && (fclose(fp), no<9999) );
+ *info = no;
+ return fopen(newfile,"w");
+}
+
+// 旧ファイルを削除&新ファイルをリネーム
+int lock_fclose(FILE *fp,const char* filename,int *info) {
+ int ret = 0;
+ char newfile[512];
+ if(fp != NULL) {
+ ret = fclose(fp);
+ sprintf(newfile,"%s_%04d.tmp",filename,*info);
+ remove(filename);
+ // このタイミングで落ちると最悪。
+ rename(newfile,filename);
+ return ret;
+ } else {
+ return 1;
+ }
+}
+