git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "René Scharfe" <rene.scharfe@lsrfire.ath.cx>
To: git@vger.kernel.org
Cc: msysgit@googlegroups.com
Subject: Re: [PATCH] Handle rename of case only, for Windows
Date: Sun, 30 Jan 2011 03:22:24 +0100	[thread overview]
Message-ID: <4D44CB60.8000506@lsrfire.ath.cx> (raw)
In-Reply-To: <1296344717.25999.1417928123@webmail.messagingengine.com>

Am 30.01.2011 00:45, schrieb Tim Abell:
>> Hmm, not so good. st_ino is always 0 on Windows, so this would make
>> false positives, no?
> 
> I tested this on windows 7 under cygwin (which is what I have
> available) and st_ino reports real numbers for me, I also tested that
> attempting to overwrite another file without --force still fails and
> added a new test case for this scenario. I have now made sure that if
> zero is returned then git won't accidentally overwrite other files as
> I hadn't thought of this before. So this patch shouldn't be
> regressive even if other versions of windows or other filesystems
> don't provide valid inode data.

On MinGW on Vista st_ino is zero, git mv refuses to overwrite and the
added case change test fails as a consequence.

>> I wonder if we can make lstat_case() that would only return 0 if it
>> matches exactly the filename, even on FAT. FindFirstFile/FindNextFile
>> should return true file name, I think. If not, we can make
>> lstat_case() take two paths (src and dst) and move all inode
>> comparison code in there. Much cleaner.
> 
> I'm afraid this is a bit beyond me at the moment, but I'm fairly
> happy with the solution I have. Thanks for the feedback though.

Hmm, if the patch only works for Cygwin and maybe OS X it's a step
forward, I guess.  A more complete solution would be better, of course.

> diff --git a/builtin/mv.c b/builtin/mv.c
> index cdbb094..c2f726a 100644
> --- a/builtin/mv.c
> +++ b/builtin/mv.c
> @@ -165,17 +165,27 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
>  		} else if (cache_name_pos(src, length) < 0)
>  			bad = "not under version control";
>  		else if (lstat(dst, &st) == 0) {
> -			bad = "destination exists";
> -			if (force) {
> -				/*
> -				 * only files can overwrite each other:
> -				 * check both source and destination
> -				 */
> -				if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
> -					warning("%s; will overwrite!", bad);
> -					bad = NULL;
> -				} else
> -					bad = "Cannot overwrite";
> +			/* If we are on a case insensitive file system (windows) and we are only
> +			 * changing the case of the file then lstat for the destination will
> +			 * return != 0 because it sees the source file.
> +			 * To prevent this causing failure, lstat is used to get the inode of the src
> +			 * and see if it's actually the same file as dst. If the inode == 0 then
> +			 * we can't tell whether it is the same file so we fail regardless. */

Can you make the lines 80 characters long at most?  This subtly helps
avoiding excessive levels of indentation by encouraging to factor out
nice and small functions.

> +			struct stat src_st;
> +			lstat(src, &src_st);

Shouldn't you check the return value of this call?  OK, the source
probably always exists, but still.  Oh, we actually know that because
that's the first lstat() call in this for loop.  You can reuse its
result instead of calling the function again.

> +			if (src_st.st_ino == 0 || src_st.st_ino != st.st_ino) {

It may be nice to avoid adding another level of indentation by combining
this if with the preceding one.

> +				bad = "destination exists";
> +				if (force) {
> +					/*
> +					 * only files can overwrite each other:
> +					 * check both source and destination
> +					 */
> +					if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
> +						warning("%s; will overwrite!", bad);
> +						bad = NULL;
> +					} else
> +						bad = "Cannot overwrite";
> +				}
>  			}
>  		} else if (string_list_has_string(&src_for_dst, dst))
>  			bad = "multiple sources for the same target";

René

  reply	other threads:[~2011-01-30  2:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-29 23:45 [PATCH] Handle rename of case only, for Windows Tim Abell
2011-01-30  2:22 ` René Scharfe [this message]
2011-01-30 16:53   ` Tim Abell
2011-01-30 21:39     ` Jonathan Nieder
2011-02-10 18:58 ` Ramsay Jones
  -- strict thread matches above, loose matches on Subject: below --
2011-01-14 13:44 [PATCH] handle rename of case only, for windows Tim Abell
2011-01-14 14:17 ` Nguyen Thai Ngoc Duy
2011-01-14 13:41 Tim Abell
2011-01-14 14:22 ` Erik Faye-Lund

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D44CB60.8000506@lsrfire.ath.cx \
    --to=rene.scharfe@lsrfire.ath.cx \
    --cc=git@vger.kernel.org \
    --cc=msysgit@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).