1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-05 10:39:27 +02:00

unify argument order for file-functions

This commit is contained in:
B. Petersen 2018-09-02 22:06:43 +02:00
parent 8e5026a86b
commit 5463509062
13 changed files with 264 additions and 264 deletions

View file

@ -113,7 +113,7 @@ static int dc_poke_eml_file(dc_context_t* context, const char* filename)
return 0;
}
if (dc_read_file(filename, (void**)&data, &data_bytes, context) == 0) {
if (dc_read_file(context, filename, (void**)&data, &data_bytes) == 0) {
goto cleanup;
}
@ -600,7 +600,7 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
char* file_name = dc_mprintf("%s/autocrypt-setup-message.html", context->blobdir);
char* file_content = NULL;
if ((file_content=dc_render_setup_file(context, setup_code)) != NULL
&& dc_write_file(file_name, file_content, strlen(file_content), context)) {
&& dc_write_file(context, file_name, file_content, strlen(file_content))) {
ret = dc_mprintf("Setup message written to: %s\nSetup code: %s", file_name, setup_code);
}
else {
@ -1222,7 +1222,7 @@ char* dc_cmdline(dc_context_t* context, const char* cmdline)
{
if (arg1) {
unsigned char* buf = NULL; size_t buf_bytes; uint32_t w, h;
if (dc_read_file(arg1, (void**)&buf, &buf_bytes, context)) {
if (dc_read_file(context, arg1, (void**)&buf, &buf_bytes)) {
dc_get_filemeta(buf, buf_bytes, &w, &h);
ret = dc_mprintf("width=%i, height=%i", (int)w, (int)h);
}

View file

@ -72,12 +72,12 @@ static uintptr_t receive_event(dc_context_t* context, int event, uintptr_t data1
case DC_EVENT_HTTP_GET:
{
char* ret = NULL;
char* tempFile = dc_get_fine_pathNfilename(context->blobdir, "curl.result");
char* tempFile = dc_get_fine_pathNfilename(context, context->blobdir, "curl.result");
char* cmd = dc_mprintf("curl --silent --location --fail --insecure %s > %s", (char*)data1, tempFile); /* --location = follow redirects */
int error = system(cmd);
if (error == 0) { /* -1=system() error, !0=curl errors forced by -f, 0=curl success */
size_t bytes = 0;
dc_read_file(tempFile, (void**)&ret, &bytes, context);
dc_read_file(context, tempFile, (void**)&ret, &bytes);
}
free(cmd);
free(tempFile);

View file

@ -34,7 +34,7 @@
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add option="-fexceptions -Wno-unused-function -Wno-misleading-indentation -Wno-deprecated-declarations" />
<Add option="-DHAVE_CONFIG_H" />
<Add option="-DDC_USE_MIME_DEBUG" />
<Add option="-DHAVE_ICONV" />

View file

@ -2096,7 +2096,7 @@ uint32_t dc_send_msg(dc_context_t* context, uint32_t chat_id, dc_msg_t* msg)
&& (dc_param_get_int(msg->param, DC_PARAM_WIDTH, 0)<=0 || dc_param_get_int(msg->param, DC_PARAM_HEIGHT, 0)<=0)) {
/* set width/height of images, if not yet done */
unsigned char* buf = NULL; size_t buf_bytes; uint32_t w, h;
if (dc_read_file(pathNfilename, (void**)&buf, &buf_bytes, msg->context)) {
if (dc_read_file(context, pathNfilename, (void**)&buf, &buf_bytes)) {
if (dc_get_filemeta(buf, buf_bytes, &w, &h)) {
dc_param_set_int(msg->param, DC_PARAM_WIDTH, w);
dc_param_set_int(msg->param, DC_PARAM_HEIGHT, h);

View file

@ -257,7 +257,7 @@ int dc_open(dc_context_t* context, const char* dbfile, const char* blobdir)
}
else {
context->blobdir = dc_mprintf("%s-blobs", dbfile);
dc_create_folder(context->blobdir, context);
dc_create_folder(context, context->blobdir);
}
/* Create/open sqlite database, this may already use the blobdir */

View file

@ -489,8 +489,8 @@ char* dc_initiate_key_transfer(dc_context_t* context)
CHECK_EXIT
if ((setup_file_name=dc_get_fine_pathNfilename(context->blobdir, "autocrypt-setup-message.html"))==NULL
|| !dc_write_file(setup_file_name, setup_file_content, strlen(setup_file_content), context)) {
if ((setup_file_name=dc_get_fine_pathNfilename(context, context->blobdir, "autocrypt-setup-message.html"))==NULL
|| !dc_write_file(context, setup_file_name, setup_file_content, strlen(setup_file_content))) {
goto cleanup;
}
@ -651,7 +651,7 @@ int dc_continue_key_transfer(dc_context_t* context, uint32_t msg_id, const char*
goto cleanup;
}
if (!dc_read_file(filename, (void**)&filecontent, &filebytes, msg->context) || filecontent==NULL || filebytes <= 0) {
if (!dc_read_file(context, filename, (void**)&filecontent, &filebytes) || filecontent==NULL || filebytes <= 0) {
dc_log_error(context, 0, "Cannot read Autocrypt Setup Message file.");
goto cleanup;
}
@ -697,7 +697,7 @@ static void export_key_to_asc_file(dc_context_t* context, const char* dir, int i
file_name = dc_mprintf("%s/%s-key-%i.asc", dir, key->type==DC_KEY_PUBLIC? "public" : "private", id);
}
dc_log_info(context, 0, "Exporting key %s", file_name);
dc_delete_file(file_name, context);
dc_delete_file(context, file_name);
if (dc_key_render_asc_to_file(key, file_name, context)) {
context->cb(context, DC_EVENT_IMEX_FILE_WRITTEN, (uintptr_t)file_name, 0);
dc_log_error(context, 0, "Cannot write key to %s", file_name);
@ -786,7 +786,7 @@ static int import_self_keys(dc_context_t* context, const char* dir_name)
free(buf);
buf = NULL;
if (!dc_read_file(path_plus_name, (void**)&buf, &buf_bytes, context)
if (!dc_read_file(context, path_plus_name, (void**)&buf, &buf_bytes)
|| buf_bytes < 50) {
continue;
}
@ -874,7 +874,7 @@ static int export_backup(dc_context_t* context, const char* dir)
char buffer[256];
timeinfo = localtime(&now);
strftime(buffer, 256, DC_BAK_PREFIX "-%Y-%m-%d." DC_BAK_SUFFIX, timeinfo);
if ((dest_pathNfilename=dc_get_fine_pathNfilename(dir, buffer))==NULL) {
if ((dest_pathNfilename=dc_get_fine_pathNfilename(context, dir, buffer))==NULL) {
dc_log_error(context, 0, "Cannot get backup file name.");
goto cleanup;
}
@ -885,7 +885,7 @@ static int export_backup(dc_context_t* context, const char* dir)
closed = 1;
dc_log_info(context, 0, "Backup \"%s\" to \"%s\".", context->dbfile, dest_pathNfilename);
if (!dc_copy_file(context->dbfile, dest_pathNfilename, context)) {
if (!dc_copy_file(context, context->dbfile, dest_pathNfilename)) {
goto cleanup; /* error already logged */
}
@ -949,7 +949,7 @@ static int export_backup(dc_context_t* context, const char* dir)
free(curr_pathNfilename);
curr_pathNfilename = dc_mprintf("%s/%s", context->blobdir, name);
free(buf);
if (!dc_read_file(curr_pathNfilename, &buf, &buf_bytes, context) || buf==NULL || buf_bytes<=0) {
if (!dc_read_file(context, curr_pathNfilename, &buf, &buf_bytes) || buf==NULL || buf_bytes<=0) {
continue;
}
@ -981,7 +981,7 @@ cleanup:
sqlite3_finalize(stmt);
dc_sqlite3_close(dest_sql);
dc_sqlite3_unref(dest_sql);
if (delete_dest_file) { dc_delete_file(dest_pathNfilename, context); }
if (delete_dest_file) { dc_delete_file(context, dest_pathNfilename); }
free(dest_pathNfilename);
free(curr_pathNfilename);
@ -1037,15 +1037,15 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
dc_sqlite3_close(context->sql);
}
dc_delete_file(context->dbfile, context);
dc_delete_file(context, context->dbfile);
if (dc_file_exist(context->dbfile)) {
if (dc_file_exist(context, context->dbfile)) {
dc_log_error(context, 0, "Cannot import backups: Cannot delete the old file.");
goto cleanup;
}
/* copy the database file */
if (!dc_copy_file(backup_to_import, context->dbfile, context)) {
if (!dc_copy_file(context, backup_to_import, context->dbfile)) {
goto cleanup; /* error already logged */
}
@ -1077,7 +1077,7 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
if (file_bytes > 0 && file_content) {
free(pathNfilename);
pathNfilename = dc_mprintf("%s/%s", context->blobdir, file_name);
if (!dc_write_file(pathNfilename, file_content, file_bytes, context)) {
if (!dc_write_file(context, pathNfilename, file_content, file_bytes)) {
dc_log_error(context, 0, "Storage full? Cannot write file %s with %i bytes.", pathNfilename, file_bytes);
goto cleanup; /* otherwise the user may believe the stuff is imported correctly, but there are files missing ... */
}
@ -1237,7 +1237,7 @@ void dc_job_do_DC_JOB_IMEX_IMAP(dc_context_t* context, dc_job_t* job)
goto cleanup;
}
/* also make sure, the directory for exporting exists */
dc_create_folder(param1, context);
dc_create_folder(context, param1);
}
switch (what)

View file

@ -181,21 +181,21 @@ static void dc_job_do_DC_JOB_DELETE_MSG_ON_IMAP(dc_context_t* context, dc_job_t*
if (!file_used_by_other_msgs)
{
dc_delete_file(pathNfilename, context);
dc_delete_file(context, pathNfilename);
char* increation_file = dc_mprintf("%s.increation", pathNfilename);
dc_delete_file(increation_file, context);
dc_delete_file(context, increation_file);
free(increation_file);
char* filenameOnly = dc_get_filename(pathNfilename);
if (msg->type==DC_MSG_VOICE) {
char* waveform_file = dc_mprintf("%s/%s.waveform", context->blobdir, filenameOnly);
dc_delete_file(waveform_file, context);
dc_delete_file(context, waveform_file);
free(waveform_file);
}
else if (msg->type==DC_MSG_VIDEO) {
char* preview_file = dc_mprintf("%s/%s-preview.jpg", context->blobdir, filenameOnly);
dc_delete_file(preview_file, context);
dc_delete_file(context, preview_file);
free(preview_file);
}
free(filenameOnly);

View file

@ -169,7 +169,7 @@ int dc_key_set_from_file(dc_key_t* key, const char* pathNfilename, dc_context_t*
goto cleanup;
}
if (!dc_read_file(pathNfilename, (void**)&buf, &buf_bytes, context)
if (!dc_read_file(context, pathNfilename, (void**)&buf, &buf_bytes)
|| buf_bytes < 50) {
goto cleanup; /* error is already loged */
}
@ -429,7 +429,7 @@ int dc_key_render_asc_to_file(const dc_key_t* key, const char* file, dc_context_
goto cleanup;
}
if (!dc_write_file(file, file_content, strlen(file_content), context)) {
if (!dc_write_file(context, file, file_content, strlen(file_content))) {
dc_log_error(context, 0, "Cannot write key to %s", file);
goto cleanup;
}

View file

@ -321,7 +321,7 @@ static int is_file_size_okay(const dc_msg_t* msg)
{
int file_size_okay = 1;
char* pathNfilename = dc_param_get(msg->param, DC_PARAM_FILE, NULL);
uint64_t bytes = dc_get_filebytes(pathNfilename);
uint64_t bytes = dc_get_filebytes(msg->context, pathNfilename);
if (bytes>DC_MSGSIZE_UPPER_LIMIT) {
file_size_okay = 0;

View file

@ -951,12 +951,12 @@ static void do_add_single_file_part(dc_mimeparser_t* parser, int msg_type, int m
char* pathNfilename = NULL;
/* create a free file name to use */
if ((pathNfilename=dc_get_fine_pathNfilename(parser->blobdir, desired_filename))==NULL) {
if ((pathNfilename=dc_get_fine_pathNfilename(parser->context, parser->blobdir, desired_filename))==NULL) {
goto cleanup;
}
/* copy data to file */
if (dc_write_file(pathNfilename, decoded_data, decoded_data_bytes, parser->context)==0) {
if (dc_write_file(parser->context, pathNfilename, decoded_data, decoded_data_bytes)==0) {
goto cleanup;
}

View file

@ -392,7 +392,7 @@ uint64_t dc_msg_get_filebytes(const dc_msg_t* msg)
goto cleanup;
}
ret = dc_get_filebytes(file);
ret = dc_get_filebytes(msg->context, file);
cleanup:
free(file);
@ -786,7 +786,7 @@ char* dc_msg_get_setupcodebegin(const dc_msg_t* msg)
goto cleanup;
}
if (!dc_read_file(filename, (void**)&buf, &buf_bytes, msg->context) || buf==NULL || buf_bytes <= 0) {
if (!dc_read_file(msg->context, filename, (void**)&buf, &buf_bytes) || buf==NULL || buf_bytes <= 0) {
goto cleanup;
}
@ -1065,7 +1065,7 @@ int dc_msg_is_increation(const dc_msg_t* msg)
char* pathNfilename = dc_param_get(msg->param, DC_PARAM_FILE, NULL);
if (pathNfilename) {
char* totest = dc_mprintf("%s.increation", pathNfilename);
if (dc_file_exist(totest)) {
if (dc_file_exist(msg->context, totest)) {
is_increation = 1;
}
free(totest);
@ -1615,7 +1615,7 @@ char* dc_get_msg_info(dc_context_t* context, uint32_t msg_id)
/* add file info */
if ((p=dc_param_get(msg->param, DC_PARAM_FILE, NULL))!=NULL) {
dc_strbuilder_catf(&ret, "\nFile: %s, %i bytes\n", p, (int)dc_get_filebytes(p));
dc_strbuilder_catf(&ret, "\nFile: %s, %i bytes\n", p, (int)dc_get_filebytes(context, p));
free(p);
}

View file

@ -1010,26 +1010,15 @@ void dc_ensure_no_slash(char* pathNfilename)
}
int dc_file_exist(const char* pathNfilename)
void dc_validate_filename(char* filename)
{
struct stat st;
if (stat(pathNfilename, &st)==0) {
return 1; /* the size, however, may be 0 */
/* function modifies the given buffer and replaces all characters not valid in filenames by a "-" */
char* p1 = filename;
while (*p1) {
if (*p1=='/' || *p1=='\\' || *p1==':') {
*p1 = '-';
}
else {
return 0;
}
}
uint64_t dc_get_filebytes(const char* pathNfilename)
{
struct stat st;
if (stat(pathNfilename, &st)==0) {
return (uint64_t)st.st_size;
}
else {
return 0;
p1++;
}
}
@ -1051,97 +1040,6 @@ char* dc_get_filename(const char* pathNfilename)
}
int dc_delete_file(const char* pathNfilename, dc_context_t* log/*may be NULL*/)
{
if (pathNfilename==NULL) {
return 0;
}
if (remove(pathNfilename)!=0) {
dc_log_warning(log, 0, "Cannot delete \"%s\".", pathNfilename);
return 0;
}
return 1;
}
int dc_copy_file(const char* src, const char* dest, dc_context_t* log/*may be NULL*/)
{
int success = 0;
int fd_src = -1;
int fd_dest = -1;
#define DC_COPY_BUF_SIZE 4096
char buf[DC_COPY_BUF_SIZE];
size_t bytes_read = 0;
int anything_copied = 0;
if (src==NULL || dest==NULL) {
return 0;
}
if ((fd_src=open(src, O_RDONLY)) < 0) {
dc_log_error(log, 0, "Cannot open source file \"%s\".", src);
goto cleanup;
}
if ((fd_dest=open(dest, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
dc_log_error(log, 0, "Cannot open destination file \"%s\".", dest);
goto cleanup;
}
while ((bytes_read=read(fd_src, buf, DC_COPY_BUF_SIZE)) > 0) {
if (write(fd_dest, buf, bytes_read)!=bytes_read) {
dc_log_error(log, 0, "Cannot write %i bytes to \"%s\".", bytes_read, dest);
}
anything_copied = 1;
}
if (!anything_copied) {
/* not a single byte copied -> check if the source is empty, too */
close(fd_src);
fd_src = -1;
if (dc_get_filebytes(src)!=0) {
dc_log_error(log, 0, "Different size information for \"%s\".", bytes_read, dest);
goto cleanup;
}
}
success = 1;
cleanup:
if (fd_src >= 0) { close(fd_src); }
if (fd_dest >= 0) { close(fd_dest); }
return success;
}
int dc_create_folder(const char* pathNfilename, dc_context_t* log)
{
struct stat st;
if (stat(pathNfilename, &st)==-1) {
if (mkdir(pathNfilename, 0755)!=0) {
dc_log_warning(log, 0, "Cannot create directory \"%s\".", pathNfilename);
return 0;
}
}
return 1;
}
char* dc_get_filesuffix_lc(const char* pathNfilename)
{
if (pathNfilename) {
const char* p = strrchr(pathNfilename, '.'); /* use the last point, we're interesting the "main" type */
if (p) {
p++;
return dc_strlower(p); /* in contrast to dc_split_filename() we return the lowercase suffix */
}
}
return NULL;
}
void dc_split_filename(const char* pathNfilename, char** ret_basename, char** ret_all_suffixes_incl_dot)
{
/* splits a filename into basename and all suffixes, eg. "/path/foo.tar.gz" is split into "foo.tar" and ".gz",
@ -1167,117 +1065,16 @@ void dc_split_filename(const char* pathNfilename, char** ret_basename, char** re
}
void dc_validate_filename(char* filename)
char* dc_get_filesuffix_lc(const char* pathNfilename)
{
/* function modifies the given buffer and replaces all characters not valid in filenames by a "-" */
char* p1 = filename;
while (*p1) {
if (*p1=='/' || *p1=='\\' || *p1==':') {
*p1 = '-';
if (pathNfilename) {
const char* p = strrchr(pathNfilename, '.'); /* use the last point, we're interesting the "main" type */
if (p) {
p++;
return dc_strlower(p); /* in contrast to dc_split_filename() we return the lowercase suffix */
}
p1++;
}
}
char* dc_get_fine_pathNfilename(const char* folder, const char* desired_filenameNsuffix__)
{
char* ret = NULL;
char* filenameNsuffix = NULL;
char* basename = NULL;
char* dotNSuffix = NULL;
time_t now = time(NULL);
struct stat st;
int i = 0;
filenameNsuffix = dc_strdup(desired_filenameNsuffix__);
dc_validate_filename(filenameNsuffix);
dc_split_filename(filenameNsuffix, &basename, &dotNSuffix);
for (i = 0; i < 1000 /*no deadlocks, please*/; i++) {
if (i) {
time_t idx = i<100? i : now+i;
ret = dc_mprintf("%s/%s-%lu%s", folder, basename, (unsigned long)idx, dotNSuffix);
}
else {
ret = dc_mprintf("%s/%s%s", folder, basename, dotNSuffix);
}
if (stat(ret, &st)==-1) {
goto cleanup; /* fine filename found */
}
free(ret); /* try over with the next index */
ret = NULL;
}
cleanup:
free(filenameNsuffix);
free(basename);
free(dotNSuffix);
return ret;
}
int dc_write_file(const char* pathNfilename, const void* buf, size_t buf_bytes, dc_context_t* log)
{
int success = 0;
FILE* f = fopen(pathNfilename, "wb");
if (f) {
if (fwrite(buf, 1, buf_bytes, f)==buf_bytes) {
success = 1;
}
else {
dc_log_warning(log, 0, "Cannot write %lu bytes to \"%s\".", (unsigned long)buf_bytes, pathNfilename);
}
fclose(f);
}
else {
dc_log_warning(log, 0, "Cannot open \"%s\" for writing.", pathNfilename);
}
return success;
}
int dc_read_file(const char* pathNfilename, void** buf, size_t* buf_bytes, dc_context_t* log)
{
int success = 0;
if (pathNfilename==NULL || buf==NULL || buf_bytes==NULL) {
return 0; /* do not go to cleanup as this would dereference "buf" and "buf_bytes" */
}
*buf = NULL;
*buf_bytes = 0;
FILE* f = fopen(pathNfilename, "rb");
if (f==NULL) { goto cleanup; }
fseek(f, 0, SEEK_END);
*buf_bytes = ftell(f);
fseek(f, 0, SEEK_SET);
if (*buf_bytes<=0) { goto cleanup; }
*buf = malloc( (*buf_bytes) + 1 /*be pragmatic and terminate all files by a null - fine for texts and does not hurt for the rest */);
if (*buf==NULL) { goto cleanup; }
((char*)*buf)[*buf_bytes /*we allocated one extra byte above*/] = 0;
if (fread(*buf, 1, *buf_bytes, f)!=*buf_bytes) { goto cleanup; }
success = 1;
cleanup:
if (f) {
fclose(f);
}
if (success==0) {
free(*buf);
*buf = NULL;
*buf_bytes = 0;
dc_log_warning(log, 0, "Cannot read \"%s\" or file is empty.", pathNfilename);
}
return success; /* buf must be free()'d by the caller */
return NULL;
}
@ -1329,3 +1126,205 @@ int dc_get_filemeta(const void* buf_start, size_t buf_bytes, uint32_t* ret_width
return 0;
}
int dc_file_exist(dc_context_t* context, const char* pathNfilename)
{
struct stat st;
if (stat(pathNfilename, &st)==0) {
return 1; /* the size, however, may be 0 */
}
else {
return 0;
}
}
uint64_t dc_get_filebytes(dc_context_t* context, const char* pathNfilename)
{
struct stat st;
if (stat(pathNfilename, &st)==0) {
return (uint64_t)st.st_size;
}
else {
return 0;
}
}
int dc_delete_file(dc_context_t* context, const char* pathNfilename)
{
if (pathNfilename==NULL) {
return 0;
}
if (remove(pathNfilename)!=0) {
dc_log_warning(context, 0, "Cannot delete \"%s\".", pathNfilename);
return 0;
}
return 1;
}
int dc_copy_file(dc_context_t* context, const char* src, const char* dest)
{
int success = 0;
int fd_src = -1;
int fd_dest = -1;
#define DC_COPY_BUF_SIZE 4096
char buf[DC_COPY_BUF_SIZE];
size_t bytes_read = 0;
int anything_copied = 0;
if (src==NULL || dest==NULL) {
return 0;
}
if ((fd_src=open(src, O_RDONLY)) < 0) {
dc_log_error(context, 0, "Cannot open source file \"%s\".", src);
goto cleanup;
}
if ((fd_dest=open(dest, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
dc_log_error(context, 0, "Cannot open destination file \"%s\".", dest);
goto cleanup;
}
while ((bytes_read=read(fd_src, buf, DC_COPY_BUF_SIZE)) > 0) {
if (write(fd_dest, buf, bytes_read)!=bytes_read) {
dc_log_error(context, 0, "Cannot write %i bytes to \"%s\".", bytes_read, dest);
}
anything_copied = 1;
}
if (!anything_copied) {
/* not a single byte copied -> check if the source is empty, too */
close(fd_src);
fd_src = -1;
if (dc_get_filebytes(context, src)!=0) {
dc_log_error(context, 0, "Different size information for \"%s\".", bytes_read, dest);
goto cleanup;
}
}
success = 1;
cleanup:
if (fd_src >= 0) { close(fd_src); }
if (fd_dest >= 0) { close(fd_dest); }
return success;
}
int dc_create_folder(dc_context_t* context, const char* pathNfilename)
{
struct stat st;
if (stat(pathNfilename, &st)==-1) {
if (mkdir(pathNfilename, 0755)!=0) {
dc_log_warning(context, 0, "Cannot create directory \"%s\".", pathNfilename);
return 0;
}
}
return 1;
}
int dc_write_file(dc_context_t* context, const char* pathNfilename, const void* buf, size_t buf_bytes)
{
int success = 0;
FILE* f = fopen(pathNfilename, "wb");
if (f) {
if (fwrite(buf, 1, buf_bytes, f)==buf_bytes) {
success = 1;
}
else {
dc_log_warning(context, 0, "Cannot write %lu bytes to \"%s\".", (unsigned long)buf_bytes, pathNfilename);
}
fclose(f);
}
else {
dc_log_warning(context, 0, "Cannot open \"%s\" for writing.", pathNfilename);
}
return success;
}
int dc_read_file(dc_context_t* context, const char* pathNfilename, void** buf, size_t* buf_bytes)
{
int success = 0;
if (pathNfilename==NULL || buf==NULL || buf_bytes==NULL) {
return 0; /* do not go to cleanup as this would dereference "buf" and "buf_bytes" */
}
*buf = NULL;
*buf_bytes = 0;
FILE* f = fopen(pathNfilename, "rb");
if (f==NULL) { goto cleanup; }
fseek(f, 0, SEEK_END);
*buf_bytes = ftell(f);
fseek(f, 0, SEEK_SET);
if (*buf_bytes<=0) { goto cleanup; }
*buf = malloc( (*buf_bytes) + 1 /*be pragmatic and terminate all files by a null - fine for texts and does not hurt for the rest */);
if (*buf==NULL) { goto cleanup; }
((char*)*buf)[*buf_bytes /*we allocated one extra byte above*/] = 0;
if (fread(*buf, 1, *buf_bytes, f)!=*buf_bytes) { goto cleanup; }
success = 1;
cleanup:
if (f) {
fclose(f);
}
if (success==0) {
free(*buf);
*buf = NULL;
*buf_bytes = 0;
dc_log_warning(context, 0, "Cannot read \"%s\" or file is empty.", pathNfilename);
}
return success; /* buf must be free()'d by the caller */
}
char* dc_get_fine_pathNfilename(dc_context_t* context, const char* pathNfolder, const char* desired_filenameNsuffix__)
{
char* ret = NULL;
char* filenameNsuffix = NULL;
char* basename = NULL;
char* dotNSuffix = NULL;
time_t now = time(NULL);
struct stat st;
int i = 0;
filenameNsuffix = dc_strdup(desired_filenameNsuffix__);
dc_validate_filename(filenameNsuffix);
dc_split_filename(filenameNsuffix, &basename, &dotNSuffix);
for (i = 0; i < 1000 /*no deadlocks, please*/; i++) {
if (i) {
time_t idx = i<100? i : now+i;
ret = dc_mprintf("%s/%s-%lu%s", pathNfolder, basename, (unsigned long)idx, dotNSuffix);
}
else {
ret = dc_mprintf("%s/%s%s", pathNfolder, basename, dotNSuffix);
}
if (stat(ret, &st)==-1) {
goto cleanup; /* fine filename found */
}
free(ret); /* try over with the next index */
ret = NULL;
}
cleanup:
free(filenameNsuffix);
free(basename);
free(dotNSuffix);
return ret;
}

View file

@ -96,18 +96,19 @@ char* dc_extract_grpid_from_rfc724_mid_list(const clist* rfc724_mid_list);
/* file tools */
void dc_ensure_no_slash (char* pathNfilename);
int dc_file_exist (const char* pathNfilename);
uint64_t dc_get_filebytes (const char* pathNfilename);
char* dc_get_filename (const char* pathNfilename); /* the return value must be free()'d */
int dc_delete_file (const char* pathNFilename, dc_context_t* log);
int dc_copy_file (const char* src_pathNFilename, const char* dest_pathNFilename, dc_context_t* log);
int dc_create_folder (const char* pathNfilename, dc_context_t* log);
int dc_write_file (const char* pathNfilename, const void* buf, size_t buf_bytes, dc_context_t* log);
int dc_read_file (const char* pathNfilename, void** buf, size_t* buf_bytes, dc_context_t* log);
char* dc_get_filesuffix_lc (const char* pathNfilename); /* the returned suffix is lower-case */
void dc_split_filename (const char* pathNfilename, char** ret_basename, char** ret_all_suffixes_incl_dot); /* the case of the suffix is preserved! */
void dc_validate_filename (char* filename);
char* dc_get_filename (const char* pathNfilename);
void dc_split_filename (const char* pathNfilename, char** ret_basename, char** ret_all_suffixes_incl_dot); // the case of the suffix is preserved
char* dc_get_filesuffix_lc (const char* pathNfilename); // the returned suffix is lower-case
int dc_get_filemeta (const void* buf, size_t buf_bytes, uint32_t* ret_width, uint32_t *ret_height);
char* dc_get_fine_pathNfilename (const char* folder, const char* desired_name);
int dc_file_exist (dc_context_t*, const char* pathNfilename);
uint64_t dc_get_filebytes (dc_context_t*, const char* pathNfilename);
int dc_delete_file (dc_context_t*, const char* pathNFilename);
int dc_copy_file (dc_context_t*, const char* pathNFilename, const char* dest_pathNFilename);
int dc_create_folder (dc_context_t*, const char* pathNfilename);
int dc_write_file (dc_context_t*, const char* pathNfilename, const void* buf, size_t buf_bytes);
int dc_read_file (dc_context_t*, const char* pathNfilename, void** buf, size_t* buf_bytes);
char* dc_get_fine_pathNfilename (dc_context_t*, const char* pathNfolder, const char* desired_name);
/* macros */