Migrate pupload.c malloc/free to pmem_malloc/pmem_free

This commit is contained in:
Levi Neely 2026-03-08 17:53:56 +01:00
parent 9113a17ca8
commit 3f17f6333d
1 changed files with 53 additions and 52 deletions

View File

@ -40,6 +40,7 @@
#include "pfoldersync.h"
#include "plibs.h"
#include "plist.h"
#include "pmem.h"
#include "pnetlibs.h"
#include "ppathstatus.h"
#include "prun.h"
@ -170,7 +171,7 @@ static int64_t do_run_command_res(const char *cmd, size_t cmdlen,
if (unlikely(!res))
return -1;
result = papi_find_result2(res, "result", PARAM_NUM)->num;
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
if (unlikely(result)) {
psync_process_api_error(result);
pdbg_logf(D_WARNING, "command %s returned code %u", cmd, (unsigned)result);
@ -236,7 +237,7 @@ static int task_createfolder(psync_syncid_t syncid,
(unsigned)result, papi_find_result2(bres, "error", PARAM_STR)->str,
(unsigned long)parentfolderid, name);
psync_process_api_error(result);
free(bres);
pmem_free(PMEM_SUBSYS_UPLOAD, bres);
if (psync_handle_api_result(result) == PSYNC_NET_TEMPFAIL)
return -1;
else
@ -264,7 +265,7 @@ static int task_createfolder(psync_syncid_t syncid,
pdiff_unlock();
if (papi_find_result2(bres, "created", PARAM_BOOL)->num)
pdiff_wake();
free(bres);
pmem_free(PMEM_SUBSYS_UPLOAD, bres);
return ret;
}
}
@ -360,7 +361,7 @@ int handle_api_errors(sync_err_struct *err_struct) {
pdbg_logf(D_NOTICE, "Got sync path: [%s] Sync folder: [%s]", syncFolder,
folder);
event_data = malloc(sizeof(event_data_struct));
event_data = pmem_malloc(PMEM_SUBSYS_UPLOAD, sizeof(event_data_struct));
event_data->eventid = PEVENT_SYNC_RENAME_F;
event_data->str1 = strdup(err_struct->newName);
event_data->str2 = folder;
@ -371,7 +372,7 @@ int handle_api_errors(sync_err_struct *err_struct) {
ptevent_process(event_data);
free(event_data);
pmem_free(PMEM_SUBSYS_UPLOAD, event_data);
break;
default:
@ -428,7 +429,7 @@ static int task_renameremotefolder(psync_folderid_t folderid,
ret = handle_api_errors(&err_struct);
}
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
return ret;
}
@ -501,7 +502,7 @@ static void set_local_file_conflicted(psync_fileid_t localfileid,
psql_rollback();
else
psql_commit();
free(newpath);
pmem_free(PMEM_SUBSYS_UPLOAD, newpath);
}
static int copy_file(psync_fileid_t fileid, uint64_t hash,
@ -531,7 +532,7 @@ static int copy_file(psync_fileid_t fileid, uint64_t hash,
result = papi_find_result2(res, "result", PARAM_NUM)->num;
if (unlikely(result)) {
pdiff_unlock();
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
pdbg_logf(D_WARNING, "command copyfile returned code %u", (unsigned)result);
psync_process_api_error(result);
return 0;
@ -541,7 +542,7 @@ static int copy_file(psync_fileid_t fileid, uint64_t hash,
papi_find_result2(meta, "fileid", PARAM_NUM)->num,
papi_find_result2(meta, "hash", PARAM_NUM)->num);
pdiff_unlock();
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
pdiff_wake();
return 1;
}
@ -603,7 +604,7 @@ static int copy_file_if_exists(const unsigned char *hashhex, uint64_t fsize,
return -1;
result = papi_find_result2(res, "result", PARAM_NUM)->num;
if (unlikely(result)) {
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
pdbg_logf(D_WARNING, "command getfilesbychecksum returned code %u",
(unsigned)result);
psync_process_api_error(result);
@ -611,7 +612,7 @@ static int copy_file_if_exists(const unsigned char *hashhex, uint64_t fsize,
}
metas = papi_find_result2(res, "metadata", PARAM_ARRAY);
if (!metas->length) {
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
return 0;
}
meta = metas->array[0];
@ -629,7 +630,7 @@ static int copy_file_if_exists(const unsigned char *hashhex, uint64_t fsize,
(long unsigned)folderid, name);
}
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
return ret;
}
@ -691,7 +692,7 @@ static int upload_file(const char *localpath, const unsigned char *hashhex,
params, ARRAY_SIZE(params), fsize, 0)))
goto err1;
bw = 0;
buff = malloc(PSYNC_COPY_BUFFER_SIZE);
buff = pmem_malloc(PMEM_SUBSYS_UPLOAD, PSYNC_COPY_BUFFER_SIZE);
while (bw < fsize) {
if (unlikely(upload->stop)) {
pdbg_logf(D_NOTICE, "upload of %s stopped", localpath);
@ -716,7 +717,7 @@ static int upload_file(const char *localpath, const unsigned char *hashhex,
upload->uploaded += rrd;
add_bytes_uploaded(rrd);
}
free(buff);
pmem_free(PMEM_SUBSYS_UPLOAD, buff);
pfile_close(fd);
psync_set_default_sendbuf(api);
pdiff_lock();
@ -729,7 +730,7 @@ static int upload_file(const char *localpath, const unsigned char *hashhex,
}
result = papi_find_result2(res, "result", PARAM_NUM)->num;
if (unlikely(result)) {
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
pdbg_logf(D_WARNING, "command uploadfile returned code %u", (unsigned)result);
psync_process_api_error(result);
if (psync_handle_api_result(result) == PSYNC_NET_TEMPFAIL)
@ -769,16 +770,16 @@ static int upload_file(const char *localpath, const unsigned char *hashhex,
"uploaded file differs localsize=%lu, remotesize=%lu, localhash=%s, "
"remotehash=%s",
(unsigned long)fsize, (unsigned long)rsize, hashhex, hashhexsrv);
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
return -1;
}
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
pdiff_wake();
pdbg_logf(D_NOTICE, "file %s uploaded to %lu/%s", localpath,
(long unsigned)folderid, name);
return 0;
err2:
free(buff);
pmem_free(PMEM_SUBSYS_UPLOAD, buff);
err1:
psync_apipool_release_bad(api);
err0:
@ -805,7 +806,7 @@ static int upload_range(psock_t *api, psync_upload_range_list_t *r,
return PSYNC_NET_TEMPFAIL;
bw = 0;
buff = malloc(PSYNC_COPY_BUFFER_SIZE);
buff = pmem_malloc(PMEM_SUBSYS_UPLOAD, PSYNC_COPY_BUFFER_SIZE);
while (bw < r->len) {
if (unlikely(upload->stop)) {
pdbg_logf(D_NOTICE, "upload stopped");
@ -825,10 +826,10 @@ static int upload_range(psock_t *api, psync_upload_range_list_t *r,
upload->uploaded += rrd;
add_bytes_uploaded(rrd);
}
free(buff);
pmem_free(PMEM_SUBSYS_UPLOAD, buff);
return PSYNC_NET_OK;
err0:
free(buff);
pmem_free(PMEM_SUBSYS_UPLOAD, buff);
return PSYNC_NET_TEMPFAIL;
}
@ -939,7 +940,7 @@ static int upload_save(psock_t *api, psync_fileid_t localfileid,
ret = PSYNC_NET_OK;
pdiff_wake();
}
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
} else
ret = PSYNC_NET_TEMPFAIL;
pdiff_unlock();
@ -982,7 +983,7 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
goto err0;
result = papi_find_result2(res, "result", PARAM_NUM)->num;
if (unlikely(result)) {
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
psync_apipool_release(api);
psync_process_api_error(result);
pdbg_logf(D_WARNING, "upload_create returned %lu", (unsigned long)result);
@ -992,7 +993,7 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
return 0;
}
uploadid = papi_find_result2(res, "uploadid", PARAM_NUM)->num;
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
psql_start();
sql = psql_query_nolock("SELECT id FROM localfile WHERE id=?");
psql_bind_uint(sql, 1, localfileid);
@ -1015,7 +1016,7 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
}
psync_list_init(&rlist);
if (likely(uploadoffset < fsize)) {
le = malloc(sizeof(psync_upload_range_list_t));
le = pmem_malloc(PMEM_SUBSYS_UPLOAD, sizeof(psync_upload_range_list_t));
le->uploadoffset = uploadoffset;
le->off = uploadoffset;
le->len = fsize - uploadoffset;
@ -1084,12 +1085,12 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
if (fileid &&
psync_net_scan_file_for_blocks(api, &rlist, fileid, hash, fd) ==
PSYNC_NET_TEMPFAIL) {
free(nname);
pmem_free(PMEM_SUBSYS_UPLOAD, nname);
goto err1;
}
} else
psql_free(sql);
free(nname);
pmem_free(PMEM_SUBSYS_UPLOAD, nname);
} else
psql_free(sql);
}
@ -1103,10 +1104,10 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
psync_net_scan_upload_for_blocks(api, &rlist,
psync_get_result_cell(fr, id, 0),
fd) == PSYNC_NET_TEMPFAIL) {
free(fr);
pmem_free(PMEM_SUBSYS_UPLOAD, fr);
goto err1;
}
free(fr);
pmem_free(PMEM_SUBSYS_UPLOAD, fr);
}
rid = 0;
respwait = 0;
@ -1115,14 +1116,14 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
list) if ((le->type == PSYNC_URANGE_COPY_FILE ||
le->type == PSYNC_URANGE_COPY_UPLOAD) &&
le->len > PSYNC_MAX_COPY_FROM_REQ) {
le2 = malloc(sizeof(psync_upload_range_list_t));
le2 = pmem_malloc(PMEM_SUBSYS_UPLOAD, sizeof(psync_upload_range_list_t));
*le2 = *le;
le->len = PSYNC_MAX_COPY_FROM_REQ;
le2->off += PSYNC_MAX_COPY_FROM_REQ;
le2->len -= PSYNC_MAX_COPY_FROM_REQ;
psync_list_add_after(&le->list, &le2->list);
}
le = malloc(sizeof(psync_upload_range_list_t));
le = pmem_malloc(PMEM_SUBSYS_UPLOAD, sizeof(psync_upload_range_list_t));
le->type = PSYNC_URANGE_LAST;
psync_list_add_tail(&rlist, &le->list);
psync_list_for_each_element(le, &rlist, psync_upload_range_list_t, list) {
@ -1149,7 +1150,7 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
result = papi_find_result2(res, "result", PARAM_NUM)->num;
if (unlikely(result)) {
id = papi_find_result2(res, "id", PARAM_NUM)->num;
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
psync_process_api_error(result);
if (pdbg_unlikely(!id))
goto err1;
@ -1158,7 +1159,7 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
if (pdbg_unlikely(!res))
goto err1;
respwait--;
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
}
psync_list_for_each_element(le2, &rlist, psync_upload_range_list_t,
list) if (le2->id == id) {
@ -1188,7 +1189,7 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
"file size mismatch after upload, expected: %lu, got: %lu",
(unsigned long)fsize,
(unsigned long)papi_find_result2(res, "size", PARAM_NUM)->num);
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
goto err1;
} else if (unlikely(memcmp(
papi_find_result2(res, PSYNC_CHECKSUM, PARAM_STR)->str,
@ -1198,12 +1199,12 @@ static int upload_big_file(const char *localpath, const unsigned char *hashhex,
"hash mismatch after upload, expected: %." NTO_STR(
PSYNC_HASH_DIGEST_HEXLEN) "s, got: %." NTO_STR(PSYNC_HASH_DIGEST_HEXLEN) "s",
hashhex, papi_find_result2(res, PSYNC_CHECKSUM, PARAM_STR)->str);
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
goto err1;
} else
pdbg_assert(respwait == 0);
}
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
}
restart:
if (le->type == PSYNC_URANGE_UPLOAD) {
@ -1275,7 +1276,7 @@ static void delete_uploadid(psync_uploadid_t uploadid) {
PAPI_NUM("uploadid", uploadid)};
binresult *res;
res = psync_api_run_command("upload_delete", params);
free(res);
pmem_free(PMEM_SUBSYS_UPLOAD, res);
}
static void delete_uploadids(psync_fileid_t localfileid) {
@ -1294,7 +1295,7 @@ static void delete_uploadids(psync_fileid_t localfileid) {
psql_bind_uint(res, 1, localfileid);
psql_run_free(res);
}
free(rows);
pmem_free(PMEM_SUBSYS_UPLOAD, rows);
}
static void delete_from_localfile(psync_fileid_t localfileid) {
@ -1365,7 +1366,7 @@ static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid,
if (stat(localpath, &st)) {
pdbg_logf(D_NOTICE, "can not stat %s anymore, failing for now",
localpath);
free(localpath);
pmem_free(PMEM_SUBSYS_UPLOAD, localpath);
return -1;
}
ctime = ptimer_time();
@ -1374,7 +1375,7 @@ static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid,
if (ret == 10) {
pdbg_logf(D_NOTICE, "file %s kept changing %d times, skipping for now",
localpath, ret);
free(localpath);
pmem_free(PMEM_SUBSYS_UPLOAD, localpath);
return -1;
}
pdbg_logf(D_NOTICE, "file %s got old enough", localpath);
@ -1383,7 +1384,7 @@ static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid,
lock = psync_lock_file(localpath);
if (!lock) {
pdbg_logf(D_NOTICE, "file %s is currently locked, skipping for now", localpath);
free(localpath);
pmem_free(PMEM_SUBSYS_UPLOAD, localpath);
psys_sleep_milliseconds(PSYNC_SLEEP_ON_LOCKED_FILE);
return -1;
}
@ -1400,7 +1401,7 @@ static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid,
ret = psync_get_upload_checksum(uploadid, uhashhex, &ufsize);
if (ret == PSYNC_NET_TEMPFAIL) {
psync_unlock_file(lock);
free(localpath);
pmem_free(PMEM_SUBSYS_UPLOAD, localpath);
return -1;
} else if (ret == PSYNC_NET_PERMFAIL)
uploadid = 0;
@ -1414,7 +1415,7 @@ static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid,
pdbg_logf(D_WARNING, "could not open local file %s, deleting it from localfile",
localpath);
psync_unlock_file(lock);
free(localpath);
pmem_free(PMEM_SUBSYS_UPLOAD, localpath);
delete_from_localfile(localfileid);
return 0;
}
@ -1443,7 +1444,7 @@ static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid,
(unsigned long)localfileid);
psql_free(res);
psync_unlock_file(lock);
free(localpath);
pmem_free(PMEM_SUBSYS_UPLOAD, localpath);
return 0;
}
psql_free(res);
@ -1479,8 +1480,8 @@ static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid,
if (ret == 1 || ret == -1) {
psync_unlock_file(lock);
free(nname);
free(localpath);
pmem_free(PMEM_SUBSYS_UPLOAD, nname);
pmem_free(PMEM_SUBSYS_UPLOAD, localpath);
return ret == 1 ? 0 : -1;
}
memcpy(upload->hash, hashhex, PSYNC_HASH_DIGEST_HEXLEN);
@ -1522,8 +1523,8 @@ static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid,
}
}
psync_unlock_file(lock);
free(nname);
free(localpath);
pmem_free(PMEM_SUBSYS_UPLOAD, nname);
pmem_free(PMEM_SUBSYS_UPLOAD, localpath);
if (!ret)
delete_uploadids(localfileid);
return ret;
@ -1570,7 +1571,7 @@ static void task_run_upload_file_thread(void *ptr) {
wake_upload_when_ready();
pthread_mutex_unlock(&current_uploads_mutex);
pstatus_upload_recalc_async();
free(ut);
pmem_free(PMEM_SUBSYS_UPLOAD, ut);
}
static int task_run_uploadfile(uint64_t taskid, psync_syncid_t syncid,
@ -1598,7 +1599,7 @@ static int task_run_uploadfile(uint64_t taskid, psync_syncid_t syncid,
psql_bind_uint(res, 1, taskid);
psql_run_free(res);
len = strlen(filename);
ut = (upload_task_t *)malloc(offsetof(upload_task_t, filename) + len +
ut = (upload_task_t *)pmem_malloc(PMEM_SUBSYS_UPLOAD, offsetof(upload_task_t, filename) + len +
1);
ut->upllist.taskid = taskid;
ut->upllist.localfileid = localfileid;
@ -1628,7 +1629,7 @@ static int task_run_uploadfile(uint64_t taskid, psync_syncid_t syncid,
}
pthread_mutex_unlock(&current_uploads_mutex);
if (stop) {
free(ut);
pmem_free(PMEM_SUBSYS_UPLOAD, ut);
res = psql_prepare("UPDATE task SET inprogress=0 WHERE id=?");
psql_bind_uint(res, 1, taskid);
psql_run_free(res);
@ -1733,7 +1734,7 @@ static void upload_thread() {
}
} else if (type != PSYNC_UPLOAD_FILE)
psys_sleep_milliseconds(PSYNC_SLEEP_ON_FAILED_UPLOAD);
free(row);
pmem_free(PMEM_SUBSYS_UPLOAD, row);
continue;
}