summaryrefslogtreecommitdiff
path: root/src/common/lock.c
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2011-09-10 16:12:07 -0700
committerBen Longbons <b.r.longbons@gmail.com>2011-09-10 16:12:07 -0700
commitf841b6fdcc802e73d52da0e67ee192c0c2c1c7e1 (patch)
treed9b013ab252968ec1e90e721f7b2ab819af0acb0 /src/common/lock.c
parent5939e1bec75f2550d3ce109b9cd9a5d22c0626c2 (diff)
parent723fb5d3431b847526c433a13aa74485cfb564a3 (diff)
downloadtmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.gz
tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.bz2
tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.xz
tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.zip
Merge commit '723fb5d3431b847526c433a13aa74485cfb564a3'
Diffstat (limited to 'src/common/lock.c')
-rw-r--r--src/common/lock.c27
1 files changed, 9 insertions, 18 deletions
diff --git a/src/common/lock.c b/src/common/lock.c
index bed657f..dd42ef2 100644
--- a/src/common/lock.c
+++ b/src/common/lock.c
@@ -1,45 +1,36 @@
-
#include <unistd.h>
#include <stdio.h>
#include "lock.h"
#include "socket.h"
-// 書き込みファイルの保護処理
-// (書き込みが終わるまで、旧ファイルを保管しておく)
+/// Protected file writing
+/// (Until the file is closed, it keeps the old file)
-// 新しいファイルの書き込み開始
+// Start writing a tmpfile
FILE *lock_fopen (const char *filename, int *info)
{
char newfile[512];
FILE *fp;
int no = getpid ();
- // 安全なファイル名を得る(手抜き)
+ // Get a filename that doesn't already exist
do
{
sprintf (newfile, "%s_%d.tmp", filename, no++);
}
- while ((fp = fopen_ (newfile, "r")) && fclose_ (fp));
+ while ((fp = fopen_ (newfile, "r")) && (fclose_ (fp), 1));
*info = --no;
return fopen_ (newfile, "w");
}
-// 旧ファイルを削除&新ファイルをリネーム
-int lock_fclose (FILE * fp, const char *filename, int *info)
+// Delete the old file and rename the new file
+void lock_fclose (FILE * fp, const char *filename, int *info)
{
- int ret = 0;
char newfile[512];
- if (fp != NULL)
+ if (fp)
{
- ret = fclose_ (fp);
+ fclose_ (fp);
sprintf (newfile, "%s_%d.tmp", filename, *info);
- remove (filename);
- // このタイミングで落ちると最悪。
rename (newfile, filename);
- return ret;
- }
- else
- {
- return 1;
}
}