curl-users
Re: cURL TODO : 15.3 prevent file overwriting
Date: Sat, 24 May 2014 19:50:43 +0200 (CEST)
On Mon, 19 May 2014, Deepak Singh wrote:
> This is an attempt to fix one of the TO DO list activities in cURL.
> Problem: 15.3 prevent file overwriting: Add an option that prevents cURL
> from overwriting existing local files. When used, and there already is an
> existing file with the target file name (either -O or -o), a number should
> be appended (and increased if already existing). So that index.html becomes
> first index.html.1 and then index.html.2 etc.
Thanks for your contribution!
I think it looks good. It needs a little fixing though but I don't see any
major obstacles.
> Solution: Added a new option 'n' which can be used with 'O' and 'o' options
> to solve the above problem,
We can't use 'n' for this since -n is already occupied. In your case your code
works mostly due to a side-effect of the parsing code, but it is important
that when using a single dash on the command line there can only be
single-letter options. That means "-On" must equal "-O -n".
> - curl --remote-name-no-overwrite http://curl.haxx.se/docs/manpage.html
Can we call it --remote-name-safe instead to make it a little shorter?
Two things on the code:
1) Your code is also using sprintf() instead of the more proper snprintf()
which would provide a boundary check.
2) There's a race condition in the code so if this would run on multiple
instances it would end up overwriting a file anyway. I suggest you instead use
open() and O_EXCL|O_CREAT so that it'll create the file if it doesn't exist
and thus avoiding the race.
-- / daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-users
FAQ: http://curl.haxx.se/docs/faq.html
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-05-24