Report errno string when writing files fails
authorCampbell Barton <ideasman42@gmail.com>
Mon, 11 Jan 2016 01:32:29 +0000 (12:32 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 11 Jan 2016 01:32:29 +0000 (12:32 +1100)
Screenshot ignored errors, some render code printed 'Saved' without checking for failure.

note: errno is now cleared from IMB_saveiff so all callers don't need to.

source/blender/blenkernel/intern/image.c
source/blender/editors/screen/screendump.c
source/blender/editors/space_image/image_ops.c
source/blender/imbuf/intern/writeimage.c
source/blender/makesrna/intern/rna_image_api.c
source/blender/render/intern/source/pipeline.c
source/blender/render/intern/source/render_result.c

index f640f113c0411a98fb36bb9441a7235dd83da5fa..7eea53497b2a8c917a1812d442172fc580f69304 100644 (file)
@@ -827,7 +827,9 @@ static ImBuf *add_ibuf_size(unsigned int width, unsigned int height, const char
 }
 
 /* adds new image block, creates ImBuf and initializes color */
-Image *BKE_image_add_generated(Main *bmain, unsigned int width, unsigned int height, const char *name, int depth, int floatbuf, short gen_type, const float color[4], const bool stereo3d)
+Image *BKE_image_add_generated(
+        Main *bmain, unsigned int width, unsigned int height, const char *name,
+        int depth, int floatbuf, short gen_type, const float color[4], const bool stereo3d)
 {
        /* on save, type is changed to FILE in editsima.c */
        Image *ima = image_alloc(bmain, name, IMA_SRC_GENERATED, IMA_TYPE_UV_TEST);
index a701fc9ccb79b2a8816d915b433e199485c76445..a354d1145fe5a9730f20b0c7b86d5c4995cc29fc 100644 (file)
@@ -30,6 +30,7 @@
 
 
 #include <string.h>
+#include <errno.h>
 
 #include "MEM_guardedalloc.h"
 
@@ -177,6 +178,7 @@ static void screenshot_crop(ImBuf *ibuf, rcti crop)
 static int screenshot_exec(bContext *C, wmOperator *op)
 {
        ScreenshotData *scd = op->customdata;
+       bool ok = false;
 
        if (scd == NULL) {
                /* when running exec directly */
@@ -204,14 +206,20 @@ static int screenshot_exec(bContext *C, wmOperator *op)
                                /* bw screenshot? - users will notice if it fails! */
                                IMB_color_to_bw(ibuf);
                        }
-                       BKE_imbuf_write(ibuf, path, &scd->im_format);
+                       if (BKE_imbuf_write(ibuf, path, &scd->im_format)) {
+                               ok = true;
+                       }
+                       else {
+                               BKE_reportf(op->reports, RPT_ERROR, "Could not write image: %s", strerror(errno));
+                       }
 
                        IMB_freeImBuf(ibuf);
                }
        }
 
        screenshot_data_free(op);
-       return OPERATOR_FINISHED;
+
+       return ok ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
 }
 
 static int screenshot_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
index 1d11e7c6287e9c0883ed874d97a6337f9a686d17..c35f5ae14c5714e3071be4fb935fb202d11b0bf1 100644 (file)
@@ -1580,7 +1580,9 @@ static void save_image_options_to_op(SaveImageOptions *simopts, wmOperator *op)
        RNA_string_set(op->ptr, "filepath", simopts->filepath);
 }
 
-static void save_image_post(wmOperator *op, ImBuf *ibuf, Image *ima, int ok, int save_copy, const char *relbase, int relative, int do_newpath, const char *filepath)
+static void save_image_post(
+        wmOperator *op, ImBuf *ibuf, Image *ima, int ok, int save_copy,
+        const char *relbase, int relative, int do_newpath, const char *filepath)
 {
        if (ok) {
                if (!save_copy) {
@@ -1630,7 +1632,7 @@ static void save_image_post(wmOperator *op, ImBuf *ibuf, Image *ima, int ok, int
                }
        }
        else {
-               BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s", filepath);
+               BKE_reportf(op->reports, RPT_ERROR, "Could not write image: %s", strerror(errno));
        }
 }
 
@@ -2151,7 +2153,7 @@ static int image_save_sequence_exec(bContext *C, wmOperator *op)
                        BLI_path_abs(name, bmain->name);
 
                        if (0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
-                               BKE_reportf(op->reports, RPT_ERROR, "Could not write image %s", name);
+                               BKE_reportf(op->reports, RPT_ERROR, "Could not write image: %s", strerror(errno));
                                break;
                        }
 
index 28710fba823e52a2336884b5299fc8e69d77e9c3..84ec2534e7f68bebb039580894d2dbfb8a242321 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #include "BLI_utildefines.h"
 #include "BLI_path_util.h"
@@ -54,6 +55,8 @@ short IMB_saveiff(struct ImBuf *ibuf, const char *name, int flags)
 {
        const ImFileType *type;
 
+       errno = 0;
+
        BLI_assert(!BLI_path_is_rel(name));
 
        if (ibuf == NULL) return (false);
index 7280cc622dcb449b8e8dbebe8fded42a94499782..23643f722d6722f92de519491f1fafe62b46c8f1 100644 (file)
@@ -48,6 +48,7 @@
 
 #ifdef RNA_RUNTIME
 
+#include <errno.h>
 #include "BKE_image.h"
 #include "BKE_packedFile.h"
 #include "BKE_main.h"
@@ -101,7 +102,7 @@ static void rna_Image_save_render(Image *image, bContext *C, ReportList *reports
                        write_ibuf->dither = scene->r.dither_intensity;
 
                        if (!BKE_imbuf_write(write_ibuf, path, &scene->r.im_format)) {
-                               BKE_reportf(reports, RPT_ERROR, "Could not write image '%s'", path);
+                               BKE_reportf(reports, RPT_ERROR, "Could not write image: %s, '%s'", strerror(errno), path);
                        }
 
                        if (write_ibuf != ibuf)
index 1ac9dd8e716b77dd9e871abedbef39e0772eefb2..75ef2fbb048ee1f239737778487b187abf4be867 100644 (file)
@@ -34,6 +34,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stddef.h>
+#include <errno.h>
 
 #include "DNA_anim_types.h"
 #include "DNA_image_types.h"
@@ -198,6 +199,35 @@ static void stats_background(void *UNUSED(arg), RenderStats *rs)
        fflush(stdout);
 }
 
+static void render_print_save_message(const char *name, int ok, int err)
+{
+       if (ok) {
+               printf("Saved: '%s'\n", name);
+       }
+       else {
+               printf("Render error (%s) cannot save: '%s'\n", strerror(err), name);
+       }
+}
+
+static int render_imbuf_write_stamp_test(
+        Scene *scene, struct RenderResult *rr, ImBuf *ibuf, const char *name,
+        const ImageFormatData *imf, bool stamp)
+{
+       int ok;
+
+       if (stamp) {
+               /* writes the name of the individual cameras */
+               ok = BKE_imbuf_write_stamp(scene, rr, ibuf, name, imf);
+       }
+       else {
+               ok = BKE_imbuf_write(ibuf, name, imf);
+       }
+
+       render_print_save_message(name, ok, errno);
+
+       return ok;
+}
+
 void RE_FreeRenderResult(RenderResult *res)
 {
        render_result_free(res);
@@ -3209,8 +3239,8 @@ bool RE_WriteRenderViewsImage(ReportList *reports, RenderResult *rr, Scene *scen
        if (ELEM(rd->im_format.imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER) &&
            rd->im_format.views_format == R_IMF_VIEWS_MULTIVIEW)
        {
-               RE_WriteRenderResult(reports, rr, name, &rd->im_format, true, NULL);
-               printf("Saved: %s\n", name);
+               ok = RE_WriteRenderResult(reports, rr, name, &rd->im_format, true, NULL);
+               render_print_save_message(name, ok, errno);
        }
 
        /* mono, legacy code */
@@ -3228,8 +3258,8 @@ bool RE_WriteRenderViewsImage(ReportList *reports, RenderResult *rr, Scene *scen
                        }
 
                        if (rd->im_format.imtype == R_IMF_IMTYPE_MULTILAYER) {
-                               RE_WriteRenderResult(reports, rr, name, &rd->im_format, false, rv->name);
-                               printf("Saved: %s\n", name);
+                               ok = RE_WriteRenderResult(reports, rr, name, &rd->im_format, false, rv->name);
+                               render_print_save_message(name, ok, errno);
                        }
                        else {
                                ImBuf *ibuf = render_result_rect_to_ibuf(rr, rd, view_id);
@@ -3237,18 +3267,7 @@ bool RE_WriteRenderViewsImage(ReportList *reports, RenderResult *rr, Scene *scen
                                IMB_colormanagement_imbuf_for_write(ibuf, true, false, &scene->view_settings,
                                                                    &scene->display_settings, &rd->im_format);
 
-                               if (stamp) {
-                                       /* writes the name of the individual cameras */
-                                       ok = BKE_imbuf_write_stamp(scene, rr, ibuf, name, &rd->im_format);
-                               }
-                               else {
-                                       ok = BKE_imbuf_write(ibuf, name, &rd->im_format);
-                               }
-
-                               if (ok == false) {
-                                       printf("Render error: cannot save %s\n", name);
-                               }
-                               else printf("Saved: %s\n", name);
+                               ok = render_imbuf_write_stamp_test(scene, rr, ibuf, name, &rd->im_format, stamp);
 
                                /* optional preview images for exr */
                                if (ok && rd->im_format.imtype == R_IMF_IMTYPE_OPENEXR && (rd->im_format.flag & R_IMF_FLAG_PREVIEW_JPG)) {
@@ -3263,14 +3282,7 @@ bool RE_WriteRenderViewsImage(ReportList *reports, RenderResult *rr, Scene *scen
                                        IMB_colormanagement_imbuf_for_write(ibuf, true, false, &scene->view_settings,
                                                                            &scene->display_settings, &imf);
 
-                                       if (stamp) {
-                                               /* writes the name of the individual cameras */
-                                               ok = BKE_imbuf_write_stamp(scene, rr, ibuf, name, &imf);
-                                       }
-                                       else {
-                                               ok = BKE_imbuf_write(ibuf, name, &imf);
-                                       }
-                                       printf("Saved: %s\n", name);
+                                       ok = render_imbuf_write_stamp_test(scene, rr, ibuf, name, &imf, stamp);
                                }
 
                                /* imbuf knows which rects are not part of ibuf */
@@ -3299,15 +3311,7 @@ bool RE_WriteRenderViewsImage(ReportList *reports, RenderResult *rr, Scene *scen
 
                        ibuf_arr[2] = IMB_stereo3d_ImBuf(&scene->r.im_format, ibuf_arr[0], ibuf_arr[1]);
 
-                       if (stamp)
-                               ok = BKE_imbuf_write_stamp(scene, rr, ibuf_arr[2], name, &rd->im_format);
-                       else
-                               ok = BKE_imbuf_write(ibuf_arr[2], name, &rd->im_format);
-
-                       if (ok == false)
-                               printf("Render error: cannot save %s\n", name);
-                       else
-                               printf("Saved: %s\n", name);
+                       ok = render_imbuf_write_stamp_test(scene, rr, ibuf_arr[2], name, &rd->im_format, stamp);
 
                        /* optional preview images for exr */
                        if (ok && rd->im_format.imtype == R_IMF_IMTYPE_OPENEXR &&
@@ -3325,12 +3329,7 @@ bool RE_WriteRenderViewsImage(ReportList *reports, RenderResult *rr, Scene *scen
                                IMB_colormanagement_imbuf_for_write(ibuf_arr[2], true, false, &scene->view_settings,
                                                                    &scene->display_settings, &imf);
 
-                               if (stamp)
-                                       ok = BKE_imbuf_write_stamp(scene, rr, ibuf_arr[2], name, &rd->im_format);
-                               else
-                                       ok = BKE_imbuf_write(ibuf_arr[2], name, &imf);
-
-                               printf("Saved: %s\n", name);
+                               ok = render_imbuf_write_stamp_test(scene, rr, ibuf_arr[2], name, &rd->im_format, stamp);
                        }
 
                        /* imbuf knows which rects are not part of ibuf */
index 19b049ee1dabe4fcd0636a2e2dbde0a414dddb7b..c89adc4a8141fe87d42624b18e3c13327928bdc7 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #include "MEM_guardedalloc.h"
 
@@ -1134,6 +1135,8 @@ bool RE_WriteRenderResult(ReportList *reports, RenderResult *rr, const char *fil
                }
        }
 
+       errno = 0;
+
        BLI_make_existing_file(filename);
 
        if (IMB_exr_begin_write(exrhandle, filename, width, height, compress, rr->stamp_data)) {
@@ -1142,7 +1145,7 @@ bool RE_WriteRenderResult(ReportList *reports, RenderResult *rr, const char *fil
        }
        else {
                /* TODO, get the error from openexr's exception */
-               BKE_report(reports, RPT_ERROR, "Error writing render result (see console)");
+               BKE_reportf(reports, RPT_ERROR, "Error writing render result, %s (see console)", strerror(errno));
                success = false;
        }