Commit c3728834 authored by Tobias Ellinghaus's avatar Tobias Ellinghaus

Use g_fopen() instead of fopen()

This fixes a bunch of bugs with the Windows version when there are
non-ASCII characters in the pathname, for example not being able to load
the config when the username contains such a char.
This also poisons fopen() so it can't be used in C files any more.
parent 10c976ae
......@@ -146,7 +146,7 @@ chart_t *parse_cht(const char *filename)
chart_t *result = (chart_t *)calloc(1, sizeof(chart_t));
int lineno = 0;
FILE *fp = fopen(filename, "rb");
FILE *fp = g_fopen(filename, "rb");
if(!fp)
{
fprintf(stderr, "error opening `%s'\n", filename);
......
......@@ -559,7 +559,7 @@ static void export_style(dt_lut_t *self, const char *filename, const char *name,
{
int num = 0;
FILE *fd = fopen(filename, "w");
FILE *fd = g_fopen(filename, "w");
if(!fd) return;
fprintf(fd, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
......@@ -594,7 +594,7 @@ static void export_raw(dt_lut_t *self, char *filename, char *name, char *descrip
GHashTableIter table_iter;
gpointer key, value;
FILE *fd = fopen(filename, "w");
FILE *fd = g_fopen(filename, "w");
if(!fd) return;
GList *patch_names = NULL;
......@@ -1609,7 +1609,7 @@ static int parse_csv(dt_lut_t *self, const char *filename, double **target_L_ptr
*name = NULL;
*description = NULL;
FILE *f = fopen(filename, "rb");
FILE *f = g_fopen(filename, "rb");
if(!f) return 0;
int N = 0;
int r = 0;
......
......@@ -25,7 +25,7 @@
float *read_pfm(const char *filename, int *wd, int *ht)
{
FILE *f = fopen(filename, "rb");
FILE *f = g_fopen(filename, "rb");
if(!f)
{
......@@ -127,7 +127,7 @@ float *read_pfm(const char *filename, int *wd, int *ht)
void write_pfm(const char *filename, int width, int height, float *data)
{
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
if(f)
{
// INFO: per-line fwrite call seems to perform best. LebedevRI, 18.04.2014
......
......@@ -164,7 +164,7 @@ gboolean dt_supported_image(const gchar *filename)
static void strip_semicolons_from_keymap(const char *path)
{
char pathtmp[PATH_MAX] = { 0 };
FILE *fin = fopen(path, "rb");
FILE *fin = g_fopen(path, "rb");
FILE *fout;
int i;
int c = '\0';
......@@ -172,7 +172,7 @@ static void strip_semicolons_from_keymap(const char *path)
if(!fin) return;
snprintf(pathtmp, sizeof(pathtmp), "%s_tmp", path);
fout = fopen(pathtmp, "wb");
fout = g_fopen(pathtmp, "wb");
if(!fout)
{
......
......@@ -44,6 +44,7 @@
#include <sys/resource.h>
#endif
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <inttypes.h>
#include <json-glib/json-glib.h>
......@@ -336,7 +337,7 @@ static inline void dt_print_mem_usage()
char pidstatus[128];
snprintf(pidstatus, sizeof(pidstatus), "/proc/%u/status", (uint32_t)getpid());
f = fopen(pidstatus, "r");
f = g_fopen(pidstatus, "r");
if(!f) return;
/* read memory size data from /proc/pid/status */
......@@ -413,7 +414,7 @@ static inline int dt_get_num_atom_cores()
#if defined(__linux__)
int count = 0;
char line[256];
FILE *f = fopen("/proc/cpuinfo", "r");
FILE *f = g_fopen("/proc/cpuinfo", "r");
if(f)
{
while(!feof(f))
......@@ -494,7 +495,7 @@ static inline int dt_get_num_atom_cores()
static inline size_t dt_get_total_memory()
{
#if defined(__linux__)
FILE *f = fopen("/proc/meminfo", "rb");
FILE *f = g_fopen("/proc/meminfo", "rb");
if(!f) return 0;
size_t mem = 0;
char *line = NULL;
......
......@@ -2674,7 +2674,7 @@ int dt_exif_xmp_write(const int imgid, const char *filename)
// we want to avoid writing the sidecar file if it didn't change to avoid issues when using the same images
// from different computers. sample use case: images on NAS, several computers using them NOT AT THE SAME TIME and
// the xmp crawler is used to find changed sidecars.
FILE *fd = fopen(filename, "rb");
FILE *fd = g_fopen(filename, "rb");
if(fd)
{
fseek(fd, 0, SEEK_END);
......
......@@ -414,7 +414,7 @@ gboolean dt_imageio_is_ldr(const char *filename)
{
size_t offset = 0;
uint8_t block[16] = { 0 };
FILE *fin = fopen(filename, "rb");
FILE *fin = g_fopen(filename, "rb");
if(fin)
{
/* read block from file */
......
......@@ -215,7 +215,7 @@ static inline void dt_imageio_write_dng(
const uint8_t xtrans[6][6],
const float whitelevel)
{
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
int k = 0;
if(f)
{
......
......@@ -110,7 +110,7 @@ dt_imageio_retval_t dt_imageio_open_j2k(dt_image_t *img, const char *filename, d
if(!img->exif_inited) (void)dt_exif_read(img, filename);
fsrc = fopen(filename, "rb");
fsrc = g_fopen(filename, "rb");
if(!fsrc)
{
fprintf(stderr, "[j2k_open] Error: failed to open `%s' for reading\n", filename);
......@@ -332,7 +332,7 @@ int dt_imageio_j2k_read_profile(const char *filename, uint8_t **out)
/* read the input file and put it in memory */
/* ---------------------------------------- */
fsrc = fopen(filename, "rb");
fsrc = g_fopen(filename, "rb");
if(!fsrc)
{
fprintf(stderr, "[j2k_read_profile] Error: failed to open `%s' for reading\n", filename);
......
......@@ -508,7 +508,7 @@ int dt_imageio_jpeg_write_with_icc_profile(const char *filename, const uint8_t *
return 1;
}
jpeg_create_compress(&(jpg.cinfo));
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
if(!f) return 1;
jpeg_stdio_dest(&(jpg.cinfo), f);
......@@ -564,7 +564,7 @@ int dt_imageio_jpeg_write(const char *filename, const uint8_t *in, const int wid
int dt_imageio_jpeg_read_header(const char *filename, dt_imageio_jpeg_t *jpg)
{
jpg->f = fopen(filename, "rb");
jpg->f = g_fopen(filename, "rb");
if(!jpg->f) return 1;
struct dt_imageio_jpeg_error_mgr jerr;
......
......@@ -36,7 +36,7 @@ dt_imageio_retval_t dt_imageio_open_pfm(dt_image_t *img, const char *filename, d
const char *ext = filename + strlen(filename);
while(*ext != '.' && ext > filename) ext--;
if(strcasecmp(ext, ".pfm")) return DT_IMAGEIO_FILE_CORRUPTED;
FILE *f = fopen(filename, "rb");
FILE *f = g_fopen(filename, "rb");
if(!f) return DT_IMAGEIO_FILE_CORRUPTED;
int ret = 0;
int cols = 3;
......
......@@ -44,7 +44,7 @@ typedef struct dt_imageio_png_t
int read_header(const char *filename, dt_imageio_png_t *png)
{
png->f = fopen(filename, "rb");
png->f = g_fopen(filename, "rb");
if(!png->f) return 1;
......
......@@ -486,7 +486,7 @@ dt_imageio_retval_t dt_imageio_open_rgbe(dt_image_t *img, const char *filename,
while(*ext != '.' && ext > filename) ext--;
if(strncmp(ext, ".hdr", 4) && strncmp(ext, ".HDR", 4) && strncmp(ext, ".Hdr", 4))
return DT_IMAGEIO_FILE_CORRUPTED;
FILE *f = fopen(filename, "rb");
FILE *f = g_fopen(filename, "rb");
if(!f) return DT_IMAGEIO_FILE_CORRUPTED;
if(RGBE_ReadHeader(f, &img->width, &img->height, NULL)) goto error_corrupt;
......
......@@ -364,7 +364,7 @@ void dt_mipmap_cache_allocate_dynamic(void *data, dt_cache_entry_t *entry)
// try and load from disk, if successful set flag
char filename[PATH_MAX] = {0};
snprintf(filename, sizeof(filename), "%s.d/%d/%d.jpg", cache->cachedir, mip, get_imgid(entry->key));
FILE *f = fopen(filename, "rb");
FILE *f = g_fopen(filename, "rb");
if(f)
{
long len = 0;
......@@ -453,7 +453,7 @@ void dt_mipmap_cache_deallocate_dynamic(void *data, dt_cache_entry_t *entry)
snprintf(filename, sizeof(filename), "%s.d/%d/%d.jpg", cache->cachedir, mip, get_imgid(entry->key));
// Don't write existing files as both performance and quality (lossy jpg) suffer
FILE *f = NULL;
if (!g_file_test(filename, G_FILE_TEST_EXISTS) && (f = fopen(filename, "wb")))
if (!g_file_test(filename, G_FILE_TEST_EXISTS) && (f = g_fopen(filename, "wb")))
{
// first check the disk isn't full
struct statvfs vfsbuf;
......
......@@ -389,7 +389,7 @@ static int dt_opencl_device_init(dt_opencl_t *cl, const int dev, cl_device_id *d
// now load all darktable cl kernels.
// TODO: compile as a job?
tstart = dt_get_wtime();
FILE *f = fopen(filename, "rb");
FILE *f = g_fopen(filename, "rb");
if(f)
{
......@@ -1416,7 +1416,7 @@ void dt_opencl_unlock_device(const int dev)
static FILE *fopen_stat(const char *filename, struct stat *st)
{
FILE *f = fopen(filename, "rb");
FILE *f = g_fopen(filename, "rb");
if(!f)
{
dt_print(DT_DEBUG_OPENCL, "[opencl_fopen_stat] could not open file `%s'!\n", filename);
......@@ -1743,7 +1743,7 @@ int dt_opencl_build_program(const int dev, const int prog, const char *binname,
// save opencl compiled binary as md5sum-named file
char link_dest[PATH_MAX] = { 0 };
snprintf(link_dest, sizeof(link_dest), "%s/%s", cachedir, md5sum);
FILE *f = fopen(link_dest, "w+");
FILE *f = g_fopen(link_dest, "w+");
if(!f) goto ret;
size_t bytes_written = fwrite(binaries[i], sizeof(char), binary_sizes[i], f);
if(bytes_written != binary_sizes[i]) goto ret;
......
......@@ -40,6 +40,8 @@
#include <time.h>
#include <zlib.h>
#include <glib/gstdio.h>
#ifdef STANDALONE
#define PACKAGE_STRING "darktable pdf library"
#else
......@@ -208,7 +210,7 @@ dt_pdf_t *dt_pdf_start(const char *filename, float width, float height, float dp
dt_pdf_t *pdf = calloc(1, sizeof(dt_pdf_t));
if(!pdf) return NULL;
pdf->fd = fopen(filename, "wb");
pdf->fd = g_fopen(filename, "wb");
if(!pdf->fd)
{
free(pdf);
......@@ -306,7 +308,7 @@ static size_t _pdf_write_stream(dt_pdf_t *pdf, dt_pdf_stream_encoder_t encoder,
int dt_pdf_add_icc(dt_pdf_t *pdf, const char *filename)
{
FILE *in = fopen(filename, "rb");
FILE *in = g_fopen(filename, "rb");
if(!in) return 0;
fseek(in, 0, SEEK_END);
......@@ -780,7 +782,7 @@ time_error:
// just for debugging to read a ppm file
float * read_ppm(const char * filename, int * wd, int * ht)
{
FILE *f = fopen(filename, "rb");
FILE *f = g_fopen(filename, "rb");
if(!f)
{
......
......@@ -19,6 +19,7 @@
#pragma GCC poison strcat // use g_strncat
#pragma GCC poison strncat // use g_strncat
#pragma GCC poison pthread_create // use dt_pthread_create, musl issues
#pragma GCC poison fopen // use g_fopen
#endif
......
......@@ -1055,7 +1055,7 @@ void dt_styles_import_from_file(const char *style_path)
style = dt_styles_style_data_new();
parser = g_markup_parse_context_new(&dt_style_parser, 0, style, NULL);
if((style_file = fopen(style_path, "r")))
if((style_file = g_fopen(style_path, "r")))
{
while(!feof(style_file))
......
......@@ -554,7 +554,7 @@ uint32_t dt_tag_get_recent_used(GList **result)
*/
ssize_t dt_tag_import(const char *filename)
{
FILE *fd = fopen(filename, "r");
FILE *fd = g_fopen(filename, "r");
if(!fd) return -1;
......@@ -647,7 +647,7 @@ ssize_t dt_tag_import(const char *filename)
*/
ssize_t dt_tag_export(const char *filename)
{
FILE *fd = fopen(filename, "w");
FILE *fd = g_fopen(filename, "w");
if(!fd) return -1;
......
......@@ -27,6 +27,7 @@
#include "common/file_location.h"
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>
#include <inttypes.h>
#include <stdio.h>
......@@ -215,7 +216,7 @@ static inline void dt_conf_init(dt_conf_t *cf, const char *filename, GSList *ove
if(!i)
{
snprintf(darktable.conf->filename, sizeof(darktable.conf->filename), "%s", filename);
f = fopen(filename, "rb");
f = g_fopen(filename, "rb");
if(!f)
{
// remember we init to default rc and try again
......@@ -228,7 +229,7 @@ static inline void dt_conf_init(dt_conf_t *cf, const char *filename, GSList *ove
char buf[PATH_MAX] = { 0 }, defaultrc[PATH_MAX] = { 0 };
dt_loc_get_datadir(buf, sizeof(buf));
snprintf(defaultrc, sizeof(defaultrc), "%s/darktablerc", buf);
f = fopen(defaultrc, "rb");
f = g_fopen(defaultrc, "rb");
}
if(!f) return;
while(!feof(f))
......@@ -274,7 +275,7 @@ static void dt_conf_print(const gchar *key, const gchar *val, FILE *f)
static inline void dt_conf_cleanup(dt_conf_t *cf)
{
FILE *f = fopen(cf->filename, "wb");
FILE *f = g_fopen(cf->filename, "wb");
if(f)
{
GList *keys = g_hash_table_get_keys(cf->table);
......
......@@ -58,8 +58,8 @@ int write_image(dt_imageio_module_data_t *ppm, const char *filename, const void
if(!strcmp(sourcefile, targetfile)) goto END;
fin = fopen(sourcefile, "rb");
fout = fopen(targetfile, "wb");
fin = g_fopen(sourcefile, "rb");
fout = g_fopen(targetfile, "wb");
if(fin == NULL || fout == NULL) goto END;
fseek(fin, 0, SEEK_END);
......
......@@ -328,7 +328,7 @@ int write_image(dt_imageio_module_data_t *jpg_tmp, const char *filename, const v
return 1;
}
jpeg_create_compress(&(jpg->cinfo));
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
if(!f) return 1;
jpeg_stdio_dest(&(jpg->cinfo), f);
......@@ -404,7 +404,7 @@ int write_image(dt_imageio_module_data_t *jpg_tmp, const char *filename, const v
static int __attribute__((__unused__)) read_header(const char *filename, dt_imageio_jpeg_t *jpg)
{
jpg->f = fopen(filename, "rb");
jpg->f = g_fopen(filename, "rb");
if(!jpg->f) return 1;
struct dt_imageio_jpeg_error_mgr jerr;
......
......@@ -33,7 +33,7 @@ int write_image(dt_imageio_module_data_t *data, const char *filename, const void
{
const dt_imageio_module_data_t *const pfm = data;
int status = 0;
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
if(f)
{
// align pfm header to sse, assuming the file will
......
......@@ -125,7 +125,7 @@ int write_image(dt_imageio_module_data_t *p_tmp, const char *filename, const voi
{
dt_imageio_png_t *p = (dt_imageio_png_t *)p_tmp;
const int width = p->width, height = p->height;
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
if(!f) return 1;
png_structp png_ptr;
......@@ -230,7 +230,7 @@ int write_image(dt_imageio_module_data_t *p_tmp, const char *filename, const voi
static int __attribute__((__unused__)) read_header(const char *filename, dt_imageio_module_data_t *p_tmp)
{
dt_imageio_png_t *png = (dt_imageio_png_t *)p_tmp;
png->f = fopen(filename, "rb");
png->f = g_fopen(filename, "rb");
if(!png->f) return 1;
......
......@@ -40,7 +40,7 @@ int write_image(dt_imageio_module_data_t *ppm, const char *filename, const void
int status = 0;
uint16_t *row = (uint16_t *)in;
uint16_t swapped[3];
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
if(f)
{
(void)fprintf(f, "P6\n%d %d\n65535\n", ppm->width, ppm->height);
......
......@@ -112,7 +112,7 @@ int write_image(dt_imageio_module_data_t *webp, const char *filename, const void
int exif_len, int imgid, int num, int total)
{
dt_imageio_webp_t *webp_data = (dt_imageio_webp_t *)webp;
FILE *out = fopen(filename, "wb");
FILE *out = g_fopen(filename, "wb");
// Create, configure and validate a WebPConfig instance
WebPConfig config;
......
......@@ -380,8 +380,8 @@ static void copy_res(const char *src, const char *dst)
dt_loc_get_datadir(share, sizeof(share));
gchar *sourcefile = g_build_filename(share, src, NULL);
char *content = NULL;
FILE *fin = fopen(sourcefile, "rb");
FILE *fout = fopen(dst, "wb");
FILE *fin = g_fopen(sourcefile, "rb");
FILE *fout = g_fopen(dst, "wb");
if(fin && fout)
{
......@@ -443,7 +443,7 @@ void finalize_store(dt_imageio_module_storage_t *self, dt_imageio_module_data_t
const char *title = d->title;
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
if(!f) return;
fprintf(f,
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "
......
......@@ -357,8 +357,8 @@ static void copy_res(const char *src, const char *dst)
dt_loc_get_datadir(share, sizeof(share));
gchar *sourcefile = g_build_filename(share, src, NULL);
char *content = NULL;
FILE *fin = fopen(sourcefile, "rb");
FILE *fout = fopen(dst, "wb");
FILE *fin = g_fopen(sourcefile, "rb");
FILE *fout = g_fopen(dst, "wb");
if(fin && fout)
{
......@@ -393,7 +393,7 @@ void finalize_store(dt_imageio_module_storage_t *self, dt_imageio_module_data_t
const char *title = d->title;
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
if(!f) return;
fprintf(f, "\\newcommand{\\dttitle}{%s}\n"
"\\newcommand{\\dtauthor}{the author}\n"
......
......@@ -1056,7 +1056,7 @@ static void process_clusters(gpointer instance, gpointer user_data)
memcpy(g->flowback.weight, p->source_weight, sizeof(float) * MAXN);
g->flowback.n = p->n;
g->flowback_set = 1;
FILE *f = fopen("/tmp/dt_colormapping_loaded", "wb");
FILE *f = g_fopen("/tmp/dt_colormapping_loaded", "wb");
if(f)
{
if(fwrite(&g->flowback, sizeof(g->flowback), 1, f) < 1)
......@@ -1153,7 +1153,7 @@ void gui_init(struct dt_iop_module_t *self)
G_CALLBACK(process_clusters), self);
FILE *f = fopen("/tmp/dt_colormapping_loaded", "rb");
FILE *f = g_fopen("/tmp/dt_colormapping_loaded", "rb");
if(f)
{
if(fread(&g->flowback, sizeof(g->flowback), 1, f) > 0) g->flowback_set = 1;
......
......@@ -504,7 +504,7 @@ void commit_params (struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pi
dt_iop_colortransfer_gui_data_t *g = (dt_iop_colortransfer_gui_data_t *)self->gui_data;
memcpy (&g->flowback, d, self->params_size);
g->flowback_set = 1;
FILE *f = fopen("/tmp/dt_colortransfer_loaded", "wb");
FILE *f = g_fopen("/tmp/dt_colortransfer_loaded", "wb");
if(f)
{
if(fwrite(&g->flowback, self->params_size, 1, f) > 0) g->flowback.flag = APPLY;
......@@ -683,7 +683,7 @@ void gui_init(struct dt_iop_module_t *self)
gtk_widget_set_tooltip_text(g->apply_button, _("apply previously analyzed image look to this image"));
gtk_box_pack_start(box, g->apply_button, TRUE, TRUE, 0);
g_signal_connect(G_OBJECT(g->apply_button), "clicked", G_CALLBACK(apply_button_pressed), (gpointer)self);
FILE *f = fopen("/tmp/dt_colortransfer_loaded", "rb");
FILE *f = g_fopen("/tmp/dt_colortransfer_loaded", "rb");
if(f)
{
if(fread(&g->flowback, self->params_size, 1, f) > 0) g->flowback_set = 1;
......
......@@ -790,7 +790,7 @@ static void process_wavelets(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_
if(piece->pipe->type != DT_DEV_PIXELPIPE_PREVIEW)
{
const int n = width*height;
FILE *f = fopen("/tmp/transformed.pfm", "wb");
FILE *f = g_fopen("/tmp/transformed.pfm", "wb");
fprintf(f, "PF\n%d %d\n-1.0\n", width, height);
for(int k=0; k<n; k++)
fwrite(((float*)ovoid)+4*k, sizeof(float), 3, f);
......@@ -814,13 +814,13 @@ static void process_wavelets(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_
{
char filename[512];
snprintf(filename, sizeof(filename), "/tmp/coarse_%d.pfm", scale);
FILE *f = fopen(filename, "wb");
FILE *f = g_fopen(filename, "wb");
fprintf(f, "PF\n%d %d\n-1.0\n", width, height);
for(size_t k = 0; k < npixels; k++)
fwrite(buf2+4*k, sizeof(float), 3, f);
fclose(f);
snprintf(filename, sizeof(filename), "/tmp/detail_%d.pfm", scale);
f = fopen(filename, "wb");
f = g_fopen(filename, "wb");
fprintf(f, "PF\n%d %d\n-1.0\n", width, height);
for(size_t k = 0; k < npixels; k++)
fwrite(buf[scale]+4*k, sizeof(float), 3, f);
......
......@@ -554,7 +554,7 @@ static GList *_lib_geotagging_get_timezones(void)
}
// parse zone.tab and put all time zone descriptions into tz
fp = fopen(zone_tab, "r");
fp = g_fopen(zone_tab, "r");
g_free(zone_tab);
if(!fp) return NULL;
......
......@@ -1378,7 +1378,7 @@ int dt_view_image_expose(dt_view_image_over_t *image_over, uint32_t imgid, cairo
char *path = dt_image_get_text_path(img->id);
if(path)
{
FILE *f = fopen(path, "rb");
FILE *f = g_fopen(path, "rb");
if(f)
{
char line[2048];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment