瀏覽代碼

libobs/util: Use os_safe_replace in safe file writes

jp9000 8 年之前
父節點
當前提交
6d3777129d
共有 2 個文件被更改,包括 4 次插入15 次删除
  1. 2 6
      libobs/util/config-file.c
  2. 2 9
      libobs/util/platform.c

+ 2 - 6
libobs/util/config-file.c

@@ -445,14 +445,10 @@ int config_save_safe(config_t *config, const char *temp_ext,
 		if (*backup_ext != '.')
 		if (*backup_ext != '.')
 			dstr_cat(&backup_file, ".");
 			dstr_cat(&backup_file, ".");
 		dstr_cat(&backup_file, backup_ext);
 		dstr_cat(&backup_file, backup_ext);
-
-		os_unlink(backup_file.array);
-		os_rename(file, backup_file.array);
-	} else {
-		os_unlink(file);
 	}
 	}
 
 
-	os_rename(temp_file.array, file);
+	if (os_safe_replace(file, temp_file.array, backup_file.array) != 0)
+		ret = CONFIG_ERROR;
 
 
 cleanup:
 cleanup:
 	pthread_mutex_unlock(&config->mutex);
 	pthread_mutex_unlock(&config->mutex);

+ 2 - 9
libobs/util/platform.c

@@ -297,17 +297,10 @@ bool os_quick_write_utf8_file_safe(const char *path, const char *str,
 		if (*backup_ext != '.')
 		if (*backup_ext != '.')
 			dstr_cat(&backup_path, ".");
 			dstr_cat(&backup_path, ".");
 		dstr_cat(&backup_path, backup_ext);
 		dstr_cat(&backup_path, backup_ext);
-
-		os_unlink(backup_path.array);
-		os_rename(path, backup_path.array);
-
-		dstr_free(&backup_path);
-	} else {
-		os_unlink(path);
 	}
 	}
 
 
-	os_rename(temp_path.array, path);
-	success = true;
+	if (os_safe_replace(path, temp_path.array, backup_path.array) == 0)
+		success = true;
 
 
 cleanup:
 cleanup:
 	dstr_free(&backup_path);
 	dstr_free(&backup_path);