text stringlengths 0 357 |
|---|
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; |
if ((pathNfilename_abs=dc_get_abs_path(context, pathNfilename))==NULL) { |
goto cleanup; |
} |
f = fopen(pathNfilename_abs, "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); |
} |
free(pathNfilename_abs); |
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* pathNfolder_wo_slash = NULL; |
char* filenameNsuffix = NULL; |
char* basename = NULL; |
char* dotNSuffix = NULL; |
time_t now = time(NULL); |
int i = 0; |
pathNfolder_wo_slash = dc_strdup(pathNfolder); |
dc_ensure_no_slash(pathNfolder_wo_slash); |
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_wo_slash, basename, (unsigned long)idx, dotNSuffix); |
} |
else { |
ret = dc_mprintf("%s/%s%s", pathNfolder_wo_slash, basename, dotNSuffix); |
} |
if (!dc_file_exist(context, ret)) { |
goto cleanup; /* fine filename found */ |
} |
free(ret); /* try over with the next index */ |
ret = NULL; |
} |
cleanup: |
free(filenameNsuffix); |
free(basename); |
free(dotNSuffix); |
free(pathNfolder_wo_slash); |
return ret; |
} |
void dc_make_rel_path(dc_context_t* context, char** path) |
{ |
if (context==NULL || path==NULL || *path==NULL) { |
return; |
} |
if (strncmp(*path, context->blobdir, strlen(context->blobdir))==0) { |
dc_str_replace(path, context->blobdir, "$BLOBDIR"); |
} |
} |
/** |
* Check if a path describes a file in the blob directory. |
* The path can be absolute or relative (starting with `$BLOBDIR`). |
* The function does not check if the file really exists. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.