Просмотр исходного кода

1, 优化修改swapi库中变量的命名,使其更符合c习惯; 2, 优化修改sw_dir_delete函数,增强容错性

niujiuru 1 неделя назад
Родитель
Сommit
1a97878347
3 измененных файлов с 48 добавлено и 47 удалено
  1. 12 11
      swapi/swdir.c
  2. 28 28
      swapi/swfile.c
  3. 8 8
      swapi/swfile.h

+ 12 - 11
swapi/swdir.c

@@ -42,7 +42,8 @@ int sw_dir_create(const char *dir)
 bool sw_dir_delete(const char *dir)
 bool sw_dir_delete(const char *dir)
 {
 {
 	DIR *hDir = NULL; struct dirent *pData = NULL; struct stat statBuf = { 0 };
 	DIR *hDir = NULL; struct dirent *pData = NULL; struct stat statBuf = { 0 };
-	char filename[MAX_PATH_CHARS] = { 0 }; // full path filename
+	char path[MAX_PATH_CHARS] = { 0 }; // full path(dir + entry name)
+	bool success = true;
 	
 	
 	hDir = (DIR *)sw_dir_open(dir);
 	hDir = (DIR *)sw_dir_open(dir);
 	if(!hDir) return false;
 	if(!hDir) return false;
@@ -53,24 +54,24 @@ bool sw_dir_delete(const char *dir)
 		
 		
 		if(xstrcasecmp(pData->d_name, ".") == 0 || xstrcasecmp(pData->d_name, "..") == 0) continue;
 		if(xstrcasecmp(pData->d_name, ".") == 0 || xstrcasecmp(pData->d_name, "..") == 0) continue;
 		
 		
-		memset(filename, 0, sizeof(filename));
-		strncpy(filename, dir, sizeof(filename)-1);
-		xstrcharreplace(filename, '\\', '/');
-		if(filename[strlen(filename)-1] != '/') strcat(filename, "/");
-		strcat(filename, pData->d_name);
+		memset(path, 0, sizeof(path));
+		strncpy(path, dir, sizeof(path)-1);
+		xstrcharreplace(path, '\\', '/');
+		if(strlen(path) > 0 && path[strlen(path)-1] != '/') strcat(path, "/");
+		strcat(path, pData->d_name);
 		
 		
 		memset(&statBuf, 0, sizeof(statBuf));
 		memset(&statBuf, 0, sizeof(statBuf));
-		if(stat(filename, &statBuf) == 0)
+		if(lstat(path, &statBuf) == 0) // 用"lstat()"获取文件信息, 避免软链接等特殊文件导致的错误删除
 		{
 		{
-			if(S_ISDIR(statBuf.st_mode)) { if(!sw_dir_delete(filename)) return false; } // 目录
-			else if(sw_file_delete(filename) != 0) return false; // 文件
+			if(S_ISDIR(statBuf.st_mode)) { if(!sw_dir_delete(path)) { success = false; break; } } // 目录
+			else if(sw_file_delete(path) != 0) { success = false; break; } // 文件
 		}
 		}
 	}
 	}
 	sw_dir_close(hDir);
 	sw_dir_close(hDir);
 	
 	
-	if(rmdir(dir) != 0) return false;
+	if(success && rmdir(dir) != 0) return false;
 	
 	
-	return true;
+	return success;
 }
 }
 
 
 /* 判断目录是否存在 */
 /* 判断目录是否存在 */

+ 28 - 28
swapi/swfile.c

@@ -18,18 +18,18 @@
 #include "swfile.h"
 #include "swfile.h"
 
 
 /* 装载文件 */
 /* 装载文件 */
-int sw_file_load(const char *filename, const char *mode, char *buf, int bufSize)
+int sw_file_load(const char *path, const char *mode, char *buf, int bufSize)
 {
 {
 	FILE *fp = NULL;
 	FILE *fp = NULL;
 	int filesize = 0, readsize = 0;
 	int filesize = 0, readsize = 0;
 
 
-	if(!filename || !mode || !buf || bufSize < 0) return -1;
+	if(!path || !mode || !buf || bufSize < 0) return -1;
 	
 	
-	filesize = sw_file_getSize(filename);
+	filesize = sw_file_getSize(path);
 	if(filesize < 0) return -1;
 	if(filesize < 0) return -1;
 	else if(filesize == 0) return 0;
 	else if(filesize == 0) return 0;
 
 
-	fp = fopen(filename, mode);
+	fp = fopen(path, mode);
 	if(!fp) return -1;
 	if(!fp) return -1;
 	if(filesize > bufSize) readsize = -1;
 	if(filesize > bufSize) readsize = -1;
 	else readsize = fread(buf, sizeof(char), filesize, fp);
 	else readsize = fread(buf, sizeof(char), filesize, fp);
@@ -39,14 +39,14 @@ int sw_file_load(const char *filename, const char *mode, char *buf, int bufSize)
 }
 }
 
 
 /* 更新文件 */
 /* 更新文件 */
-int sw_file_update(const char *filename, const char *mode, const char *buf, int bufSize)
+int sw_file_update(const char *path, const char *mode, const char *buf, int bufSize)
 {
 {
 	FILE *fp = NULL;
 	FILE *fp = NULL;
 	int writesize = 0;
 	int writesize = 0;
 
 
-	if(!filename || !mode || !buf || bufSize < 0) return -1;
+	if(!path || !mode || !buf || bufSize < 0) return -1;
 
 
-	fp = fopen(filename, mode);
+	fp = fopen(path, mode);
 	if(!fp) return -1;
 	if(!fp) return -1;
 	if(bufSize > 0) writesize = fwrite(buf, sizeof(char), bufSize, fp);
 	if(bufSize > 0) writesize = fwrite(buf, sizeof(char), bufSize, fp);
 	fclose(fp);
 	fclose(fp);
@@ -55,13 +55,13 @@ int sw_file_update(const char *filename, const char *mode, const char *buf, int
 }
 }
 
 
 /* 得到文件大小 */
 /* 得到文件大小 */
-int sw_file_getSize(const char *filename)
+int sw_file_getSize(const char *path)
 {
 {
 	struct stat statBuf = { 0 };
 	struct stat statBuf = { 0 };
 
 
-	if(!filename) return -1;
+	if(!path) return -1;
 
 
-	if(stat(filename, &statBuf) == 0) return statBuf.st_size;
+	if(stat(path, &statBuf) == 0) return statBuf.st_size;
 	else return -1;
 	else return -1;
 }
 }
 
 
@@ -92,32 +92,32 @@ int sw_file_copy(const char *existingFilename, const char *newFilename, bool fai
 }
 }
 
 
 /* 删除文件 */
 /* 删除文件 */
-int sw_file_delete(const char *filename)
+int sw_file_delete(const char *path)
 {
 {
-	return remove(filename);	
+	return remove(path);	
 }
 }
 
 
 /* 判断文件是否存在 */
 /* 判断文件是否存在 */
-bool sw_file_exists(const char *filename)
+bool sw_file_exists(const char *path)
 {
 {
-	if(access(filename, 0) == 0) return true;
+	if(access(path, 0) == 0) return true;
 	else return false;
 	else return false;
 }
 }
 
 
 /* 读配置或字典文件(mode : "GetFieldName", 根据"字段值"获取"字段名"; "GetFieldValue", 根据"字段名"获取"字段值") */
 /* 读配置或字典文件(mode : "GetFieldName", 根据"字段值"获取"字段名"; "GetFieldValue", 根据"字段名"获取"字段值") */
-char *sw_file_getCfgString(const char *filename, const char *section, const char *mode, char *field, char *value, int resultBufSize)
+char *sw_file_getCfgString(const char *path, const char *section, const char *mode, char *field, char *value, int resultBufSize)
 {
 {
 	char *pBuf = NULL, *pSection = NULL, *pStr = NULL;
 	char *pBuf = NULL, *pSection = NULL, *pStr = NULL;
 	int filesize = 0;
 	int filesize = 0;
 
 
-	if(!filename || !mode || !field || !value || resultBufSize <= 0) return NULL;
+	if(!path || !mode || !field || !value || resultBufSize <= 0) return NULL;
 
 
-	filesize = sw_file_getSize(filename);
+	filesize = sw_file_getSize(path);
 	if(filesize <= 0) return NULL;
 	if(filesize <= 0) return NULL;
 
 
 	pBuf = (char *)malloc(filesize+1);
 	pBuf = (char *)malloc(filesize+1);
 	if(!pBuf) return NULL;
 	if(!pBuf) return NULL;
-	if(sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
+	if(sw_file_load(path, "rb", pBuf, filesize) != filesize) goto EndP;
 	else pBuf[filesize] = '\0';
 	else pBuf[filesize] = '\0';
 
 
 	if(section)
 	if(section)
@@ -138,20 +138,20 @@ EndP:
 }
 }
 
 
 /* 写配置或字典文件(mode : "SetFieldName", 根据"字段值"设置"字段名"; "SetFieldValue", 根据"字段名"设置"字段值") */
 /* 写配置或字典文件(mode : "SetFieldName", 根据"字段值"设置"字段名"; "SetFieldValue", 根据"字段名"设置"字段值") */
-bool sw_file_setCfgString(const char *filename, const char *section, const char *mode, const char *field, const char *value, bool autoAdd)
+bool sw_file_setCfgString(const char *path, const char *section, const char *mode, const char *field, const char *value, bool autoAdd)
 {
 {
 	char *pBuf = NULL, *pSection = NULL;
 	char *pBuf = NULL, *pSection = NULL;
 	int filesize = 0, varysize1 = 0, varysize2 = 0;
 	int filesize = 0, varysize1 = 0, varysize2 = 0;
 	bool bRet = false;
 	bool bRet = false;
 
 
-	if(!filename || !mode || !field || !value) return false;
+	if(!path || !mode || !field || !value) return false;
 
 
-	filesize = sw_file_getSize(filename);
+	filesize = sw_file_getSize(path);
 	if(filesize < 0) filesize = 0;
 	if(filesize < 0) filesize = 0;
 
 
 	pBuf = (char *)malloc(filesize+MAX_LINE_CHARS*2);
 	pBuf = (char *)malloc(filesize+MAX_LINE_CHARS*2);
 	if(!pBuf) return false;
 	if(!pBuf) return false;
-	if(filesize > 0 && sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
+	if(filesize > 0 && sw_file_load(path, "rb", pBuf, filesize) != filesize) goto EndP;
 	memset(pBuf+filesize, 0, MAX_LINE_CHARS*2);
 	memset(pBuf+filesize, 0, MAX_LINE_CHARS*2);
 
 
 	if(section)
 	if(section)
@@ -183,7 +183,7 @@ bool sw_file_setCfgString(const char *filename, const char *section, const char
 	    if(!bRet) goto EndP;
 	    if(!bRet) goto EndP;
 	}
 	}
 
 
-	if(bRet) bRet = (sw_file_update(filename, "wb", pBuf, filesize+varysize1+varysize2) == filesize+varysize1+varysize2);
+	if(bRet) bRet = (sw_file_update(path, "wb", pBuf, filesize+varysize1+varysize2) == filesize+varysize1+varysize2);
 
 
 EndP:
 EndP:
 	free(pBuf);
 	free(pBuf);
@@ -191,21 +191,21 @@ EndP:
 }
 }
 
 
 /* 从配置或字典文件中删除(mode : "DelFieldByName", 根据"字段名"删除该"字段"; "DelFieldByValue", 根据"字段值"删除该"字段") */
 /* 从配置或字典文件中删除(mode : "DelFieldByName", 根据"字段名"删除该"字段"; "DelFieldByValue", 根据"字段值"删除该"字段") */
-bool sw_file_delCfgString(const char *filename, const char *section, const char *mode, const char *field, const char *value)
+bool sw_file_delCfgString(const char *path, const char *section, const char *mode, const char *field, const char *value)
 {
 {
 	char *pBuf = NULL, *pSection = NULL;
 	char *pBuf = NULL, *pSection = NULL;
 	int filesize = 0, varysize = 0;
 	int filesize = 0, varysize = 0;
 	bool bRet =  false;
 	bool bRet =  false;
 
 
-	if(!filename || !mode || (!field && !value)) return false;
+	if(!path || !mode || (!field && !value)) return false;
 
 
-	filesize = sw_file_getSize(filename);
+	filesize = sw_file_getSize(path);
 	if(filesize < 0) return false;
 	if(filesize < 0) return false;
 	else if(filesize == 0) return true;
 	else if(filesize == 0) return true;
 
 
 	pBuf = (char *)malloc(filesize+1);
 	pBuf = (char *)malloc(filesize+1);
 	if(!pBuf) return false;
 	if(!pBuf) return false;
-	if(sw_file_load(filename, "rb", pBuf, filesize) != filesize) goto EndP;
+	if(sw_file_load(path, "rb", pBuf, filesize) != filesize) goto EndP;
 	else pBuf[filesize] = '\0';
 	else pBuf[filesize] = '\0';
 
 
 	if(section)
 	if(section)
@@ -226,7 +226,7 @@ bool sw_file_delCfgString(const char *filename, const char *section, const char
 	    if(!bRet) goto EndP;
 	    if(!bRet) goto EndP;
 	}
 	}
 
 
-	if(bRet) bRet = (sw_file_update(filename, "wb", pBuf, filesize+varysize) == filesize+varysize);
+	if(bRet) bRet = (sw_file_update(path, "wb", pBuf, filesize+varysize) == filesize+varysize);
 
 
 EndP:
 EndP:
 	free(pBuf);
 	free(pBuf);

+ 8 - 8
swapi/swfile.h

@@ -17,13 +17,13 @@ extern "C"
 #endif
 #endif
 
 
 /* 装载文件 */
 /* 装载文件 */
-int sw_file_load(const char *filename, const char *mode, char *buf, int bufSize);
+int sw_file_load(const char *path, const char *mode, char *buf, int bufSize);
 
 
 /* 更新文件 */
 /* 更新文件 */
-int sw_file_update(const char *filename, const char *mode, const char *buf, int bufSize); 
+int sw_file_update(const char *path, const char *mode, const char *buf, int bufSize); 
 				 
 				 
 /* 得到文件大小 */
 /* 得到文件大小 */
-int sw_file_getSize(const char *filename);
+int sw_file_getSize(const char *path);
 
 
 /* 重命名文件 */
 /* 重命名文件 */
 int sw_file_rename(const char *oldName, const char *newName);
 int sw_file_rename(const char *oldName, const char *newName);
@@ -32,19 +32,19 @@ int sw_file_rename(const char *oldName, const char *newName);
 int sw_file_copy(const char *existingFilename, const char *newFilename, bool failIfExists);
 int sw_file_copy(const char *existingFilename, const char *newFilename, bool failIfExists);
 
 
 /* 删除文件 */
 /* 删除文件 */
-int sw_file_delete(const char *filename);
+int sw_file_delete(const char *path);
 
 
 /* 判断文件是否存在 */
 /* 判断文件是否存在 */
-bool sw_file_exists(const char *filename);
+bool sw_file_exists(const char *path);
 
 
 /* 读配置或字典文件(mode : "GetFieldName", 根据"字段值"获取"字段名"; "GetFieldValue", 根据"字段名"获取"字段值") */
 /* 读配置或字典文件(mode : "GetFieldName", 根据"字段值"获取"字段名"; "GetFieldValue", 根据"字段名"获取"字段值") */
-char *sw_file_getCfgString(const char *filename, const char *section, const char *mode, char *field, char *value, int resultBufSize);
+char *sw_file_getCfgString(const char *path, const char *section, const char *mode, char *field, char *value, int resultBufSize);
 
 
 /* 写配置或字典文件(mode : "SetFieldName", 根据"字段值"设置"字段名"; "SetFieldValue", 根据"字段名"设置"字段值") */
 /* 写配置或字典文件(mode : "SetFieldName", 根据"字段值"设置"字段名"; "SetFieldValue", 根据"字段名"设置"字段值") */
-bool sw_file_setCfgString(const char *filename, const char *section, const char *mode, const char *field, const char *value, bool autoAdd);
+bool sw_file_setCfgString(const char *path, const char *section, const char *mode, const char *field, const char *value, bool autoAdd);
 
 
 /* 从配置或字典文件中删除(mode : "DelFieldByName", 根据"字段名"删除该"字段"; "DelFieldByValue", 根据"字段值"删除该"字段") */
 /* 从配置或字典文件中删除(mode : "DelFieldByName", 根据"字段名"删除该"字段"; "DelFieldByValue", 根据"字段值"删除该"字段") */
-bool sw_file_delCfgString(const char *filename, const char *section, const char *mode, const char *field, const char *value);
+bool sw_file_delCfgString(const char *path, const char *section, const char *mode, const char *field, const char *value);
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }