summaryrefslogtreecommitdiff
path: root/src/common/lock.c
diff options
context:
space:
mode:
authorcelest <celest@54d463be-8e91-2dee-dedb-b68131a5f0ec>2005-03-31 10:56:19 +0000
committercelest <celest@54d463be-8e91-2dee-dedb-b68131a5f0ec>2005-03-31 10:56:19 +0000
commit43a1c839e4e0c6097aa83ea9bf9216bbb4a1194e (patch)
tree2b4e42ef674c5ff1409457c7caf06a1d5ff86b02 /src/common/lock.c
parent5e8e549480b8a653ba927eca964ea54d4bc2dcb8 (diff)
downloadhercules-43a1c839e4e0c6097aa83ea9bf9216bbb4a1194e.tar.gz
hercules-43a1c839e4e0c6097aa83ea9bf9216bbb4a1194e.tar.bz2
hercules-43a1c839e4e0c6097aa83ea9bf9216bbb4a1194e.tar.xz
hercules-43a1c839e4e0c6097aa83ea9bf9216bbb4a1194e.zip
* Added back up old files in 'save' before saving new data
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/branches/stable@1353 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/common/lock.c')
-rw-r--r--src/common/lock.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/src/common/lock.c b/src/common/lock.c
index 341903434..288093c68 100644
--- a/src/common/lock.c
+++ b/src/common/lock.c
@@ -2,42 +2,48 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
+#include <unistd.h>
#include "lock.h"
#include "showmsg.h"
+#define exists(filename) (!access(filename, F_OK))
// 書き込みファイルの保護処理
// (書き込みが終わるまで、旧ファイルを保管しておく)
// 新しいファイルの書き込み開始
-FILE* lock_fopen(const char* filename,int *info) {
+FILE* lock_fopen (const char* filename, int *info) {
char newfile[512];
FILE *fp;
- int no = 0;
+ int no = 0;
// 安全なファイル名を得る(手抜き)
do {
- sprintf(newfile,"%s_%04d.tmp",filename,++no);
- } while((fp = fopen(newfile,"r")) && (fclose(fp), no<9999) );
+ 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;
+int lock_fclose (FILE *fp, const char* filename, int *info) {
+ int ret = 1;
char newfile[512];
- if(fp != NULL) {
+ char oldfile[512];
+ if (fp != NULL) {
ret = fclose(fp);
- sprintf(newfile,"%s_%04d.tmp",filename,*info);
- remove(filename);
+ sprintf(newfile, "%s_%04d.tmp", filename, *info);
+ sprintf(oldfile, "%s.bak", filename); // old backup file
+
+ if (exists(oldfile)) remove(oldfile); // remove backup file if it already exists
+ rename (filename, oldfile); // backup our older data instead of deleting it
+
// このタイミングで落ちると最悪。
- if (rename(newfile,filename) != 0) {
+ if ((ret = rename(newfile,filename)) != 0) { // rename our temporary file to its correct name
sprintf(tmp_output,"%s - '"CL_WHITE"%s"CL_RESET"'\n", strerror(errno), newfile);
ShowError(tmp_output);
}
- return ret;
- } else {
- return 1;
}
+
+ return ret;
}