gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 1ef6e92a: All programs: error when input and o


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 1ef6e92a: All programs: error when input and output names are the same
Date: Sat, 17 Dec 2022 13:00:54 -0500 (EST)

branch: master
commit 1ef6e92a5ede396490cec1f8729892f4cc456b86
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    All programs: error when input and output names are the same
    
    Until now, when the given (or automatically selected) output file name of a
    program was the same as the input, the program would delete the output file
    (and thus the input) before reading it! As a result, it would delete the
    input file and also complain that it doesn't exist. This would be a very
    confusing error (and possibly even resulting in loss of data!).
    
    With this commit, the function that checks the writability of the output
    (and deletes it, if it exists), also takes the name of the program's input
    file and will abort if the input or output names are the same.
    
    This bug was reported by Faezeh Bidjarchian.
    
    This bug was reported by bug #63543.
---
 NEWS                             |  2 ++
 bin/arithmetic/arithmetic.c      |  2 +-
 bin/arithmetic/ui.c              | 11 ++++++-----
 bin/convertt/convertt.c          |  3 ++-
 bin/convertt/ui.c                |  5 +++--
 bin/convolve/ui.c                |  7 ++++---
 bin/crop/crop.c                  |  2 +-
 bin/crop/onecrop.c               |  7 +++----
 bin/crop/ui.c                    |  3 ++-
 bin/fits/keywords.c              |  5 +++--
 bin/fits/meta.c                  |  2 +-
 bin/fits/ui.c                    |  3 ++-
 bin/match/ui.c                   | 14 +++++++++-----
 bin/mkcatalog/ui.c               | 16 ++++++++++++++--
 bin/mknoise/ui.c                 |  3 ++-
 bin/mkprof/mkprof.c              |  4 ++--
 bin/mkprof/ui.c                  |  4 ++--
 bin/noisechisel/ui.c             |  3 ++-
 bin/query/ui.c                   |  4 ++--
 bin/script/psf-select-stars.in   |  1 +
 bin/segment/ui.c                 |  3 ++-
 bin/statistics/statistics.c      |  4 ++--
 bin/statistics/ui.c              |  6 +++---
 bin/table/ui.c                   |  3 ++-
 bin/warp/ui.c                    |  3 ++-
 doc/announce-acknowledge.txt     |  1 +
 lib/arithmetic.c                 |  4 ++--
 lib/checkset.c                   | 20 ++++++++++++++++----
 lib/gnuastro-internal/checkset.h |  6 ++++--
 lib/label.c                      |  2 +-
 lib/options.c                    |  2 +-
 31 files changed, 100 insertions(+), 55 deletions(-)

diff --git a/NEWS b/NEWS
index 12b26728..c955117b 100644
--- a/NEWS
+++ b/NEWS
@@ -178,6 +178,8 @@ See the end of the file for license conditions.
               has no rows. Reported by Elham Saremi.
   bug #63485: Oversampling in radial profile has incorrect center. Reported
               by Nafiseh Sedighi.
+  bug #63543: Programs delete input when it has same name as output.
+              Reported by Faezeh Bidjarchian.
 
 
 
diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index f8074caa..b10362a2 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -1094,7 +1094,7 @@ arithmetic_tofile(struct arithmeticparams *p, char 
*token, int freeflag)
                            : OPERATOR_PREFIX_LENGTH_TOFILE     ];
 
   /* Make sure that if the file exists, it is deleted. */
-  gal_checkset_writable_remove(filename, p->cp.keep,
+  gal_checkset_writable_remove(filename, NULL, p->cp.keep,
                                p->cp.dontdelete);
 
   /* Save it to a file. */
diff --git a/bin/arithmetic/ui.c b/bin/arithmetic/ui.c
index a58f2c28..1a42fbf4 100644
--- a/bin/arithmetic/ui.c
+++ b/bin/arithmetic/ui.c
@@ -298,7 +298,8 @@ ui_check_options_and_arguments(struct arithmeticparams *p)
                   OPERATOR_PREFIX_LENGTH_TOFILEFREE)==0 )
         {
           filename=&token->v[ OPERATOR_PREFIX_LENGTH_TOFILE ];
-          gal_checkset_writable_remove(filename, cp->keep, cp->dontdelete);
+          gal_checkset_writable_remove(filename, basename, cp->keep,
+                                       cp->dontdelete);
         }
 
       /* This may be a simple filename. */
@@ -322,8 +323,7 @@ ui_check_options_and_arguments(struct arithmeticparams *p)
 
               /* If no output name is given, we need to extract the output
                  name from the inputs. */
-              if(cp->output==NULL && basename==NULL)
-                basename=token->v;
+              if(basename==NULL) basename=token->v;
             }
 
           /* This token is a number. Check if a negative dash was present
@@ -338,7 +338,8 @@ ui_check_options_and_arguments(struct arithmeticparams *p)
      'makenew' when the user doesn't set an output name explicity), use a
      default name. */
   if(cp->output)
-    gal_checkset_writable_remove(cp->output, cp->keep, cp->dontdelete);
+    gal_checkset_writable_remove(cp->output, basename, cp->keep,
+                                 cp->dontdelete);
   else
     {
       if(basename)
@@ -347,7 +348,7 @@ ui_check_options_and_arguments(struct arithmeticparams *p)
       else
         {
           gal_checkset_allocate_copy("arithmetic.fits", &cp->output);
-          gal_checkset_writable_remove(cp->output, cp->keep,
+          gal_checkset_writable_remove(cp->output, basename, cp->keep,
                                        cp->dontdelete);
         }
     }
diff --git a/bin/convertt/convertt.c b/bin/convertt/convertt.c
index a7985f68..e2e23d14 100644
--- a/bin/convertt/convertt.c
+++ b/bin/convertt/convertt.c
@@ -336,7 +336,8 @@ convertt(struct converttparams *p)
 
     /* Plain text: only one channel is acceptable. */
     case OUT_FORMAT_TXT:
-      gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+      gal_checkset_writable_remove(p->cp.output, p->inputnames->v, 0,
+                                   p->cp.dontdelete);
       gal_txt_write(p->chll, NULL, NULL, p->cp.output, 0);
       break;
 
diff --git a/bin/convertt/ui.c b/bin/convertt/ui.c
index 632308bc..8e8c8e8d 100644
--- a/bin/convertt/ui.c
+++ b/bin/convertt/ui.c
@@ -418,7 +418,7 @@ ui_show_fonts(struct converttparams *p)
 
   /* Set the PDF and PS file names. */
   outpdf = gal_checkset_automatic_output(&p->cp, p->cp.output, "-fonts.pdf");
-  gal_checkset_writable_remove(outpdf, 0, p->cp.dontdelete);
+  gal_checkset_writable_remove(outpdf, p->cp.output, 0, p->cp.dontdelete);
   outps=gal_checkset_automatic_output(&p->cp, outpdf, ".ps");
 
   /* This command was taken from the 'ps2pdfwr' installed script that comes
@@ -1231,7 +1231,8 @@ ui_set_output(struct converttparams *p)
     }
 
   /* Check if the output already exists and remove it if allowed. */
-  gal_checkset_writable_remove(cp->output, 0, cp->dontdelete);
+  gal_checkset_writable_remove(cp->output, p->inputnames->v, 0,
+                               cp->dontdelete);
 }
 
 
diff --git a/bin/convolve/ui.c b/bin/convolve/ui.c
index f0eba4fa..c9517592 100644
--- a/bin/convolve/ui.c
+++ b/bin/convolve/ui.c
@@ -657,18 +657,19 @@ ui_preparations(struct convolveparams *p)
   /* Set the output name if the user hasn't set it. */
   if(cp->output==NULL)
     cp->output=gal_checkset_automatic_output(cp, p->filename, outsuffix);
-  gal_checkset_writable_remove(cp->output, 0, cp->dontdelete);
+  gal_checkset_writable_remove(cp->output, p->filename, 0, cp->dontdelete);
   if(p->checkfreqsteps)
     {
       p->freqstepsname=gal_checkset_automatic_output(cp, p->filename,
                                                      "_freqsteps.fits");
-      gal_checkset_writable_remove(p->freqstepsname, 0, cp->dontdelete);
+      gal_checkset_writable_remove(p->freqstepsname, p->filename, 0,
+                                   cp->dontdelete);
     }
   if(cp->tl.checktiles)
     {
       cp->tl.tilecheckname=gal_checkset_automatic_output(cp, p->filename,
                                                          "_tiled.fits");
-      gal_checkset_writable_remove(cp->tl.tilecheckname, 0,
+      gal_checkset_writable_remove(cp->tl.tilecheckname, p->filename, 0,
                                    cp->dontdelete);
     }
 }
diff --git a/bin/crop/crop.c b/bin/crop/crop.c
index 9a91d071..1f28ecee 100644
--- a/bin/crop/crop.c
+++ b/bin/crop/crop.c
@@ -483,7 +483,7 @@ crop(struct cropparams *p)
             error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
           gal_list_str_add(&comments, tmp, 0);
         }
-      gal_checkset_writable_remove(LOGFILENAME, 0, p->cp.dontdelete);
+      gal_checkset_writable_remove(LOGFILENAME, NULL, 0, p->cp.dontdelete);
       gal_table_write_log(p->log, PROGRAM_STRING, &p->rawtime, comments,
                           LOGFILENAME, p->cp.quiet);
       gal_list_str_free(comments, 1);
diff --git a/bin/crop/onecrop.c b/bin/crop/onecrop.c
index 537b1a53..3abbd968 100644
--- a/bin/crop/onecrop.c
+++ b/bin/crop/onecrop.c
@@ -405,7 +405,7 @@ onecrop_name(struct onecropparams *crp)
           error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
 
       /* Make sure the file doesn't exist. */
-      gal_checkset_writable_remove(crp->name, cp->keep, cp->dontdelete);
+      gal_checkset_writable_remove(crp->name, NULL, cp->keep, cp->dontdelete);
     }
   else
     {
@@ -413,14 +413,13 @@ onecrop_name(struct onecropparams *crp)
       if(p->outnameisfile)            /* An output file was specified. */
         {
           crp->name=cp->output;
-          gal_checkset_writable_remove(crp->name, cp->keep, cp->dontdelete);
+          gal_checkset_writable_remove(crp->name, p->imgs[crp->in_ind].name,
+                                       cp->keep, cp->dontdelete);
         }
       else          /* The output was a directory, use automatic output. */
-        {
         crp->name=gal_checkset_automatic_output(cp,
                                                 p->imgs[crp->in_ind].name,
                                                 p->suffix);
-        }
     }
 }
 
diff --git a/bin/crop/ui.c b/bin/crop/ui.c
index d5da5507..eb5d0417 100644
--- a/bin/crop/ui.c
+++ b/bin/crop/ui.c
@@ -505,7 +505,8 @@ ui_check_options_and_arguments(struct cropparams *p)
   else
     {
       p->cp.numthreads=1;
-      p->outnameisfile=gal_checkset_dir_0_file_1(&p->cp, p->cp.output);
+      p->outnameisfile=gal_checkset_dir_0_file_1(&p->cp, p->cp.output,
+                                                 p->inputs->v);
     }
 }
 
diff --git a/bin/fits/keywords.c b/bin/fits/keywords.c
index 7c08dcd8..95349304 100644
--- a/bin/fits/keywords.c
+++ b/bin/fits/keywords.c
@@ -647,7 +647,7 @@ keywords_wcs_convert(struct fitsparams *p)
         error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
       output=gal_checkset_automatic_output(&p->cp, p->input->v, suffix);
     }
-  gal_checkset_writable_remove(output, 0, p->cp.dontdelete);
+  gal_checkset_writable_remove(output, p->input->v, 0, p->cp.dontdelete);
 
   /* Write the output file. */
   if(data)
@@ -985,7 +985,8 @@ keywords_value(struct fitsparams *p)
     }
 
   /* Write the values. */
-  gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+  gal_checkset_writable_remove(p->cp.output, p->input->v, 0,
+                               p->cp.dontdelete);
   gal_table_write(out, NULL, NULL, p->cp.tableformat,
                   p->cp.output, "KEY-VALUES", p->colinfoinstdout);
 
diff --git a/bin/fits/meta.c b/bin/fits/meta.c
index 3a445f89..5e560e0b 100644
--- a/bin/fits/meta.c
+++ b/bin/fits/meta.c
@@ -64,7 +64,7 @@ meta_initialize(struct fitsparams *p, gal_warp_wcsalign_t *wa)
 
   /* Check if we're allowed to delete the output image. If not, fail FAST
      before any CPU-intensive process. */
-  gal_checkset_writable_remove(cp->output, 0, cp->dontdelete);
+  gal_checkset_writable_remove(cp->output, inputname, 0, cp->dontdelete);
 
   /* Read the input image and its WCS, must free it when done. */
   input=gal_array_read_one_ch_to_type(inputname, hdu, NULL,
diff --git a/bin/fits/ui.c b/bin/fits/ui.c
index a35e7c54..74505e50 100644
--- a/bin/fits/ui.c
+++ b/bin/fits/ui.c
@@ -514,7 +514,8 @@ ui_read_check_mode_extension(struct fitsparams *p)
       else
         {
           if(p->cp.output)
-            gal_checkset_writable_remove(p->cp.output, 1, p->cp.dontdelete);
+            gal_checkset_writable_remove(p->cp.output, p->input->v, 1,
+                                         p->cp.dontdelete);
           else
             p->cp.output=gal_checkset_automatic_output(&p->cp, p->input->v,
                                                        "_ext.fits");
diff --git a/bin/match/ui.c b/bin/match/ui.c
index 53806b02..7e0ce856 100644
--- a/bin/match/ui.c
+++ b/bin/match/ui.c
@@ -1017,7 +1017,7 @@ ui_preparations_out_name(struct matchparams *p)
         }
 
       /* Make sure a file with this name doesn't exist. */
-      gal_checkset_writable_remove(p->logname, 0, p->cp.dontdelete);
+      gal_checkset_writable_remove(p->logname, NULL, 0, p->cp.dontdelete);
 
       /* The main output name needs to be available in p->out1name (for the
          final step when we want to write the input configurations as FITS
@@ -1039,7 +1039,8 @@ ui_preparations_out_name(struct matchparams *p)
               p->out1name = gal_checkset_automatic_output(&p->cp,
                                                           refname, suffix);
             }
-          gal_checkset_writable_remove(p->out1name, 0, p->cp.dontdelete);
+          gal_checkset_writable_remove(p->out1name, refname, 0,
+                                       p->cp.dontdelete);
         }
       else
         {
@@ -1089,8 +1090,10 @@ ui_preparations_out_name(struct matchparams *p)
             }
 
           /* Make sure no file with these names exists. */
-          gal_checkset_writable_remove(p->out1name, 0, p->cp.dontdelete);
-          gal_checkset_writable_remove(p->out2name, 0, p->cp.dontdelete);
+          gal_checkset_writable_remove(p->out1name, refname, 0,
+                                       p->cp.dontdelete);
+          gal_checkset_writable_remove(p->out2name, refname, 0,
+                                       p->cp.dontdelete);
         }
 
       /* If a log file is necessary, set its name here. */
@@ -1099,7 +1102,8 @@ ui_preparations_out_name(struct matchparams *p)
           p->logname = ( p->cp.tableformat==GAL_TABLE_FORMAT_TXT
                          ? PROGRAM_EXEC".txt"
                          : PROGRAM_EXEC".fits" );
-          gal_checkset_writable_remove(p->logname, 0, p->cp.dontdelete);
+          gal_checkset_writable_remove(p->logname, refname, 0,
+                                       p->cp.dontdelete);
         }
     }
 }
diff --git a/bin/mkcatalog/ui.c b/bin/mkcatalog/ui.c
index a9385b4c..aaa31fb9 100644
--- a/bin/mkcatalog/ui.c
+++ b/bin/mkcatalog/ui.c
@@ -1541,7 +1541,18 @@ ui_preparations_both_names(struct mkcatalogparams *p)
     }
   else
     {
-      p->objectsout=gal_checkset_automatic_output(&p->cp, basename, suffix);
+      /* The output file is a FITS file (the 'p->cp.tableformat' has been
+         set before), so a single file output is enough. If no output name
+         has been given, then use automatic output. Otherwise, just check
+         if the given name is writable. */
+      if(p->cp.output)
+        {
+          gal_checkset_allocate_copy(p->cp.output, &p->objectsout);
+          gal_checkset_writable_remove(p->objectsout, p->objectsfile, 0,
+                                       p->cp.dontdelete);
+        }
+      else
+        p->objectsout=gal_checkset_automatic_output(&p->cp, basename, suffix);
       p->clumpsout=p->objectsout;
     }
 
@@ -1578,7 +1589,8 @@ ui_preparations_outnames(struct mkcatalogparams *p)
       if(p->clumps) ui_preparations_both_names(p);
       else
         {
-          gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+          gal_checkset_writable_remove(p->cp.output, p->objectsfile, 0,
+                                       p->cp.dontdelete);
           gal_checkset_allocate_copy(p->cp.output, &p->objectsout);
         }
     }
diff --git a/bin/mknoise/ui.c b/bin/mknoise/ui.c
index a02e90b4..3f396e1d 100644
--- a/bin/mknoise/ui.c
+++ b/bin/mknoise/ui.c
@@ -326,7 +326,8 @@ ui_preparations(struct mknoiseparams *p)
 
   /* Set the output name: */
   if(p->cp.output)
-    gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+    gal_checkset_writable_remove(p->cp.output, p->inputname, 0,
+                                 p->cp.dontdelete);
   else
     p->cp.output=gal_checkset_automatic_output(&p->cp, p->inputname,
                                                "_noised.fits");
diff --git a/bin/mkprof/mkprof.c b/bin/mkprof/mkprof.c
index 41727698..f31b5e5f 100644
--- a/bin/mkprof/mkprof.c
+++ b/bin/mkprof/mkprof.c
@@ -144,7 +144,7 @@ saveindividual(struct mkonthread *mkp)
     {
       if( asprintf(&filename, "%s%zu_%s", outdir, ibq->id, p->basename)<0 )
         error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
-      gal_checkset_writable_remove(filename, 0, p->cp.dontdelete);
+      gal_checkset_writable_remove(filename, NULL, 0, p->cp.dontdelete);
     }
 
 
@@ -872,7 +872,7 @@ mkprof(struct mkprofparams *p)
       if( asprintf(&tmp, "Zeropoint: %g", p->zeropoint)<0 )
         error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
       gal_list_str_add(&comments, tmp, 0);
-      gal_checkset_writable_remove(LOGFILENAME, 0, p->cp.dontdelete);
+      gal_checkset_writable_remove(LOGFILENAME, NULL, 0, p->cp.dontdelete);
       gal_table_write_log(p->log, PROGRAM_STRING, &p->rawtime, comments,
                           LOGFILENAME, p->cp.quiet);
       gal_list_str_free(comments, 1);
diff --git a/bin/mkprof/ui.c b/bin/mkprof/ui.c
index 081175c8..e001785b 100644
--- a/bin/mkprof/ui.c
+++ b/bin/mkprof/ui.c
@@ -636,7 +636,7 @@ ui_check_options_and_arguments(struct mkprofparams *p)
 
 
   /* Set the necessary output names. */
-  d0f1=gal_checkset_dir_0_file_1(&p->cp, p->cp.output);
+  d0f1=gal_checkset_dir_0_file_1(&p->cp, p->cp.output, p->catname);
   if(d0f1)                        /* --output is a file name. */
     {
       p->mergedimgname=p->cp.output;
@@ -662,7 +662,7 @@ ui_check_options_and_arguments(struct mkprofparams *p)
   /* If a merged image is requested (or '--kernel' is called), then delete
      the final filename if it exists. */
   if(p->nomerged==0 && p->kernel)
-    gal_checkset_writable_remove(p->mergedimgname, p->cp.keep,
+    gal_checkset_writable_remove(p->mergedimgname, p->catname, p->cp.keep,
                                  p->cp.dontdelete);
 }
 
diff --git a/bin/noisechisel/ui.c b/bin/noisechisel/ui.c
index 89717cc8..bd2a725f 100644
--- a/bin/noisechisel/ui.c
+++ b/bin/noisechisel/ui.c
@@ -373,7 +373,8 @@ ui_set_output_names(struct noisechiselparams *p)
   if(output)
     {
       /* Delete the file if it already exists. */
-      gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+      gal_checkset_writable_remove(p->cp.output, p->inputname, 0,
+                                   p->cp.dontdelete);
 
       /* When the output name is given (possibly with directory
          information), the check images will also be put in that same
diff --git a/bin/query/ui.c b/bin/query/ui.c
index 90eff97a..7dbdb9e9 100644
--- a/bin/query/ui.c
+++ b/bin/query/ui.c
@@ -403,7 +403,7 @@ ui_read_check_only_options(struct queryparams *p)
   /* Make sure that the output name is in a writable location and that it
      doesn't exist. If it exists, and the user hasn't called
      '--dontdelete', then delete the existing file. */
-  gal_checkset_writable_remove(p->cp.output, p->cp.keep,
+  gal_checkset_writable_remove(p->cp.output, NULL, p->cp.keep,
                                p->cp.dontdelete);
 
   /* Set the suffix of the default download names for NED (since extinction
@@ -462,7 +462,7 @@ ui_read_check_only_options(struct queryparams *p)
          temporarily activating 'keepinputdir'. */
       keepinputdir=p->cp.keepinputdir;
       p->cp.keepinputdir=1;
-      gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+      gal_checkset_writable_remove(p->cp.output, NULL, 0, p->cp.dontdelete);
       p->downloadname=gal_checkset_automatic_output(&p->cp, p->cp.output,
                                                     rdsuffix);
       p->cp.keepinputdir=keepinputdir;
diff --git a/bin/script/psf-select-stars.in b/bin/script/psf-select-stars.in
index 8803ad0e..5abcf7b3 100644
--- a/bin/script/psf-select-stars.in
+++ b/bin/script/psf-select-stars.in
@@ -569,6 +569,7 @@ if [ x"$catalog" != x ]; then
         if [ -f "$catalog_main" ]; then
             echo "External Catalog already exists "
         else
+
             # Select stars with magnitude between brighter to fainter.
             asttable $catalog \
                      --range=$field,$brightmag,$fainter --sort=$field \
diff --git a/bin/segment/ui.c b/bin/segment/ui.c
index 121165c5..b70e63ea 100644
--- a/bin/segment/ui.c
+++ b/bin/segment/ui.c
@@ -366,7 +366,8 @@ ui_set_output_names(struct segmentparams *p)
   if(output)
     {
       /* Delete the file if it already exists. */
-      gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+      gal_checkset_writable_remove(p->cp.output, p->inputname, 0,
+                                   p->cp.dontdelete);
 
       /* When the output name is given (possibly with directory
          information), the check images will also be put in that same
diff --git a/bin/statistics/statistics.c b/bin/statistics/statistics.c
index 9fb77882..3814bf99 100644
--- a/bin/statistics/statistics.c
+++ b/bin/statistics/statistics.c
@@ -640,7 +640,7 @@ statistics_output_name(struct statisticsparams *p, char 
*suf, int *isfits)
   *isfits=strcmp(fix, "fits")==0;
 
   /* Make sure it doesn't already exist. */
-  gal_checkset_writable_remove(out, 0, p->cp.dontdelete);
+  gal_checkset_writable_remove(out, p->inputname, 0, p->cp.dontdelete);
 
   /* Clean up and return. */
   if(suffix) free(suffix);
@@ -677,7 +677,7 @@ write_output_table(struct statisticsparams *p, gal_data_t 
*table,
 
 
   /* Write the table. */
-  gal_checkset_writable_remove(output, 0, p->cp.dontdelete);
+  gal_checkset_writable_remove(output, p->inputname, 0, p->cp.dontdelete);
   gal_table_write(table, NULL, comments, p->cp.tableformat, output,
                   "TABLE", 0);
 
diff --git a/bin/statistics/ui.c b/bin/statistics/ui.c
index d39a8ac6..6445f0dc 100644
--- a/bin/statistics/ui.c
+++ b/bin/statistics/ui.c
@@ -1183,8 +1183,8 @@ ui_preparations(struct statisticsparams *p)
                                PROGRAM_NAME);
           else
             {
-              gal_checkset_writable_remove(tl->tilecheckname, 0,
-                                           cp->dontdelete);
+              gal_checkset_writable_remove(tl->tilecheckname, p->inputname,
+                                           0, cp->dontdelete);
               gal_table_write(check, NULL, NULL, cp->tableformat,
                               tl->tilecheckname, "TABLE", 0);
             }
@@ -1232,7 +1232,7 @@ ui_preparations(struct statisticsparams *p)
   p->cp.keepinputdir=keepinputdir;
 
   /* Make sure the output doesn't already exist. */
-  gal_checkset_writable_remove(p->cp.output, p->cp.keep,
+  gal_checkset_writable_remove(p->cp.output, p->inputname, p->cp.keep,
                                p->cp.dontdelete);
 
   /* Set the fit-estimate column, and prepare the weight based on the
diff --git a/bin/table/ui.c b/bin/table/ui.c
index 783afba7..c4faea03 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -1279,7 +1279,8 @@ ui_preparations(struct tableparams *p)
 
 
   /* Make sure the (possible) output name is writable. */
-  gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+  gal_checkset_writable_remove(p->cp.output, p->filename, 0,
+                               p->cp.dontdelete);
 
 
   /* If random rows are desired, we need to define a GSL random number
diff --git a/bin/warp/ui.c b/bin/warp/ui.c
index 351f9e59..25093daa 100644
--- a/bin/warp/ui.c
+++ b/bin/warp/ui.c
@@ -1113,7 +1113,8 @@ ui_preparations(struct warpparams *p)
      which we will need to determine the suffix if no output name is
      specified. */
   if(p->cp.output)
-    gal_checkset_writable_remove(p->cp.output, 0, p->cp.dontdelete);
+    gal_checkset_writable_remove(p->cp.output, p->inputname, 0,
+                                 p->cp.dontdelete);
   else
     p->cp.output=gal_checkset_automatic_output(&p->cp, p->inputname,
                                                ui_set_suffix(p));
diff --git a/doc/announce-acknowledge.txt b/doc/announce-acknowledge.txt
index 3f7f8437..fbe4e7db 100644
--- a/doc/announce-acknowledge.txt
+++ b/doc/announce-acknowledge.txt
@@ -1,5 +1,6 @@
 Alphabetically ordered list to acknowledge in the next release.
 
+Faezeh Bidjarchian
 Alejandro Serrano Borlaff
 Sepideh Eskandarlou
 Zohreh Ghaffari
diff --git a/lib/arithmetic.c b/lib/arithmetic.c
index 8bae6156..92c77cb2 100644
--- a/lib/arithmetic.c
+++ b/lib/arithmetic.c
@@ -2566,8 +2566,8 @@ arithmetic_box_around_ellipse(gal_data_t *d1, gal_data_t 
*d2,
       d1->size=0; return d1;
     }
 
-  /* Convert the two inputs into double. Note that if the user doesn't want
-     to free the inputs, we should make a copy of 'a_data' and 'b_data'
+  /* Convert the inputs into double. Note that if the user doesn't want to
+     free the inputs, we should make a copy of 'a_data' and 'b_data'
      because the output will also be written in them. */
   a_data=( ( d1->type==GAL_TYPE_FLOAT64
              || flags & GAL_ARITHMETIC_FLAG_FREE )
diff --git a/lib/checkset.c b/lib/checkset.c
index 04a8f940..27024598 100644
--- a/lib/checkset.c
+++ b/lib/checkset.c
@@ -708,7 +708,8 @@ gal_checkset_writable_notexist(char *filename)
    won't be deleted, but the program will abort with an error, informing
    the user that the output can't be made. */
 void
-gal_checkset_writable_remove(char *filename, int keep, int dontdelete)
+gal_checkset_writable_remove(char *filename, char *basename, int keep,
+                             int dontdelete)
 {
   char *dir;
   FILE *tmpfile;
@@ -718,6 +719,16 @@ gal_checkset_writable_remove(char *filename, int keep, int 
dontdelete)
   if(filename==NULL)
     return;
 
+  /* If a base-name was given, then this name may be related to the input's
+     name. In that case, we should check if they are the same string. */
+  if(basename)
+    if( !strcmp(filename, basename) )
+      error(EXIT_FAILURE, 0, "the output filename name ('%s') is the same "
+            "as the input's file name! The output file is re-written if "
+            "it already exists. Therefore, when the input and output file "
+            "names are the same there is a problem! Please select a "
+            "different output name", filename);
+
   /* We want to make sure that we can open and write to this file. But
      the user might have asked to not delete the file, so the
      contents should not be changed. Therefore we have to open it with
@@ -774,7 +785,8 @@ gal_checkset_writable_remove(char *filename, int keep, int 
dontdelete)
    a directory) it will return 0. Finally, if it exists but cannot be
    deleted, report an error and abort. */
 int
-gal_checkset_dir_0_file_1(struct gal_options_common_params *cp, char *name)
+gal_checkset_dir_0_file_1(struct gal_options_common_params *cp, char *name,
+                          char *basename)
 {
   FILE *tmpfile;
   struct stat nameinfo;
@@ -813,7 +825,7 @@ gal_checkset_dir_0_file_1(struct gal_options_common_params 
*cp, char *name)
     return 0;
   else if (S_ISREG(nameinfo.st_mode))  /* It is a file, GOOD. */
     {
-      gal_checkset_writable_remove(name, cp->keep, cp->dontdelete);
+      gal_checkset_writable_remove(name, basename, cp->keep, cp->dontdelete);
       return 1;
     }
   else                                 /* Not a file or a dir, ABORT */
@@ -899,7 +911,7 @@ gal_checkset_automatic_output(struct 
gal_options_common_params *cp,
     }
 
   /* Remove the created filename if it already exits. */
-  gal_checkset_writable_remove(out, cp->keep, cp->dontdelete);
+  gal_checkset_writable_remove(out, inname, cp->keep, cp->dontdelete);
 
   /* Return the resulting filename. */
   return out;
diff --git a/lib/gnuastro-internal/checkset.h b/lib/gnuastro-internal/checkset.h
index 9093f7ca..6b5f0102 100644
--- a/lib/gnuastro-internal/checkset.h
+++ b/lib/gnuastro-internal/checkset.h
@@ -124,10 +124,12 @@ int
 gal_checkset_writable_notexist(char *filename);
 
 void    /* keep==0 && dontdelete==0: file will be deleted if exists.*/
-gal_checkset_writable_remove(char *filename, int keep, int dontdelete);
+gal_checkset_writable_remove(char *filename, char *basename, int keep,
+                             int dontdelete);
 
 int
-gal_checkset_dir_0_file_1(struct gal_options_common_params *cp, char *name);
+gal_checkset_dir_0_file_1(struct gal_options_common_params *cp, char *name,
+                          char *basename);
 
 char *
 gal_checkset_automatic_output(struct gal_options_common_params *cp,
diff --git a/lib/label.c b/lib/label.c
index 1170bae1..dce5616c 100644
--- a/lib/label.c
+++ b/lib/label.c
@@ -222,7 +222,7 @@ gal_label_watershed(gal_data_t *values, gal_data_t *indexs,
                                   GAL_TYPE_INVALID, 2, checkdsize,
                                   NULL, 0, 0, NULL, NULL, NULL);
   tile->block=values;
-  gal_checkset_writable_remove(filename, 0, 0);
+  gal_checkset_writable_remove(filename, NULL, 0, 0);
   crop=gal_data_copy(tile);
   gal_fits_img_write(crop, filename, NULL, PROGRAM_NAME);
   gal_data_free(crop);
diff --git a/lib/options.c b/lib/options.c
index f1c2a066..fd315c43 100644
--- a/lib/options.c
+++ b/lib/options.c
@@ -3043,7 +3043,7 @@ options_print_all(struct gal_options_common_params *cp, 
char *dirname,
         error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
 
       /* Remove the file if it already exists. */
-      gal_checkset_writable_remove(filename, 0, 0);
+      gal_checkset_writable_remove(filename, NULL, 0, 0);
 
       /* Open the file for writing */
       errno=0;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]