changeset 31162:28817158ca86

Tiff: changed write functions to use fcntl wrappers.
author magedrifaat <magedrifaat@gmail.com>
date Wed, 10 Aug 2022 20:46:56 +0200
parents b731c8f6db95
children d701c6a4cda1
files libinterp/corefcn/__tiff__.cc
diffstat 1 files changed, 17 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__tiff__.cc	Wed Aug 10 16:01:51 2022 +0200
+++ b/libinterp/corefcn/__tiff__.cc	Wed Aug 10 20:46:56 2022 +0200
@@ -5,12 +5,6 @@
 #include <string>
 #include <iostream>
 
-// Workaround to not have to include fcntl which doesn't exist on windows
-// TODO(maged): see what octave does for this (fcntl)
-#ifndef O_RDONLY
-#define O_RDONLY 0
-#endif
-
 #include "defun.h"
 #include "ov.h"
 #include "ovl.h"
@@ -18,6 +12,8 @@
 
 #include "errwarn.h"
 
+#include "fcntl-wrappers.h"
+
 #if defined (HAVE_TIFF)
 #  include <tiffio.h>
 TIFFErrorHandler tiff_default_handler = NULL;
@@ -65,6 +61,16 @@
     }
   };
 
+  void
+  check_readonly (TIFF *tif)
+  {
+    // This can't use O_RDONLY directly because its header file "fcntl.h"
+    // isn't available on Windows. The wrapper, however, seems to return
+    // the right thing even on Windows.
+    if (TIFFGetMode (tif) == octave_o_rdonly_wrapper ())
+      error ("Can't write data to a file opened in read-only mode");
+  }
+
   // Error if status is not 1 (success status for TIFFGetField)
   void
   validate_tiff_get_field (bool status, void *p_to_free=NULL)
@@ -1837,6 +1843,8 @@
       error ("Too few arguments provided\n");
     
     TIFF *tif = (TIFF *)(args(0).uint64_value ());
+
+    check_readonly (tif);
     
     if (args(1).isstruct ())
       {
@@ -2036,9 +2044,7 @@
     
     TIFF *tif = (TIFF *)(args(0).uint64_value ());
 
-    // TODO(maged): check on windows
-    if (TIFFGetMode (tif) == O_RDONLY)
-      error ("Can't write data to a file opened in read-only mode");
+    check_readonly (tif);
 
     // Obtain all necessary tags
     tiff_image_data image_data (tif);
@@ -2130,9 +2136,7 @@
     
     TIFF *tif = (TIFF *)(args(0).uint64_value ());
 
-    // TODO(maged): check on windows
-    if (TIFFGetMode (tif) == O_RDONLY)
-      error ("Can't write data to a file opened in read-only mode");
+    check_readonly (tif);
 
     // Obtain all necessary tags
     tiff_image_data image_data (tif);
@@ -2164,9 +2168,7 @@
     
     TIFF *tif = (TIFF *)(args(0).uint64_value ());
 
-    // TODO(maged): check on windows
-    if (TIFFGetMode (tif) == O_RDONLY)
-      error ("Can't write data to a file opened in read-only mode");
+    check_readonly (tif);
 
     // Obtain all necessary tags
     tiff_image_data image_data (tif);