text
stringlengths
0
357
}
if ((fd_src=open(src_abs, O_RDONLY)) < 0) {
dc_log_error(context, 0, "Cannot open source file \"%s\".", src);
goto cleanup;
}
if ((fd_dest=open(dest_abs, 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); }
free(src_abs);
free(dest_abs);
return success;
}
int dc_create_folder(dc_context_t* context, const char* pathNfilename)
{
int success = 0;
char* pathNfilename_abs = NULL;
if ((pathNfilename_abs=dc_get_abs_path(context, pathNfilename))==NULL) {
goto cleanup;
}
struct stat st;
if (stat(pathNfilename_abs, &st)==-1) {
if (mkdir(pathNfilename_abs, 0755)!=0) {
dc_log_warning(context, 0, "Cannot create directory \"%s\".", pathNfilename);
goto cleanup;
}
}
success = 1;
cleanup:
free(pathNfilename_abs);
return success;
}
int dc_write_file(dc_context_t* context, const char* pathNfilename, const void* buf, size_t buf_bytes)
{
int success = 0;
char* pathNfilename_abs = NULL;
if ((pathNfilename_abs=dc_get_abs_path(context, pathNfilename))==NULL) {
goto cleanup;
}
FILE* f = fopen(pathNfilename_abs, "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);
}
cleanup:
free(pathNfilename_abs);
return success;
}
int dc_read_file(dc_context_t* context, const char* pathNfilename, void** buf, size_t* buf_bytes)
{
int success = 0;
char* pathNfilename_abs = NULL;
FILE* f = NULL;