Don't overwrite the output file

View: New views
4 Messages — Rating Filter:   Alert me  

Don't overwrite the output file

by Stefan Bauer-12 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Don't overwrite the output file

In case the output file specified via -F already exists, it is overwritten
without a hint to the user.
The patch changes the behavior so that an output file is never touched if it
is already writeable.

---

Alternative: Rename the existing file to filename~.

Shortcomings: The program aborts late - the whole input data is read from
fname before the check is executed. The realize a early check, the main()
function has to be redesigned to first read _all_ options before anything is
read from fname.
Please tell me if such a rearranging is appreciated.

diff --git a/main.c b/main.c
index 778d687..6b4195e 100644
--- a/main.c
+++ b/main.c
@@ -218,6 +218,21 @@ print_extended_info(void)
  "\n");
 }
 
+/**
+ * @return 1 if file exits and is writeable, otherwise 0
+ */
+static int
+file_writeable(const char *filename)
+{
+ FILE *f = fopen(filename, "w");
+ int retval = f != NULL;
+
+ if (f != NULL)
+ fclose(f);
+
+ return retval;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -373,6 +388,12 @@ main(int argc, char *argv[])
  if (ofname == NULL) {
  fatal ("No output file or device name specified.\n");
  }
+
+ if (file_writeable(ofname))
+ {
+ fatal ("Output file already exists.\n");
+ }
+
  if (ovecs && (!(global_opts.masked_objective & POSNDATAMASK))) {
  /* simulates the default behaviour of waypoints */
  if (doing_nothing)

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gpsbabel-code mailing list  http://www.gpsbabel.org
Gpsbabel-code@...
https://lists.sourceforge.net/lists/listinfo/gpsbabel-code

Re: Don't overwrite the output file

by Khai Mong :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

The code won't work as it is.  The mere act of fopen(..., "w") will have overwritten the file and truncated it.   There are are other ways of testing what you want.

On the other hand, for a command lline execution, it is normal and expected behavior to ovewrite existing files.



On Thu, Sep 10, 2009 at 5:55 PM, Stefan Bauer <yoltia-maintain@...> wrote:
Don't overwrite the output file

In case the output file specified via -F already exists, it is overwritten
without a hint to the user.
The patch changes the behavior so that an output file is never touched if it
is already writeable.

---

Alternative: Rename the existing file to filename~.

Shortcomings: The program aborts late - the whole input data is read from
fname before the check is executed. The realize a early check, the main()
function has to be redesigned to first read _all_ options before anything is
read from fname.
Please tell me if such a rearranging is appreciated.

diff --git a/main.c b/main.c
index 778d687..6b4195e 100644
--- a/main.c
+++ b/main.c
@@ -218,6 +218,21 @@ print_extended_info(void)
       "\n");
 }

+/**
+ * @return 1 if file exits and is writeable, otherwise 0
+ */
+static int
+file_writeable(const char *filename)
+{
+       FILE *f = fopen(filename, "w");
+       int retval = f != NULL;
+
+       if (f != NULL)
+               fclose(f);
+
+       return retval;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -373,6 +388,12 @@ main(int argc, char *argv[])
                               if (ofname == NULL) {
                                       fatal ("No output file or device name specified.\n");
                               }
+
+                               if (file_writeable(ofname))
+                               {
+                                       fatal ("Output file already exists.\n");
+                               }
+
                               if (ovecs && (!(global_opts.masked_objective & POSNDATAMASK))) {
                                       /* simulates the default behaviour of waypoints */
                                       if (doing_nothing)

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gpsbabel-code mailing list  http://www.gpsbabel.org
Gpsbabel-code@...
https://lists.sourceforge.net/lists/listinfo/gpsbabel-code


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gpsbabel-code mailing list  http://www.gpsbabel.org
Gpsbabel-code@...
https://lists.sourceforge.net/lists/listinfo/gpsbabel-code

Parent Message unknown Re: Don't overwrite the output file

by Stefan Bauer-12 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Friday 11 September 2009 00:34:26 you wrote:
> Hello,
>
> The code won't work as it is.  The mere act of fopen(..., "w") will have
> overwritten the file and truncated it.

Thanks for pointing this out.

> There are are other ways of
> testing what you want.

I know of 3 methods: fopen, fstat, access. The latter ones are not platform
independent. Any suggestions?

> On the other hand, for a command lline execution, it is normal and expected
> behavior to ovewrite existing files.

I ran into this issue as today I accidently was about to overwrite the track I
downloaded the day before just because of reusing the complete command line
from history.
There ary many command line tools where overwriting must be forced.

Regards,
Stefan

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gpsbabel-code mailing list  http://www.gpsbabel.org
Gpsbabel-code@...
https://lists.sourceforge.net/lists/listinfo/gpsbabel-code

Re: Don't overwrite the output file

by Andy Armstrong :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11 Sep 2009, at 00:06, Stefan Bauer wrote:
> I ran into this issue as today I accidently was about to overwrite  
> the track I
> downloaded the day before just because of reusing the complete  
> command line
> from history.
> There ary many command line tools where overwriting must be forced.


It's still the exception rather than the rule and changing the  
behaviour of gpsbabel in this way will break extant scripts that rely  
on the old behaviour.

If you want to make sure it can't happen by accident put

[ -e out.gpx ] || gpsbabel ... -F out.kpx

in your command line history.

--
Andy Armstrong, Hexten




------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gpsbabel-code mailing list  http://www.gpsbabel.org
Gpsbabel-code@...
https://lists.sourceforge.net/lists/listinfo/gpsbabel-code