# [PATCH 0/1] add: respect --ignore-errors when lstat() reports errors

6 messages from 2019-10-26 to 2019-11-03. Participants: Qusielle via GitGitGadget, qusielle via GitGitGadget, Junio C Hamano, qusielle@gmail.com.
Thread: https://gitlist.dev/t/52129

## Qusielle via GitGitGadget, 2019-10-26 21:59

Subject: [PATCH 0/1] add: respect --ignore-errors when lstat() reports errors
Message-ID: <pull.432.git.1572127149.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.432.git.1572127149.gitgitgadget%40gmail.com

```
"git add --ignore-errors" command fails immediately when lstat returns an
error, despite the ignore errors' flag is enabled.

There could be files that triggers an error on stat(), when other files
proceed correctly. Issue can be reproduced when running git under Cygwin and
some target files have utf-8 long names (200+ utf chars). Windows can handle
them, but all operations on them failed under Cygwin. Issue can not be
reproduced with usual latin/numeric only names. For example, create a file
with 220 'й' letters by Windows Explorer, then in Cygwin:

Here and below "ййй..." means the line of й copied 220 times.
=============================================================

$ echo -n 'ййй...' | wc -c # check the real size 440

$ ls -la ls: cannot access 'ййй...'$'\320': No such file or directory
-????????? ? ? ? ? ? 'ййй...'$'\320'

$ ls й* ls: cannot access 'ййй...'$'\320': No such file or directory

$ stat й* stat: cannot stat 'ййй...'$'\320': No such file or directory

In my perspective, it's okay to skip these problematic files when ignore
error flag is specified, but official Git terminates entire git add command
when such files come up. But with proposed patch it is the desired behavior:

$ git add --ignore-errors . error: ййй... can only add regular files,
symbolic links or git-directories

All other files have been added correctly.
==========================================

Signed-off-by: Qusielle <qusielle@gmail.com>

qusielle (1):
  add: respect `--ignore-errors` when `lstat()` reports errors

 read-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


base-commit: 566a1439f6f56c2171b8853ddbca0ad3f5098770
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-432%2Fqusielle%2Fmaster-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-432/qusielle/master-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/432
-- 
gitgitgadget

```

## qusielle via GitGitGadget, 2019-10-26 21:59

Subject: [PATCH 1/1] add: respect `--ignore-errors` when `lstat()` reports errors
Message-ID: <fd022f88f54f6cf0feb61965b2dc47bca64c0937.1572127149.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/fd022f88f54f6cf0feb61965b2dc47bca64c0937.1572127149.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.432.git.1572127149.gitgitgadget@gmail.com>

```
From: qusielle <31454380+qusielle@users.noreply.github.com>

"git add --ignore-errors" command fails immediately when lstat returns
an error, despite the ignore errors' flag is enabled.

There could be files that triggers an error on stat(), when other files
proceed correctly.
Issue can be reproduced when running git under Cygwin and some target files
have utf-8 long names (200+ utf chars). Windows can handle them, but all
operations on them failed under Cygwin.
Issue can not be reproduced with usual latin/numeric only names.
For example, create a file with 220 'й' letters by Windows Explorer,
then in Cygwin:

 # Here and below "ййй..." means the line of й copied 220 times.
$ echo -n 'ййй...' | wc -c  # check the real size
440

$ ls -la
ls: cannot access 'ййй...'$'\320': No such file or directory
-????????? ? ?              ?        ?            ? 'ййй...'$'\320'

$ ls й*
ls: cannot access 'ййй...'$'\320': No such file or directory

$ stat й*
stat: cannot stat 'ййй...'$'\320': No such file or directory

In my perspective, it's okay to skip these problematic files when ignore
error flag is specified, but official Git terminates entire git add command
when such files come up. But with proposed patch it is the desired behavior:

$ git add --ignore-errors .
error: ййй... can only add regular files, symbolic links or git-directories
 # All other files have been added correctly.

Signed-off-by: Qusielle <qusielle@gmail.com>
---
 read-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/read-cache.c b/read-cache.c
index 133f790fa4..67237ecd29 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -801,7 +801,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 int add_file_to_index(struct index_state *istate, const char *path, int flags)
 {
 	struct stat st;
-	if (lstat(path, &st))
+	if (lstat(path, &st) && !(flags & ADD_CACHE_IGNORE_ERRORS))
 		die_errno(_("unable to stat '%s'"), path);
 	return add_to_index(istate, path, &st, flags);
 }
-- 
gitgitgadget

```

## Junio C Hamano, 2019-10-28 02:03

Subject: Re: [PATCH 1/1] add: respect `--ignore-errors` when `lstat()` reports errors
Message-ID: <xmqq36fdbp8v.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqq36fdbp8v.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <fd022f88f54f6cf0feb61965b2dc47bca64c0937.1572127149.git.gitgitgadget@gmail.com>

```
"qusielle via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: qusielle <31454380+qusielle@users.noreply.github.com>
>
> "git add --ignore-errors" command fails immediately when lstat returns
> an error, despite the ignore errors' flag is enabled.
> ...
> diff --git a/read-cache.c b/read-cache.c
> index 133f790fa4..67237ecd29 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -801,7 +801,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
>  int add_file_to_index(struct index_state *istate, const char *path, int flags)
>  {
>  	struct stat st;
> -	if (lstat(path, &st))
> +	if (lstat(path, &st) && !(flags & ADD_CACHE_IGNORE_ERRORS))
>  		die_errno(_("unable to stat '%s'"), path);
>  	return add_to_index(istate, path, &st, flags);
>  }

The only callers of this function that matter calls it and then
responds to an error return like so:

(in builtin/add.c::update_callback())

	if (add_file_to_index(&the_index, path,	data->flags)) {
		if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
			die(_("updating files failed"));


(in builtin/add.c::add_files(), where ignore_add_errors was used to
set the ADD_CACHE_IGNORE_ERRORS to flags in its caller)

	if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
		if (!ignore_add_errors)
			die(_("adding files failed"));

So you correctly identified what is the right place to fix.  We
should not "die_errno()"; we should give the control back to the
caller instead.

But after a failed stat, the code with your patch still calls
add_to_index() using the now undefined stat data, which would
contaminate the in-core index with wrong data.  

I think we should instead return without touching the index for the
path we had trouble lstat()ing.

IOW

	if (lstat(path, &st)) {
		if (flags & ADD_CACHE_IGNORE_ERRORS)
			return -1;
		else
			die_errno(_("unable to ..."));
	}
	return add_to_index(...);


```

## qusielle@gmail.com, 2019-11-03 20:17

Subject: Re: Re: [PATCH 1/1] add: respect `--ignore-errors` when `lstat()` reports errors
Message-ID: <eac0456e-0f61-9424-975d-0567bb92327d@gmail.com>
URL: https://gitlist.dev/e/eac0456e-0f61-9424-975d-0567bb92327d%40gmail.com
In-Reply-To: <xmqq36fdbp8v.fsf@gitster-ct.c.googlers.com>

```
Dear Junio,

Thank you for reviewing my patch. I completely agree with you, that 
add_to_index() should not be called with undefined data.

I will amend patch now with proposed changes.

Thank you!

Best regards,
Qusielle


On 28.10.2019 03:03, Junio C Hamano wrote:
> "qusielle via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>> From: qusielle <31454380+qusielle@users.noreply.github.com>
>>
>> "git add --ignore-errors" command fails immediately when lstat returns
>> an error, despite the ignore errors' flag is enabled.
>> ...
>> diff --git a/read-cache.c b/read-cache.c
>> index 133f790fa4..67237ecd29 100644
>> --- a/read-cache.c
>> +++ b/read-cache.c
>> @@ -801,7 +801,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
>>   int add_file_to_index(struct index_state *istate, const char *path, int flags)
>>   {
>>   	struct stat st;
>> -	if (lstat(path, &st))
>> +	if (lstat(path, &st) && !(flags & ADD_CACHE_IGNORE_ERRORS))
>>   		die_errno(_("unable to stat '%s'"), path);
>>   	return add_to_index(istate, path, &st, flags);
>>   }
> The only callers of this function that matter calls it and then
> responds to an error return like so:
>
> (in builtin/add.c::update_callback())
>
> 	if (add_file_to_index(&the_index, path,	data->flags)) {
> 		if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
> 			die(_("updating files failed"));
>
>
> (in builtin/add.c::add_files(), where ignore_add_errors was used to
> set the ADD_CACHE_IGNORE_ERRORS to flags in its caller)
>
> 	if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
> 		if (!ignore_add_errors)
> 			die(_("adding files failed"));
>
> So you correctly identified what is the right place to fix.  We
> should not "die_errno()"; we should give the control back to the
> caller instead.
>
> But after a failed stat, the code with your patch still calls
> add_to_index() using the now undefined stat data, which would
> contaminate the in-core index with wrong data.
>
> I think we should instead return without touching the index for the
> path we had trouble lstat()ing.
>
> IOW
>
> 	if (lstat(path, &st)) {
> 		if (flags & ADD_CACHE_IGNORE_ERRORS)
> 			return -1;
> 		else
> 			die_errno(_("unable to ..."));
> 	}
> 	return add_to_index(...);
>
>

```

## Qusielle via GitGitGadget, 2019-11-03 20:26

Subject: [PATCH v2 0/1] add: respect --ignore-errors when lstat() reports errors
Message-ID: <pull.432.v2.git.1572812819.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.432.v2.git.1572812819.gitgitgadget%40gmail.com
In-Reply-To: <pull.432.git.1572127149.gitgitgadget@gmail.com>

```
"git add --ignore-errors" command fails immediately when lstat returns an
error, despite the ignore errors' flag is enabled.

There could be files that triggers an error on stat(), when other files
proceed correctly. Issue can be reproduced when running git under Cygwin and
some target files have utf-8 long names (200+ utf chars). Windows can handle
them, but all operations on them failed under Cygwin. Issue can not be
reproduced with usual latin/numeric only names. For example, create a file
with 220 'й' letters by Windows Explorer, then in Cygwin:

Here and below "ййй..." means the line of й copied 220 times.
=============================================================

$ echo -n 'ййй...' | wc -c # check the real size 440

$ ls -la ls: cannot access 'ййй...'$'\320': No such file or directory
-????????? ? ? ? ? ? 'ййй...'$'\320'

$ ls й* ls: cannot access 'ййй...'$'\320': No such file or directory

$ stat й* stat: cannot stat 'ййй...'$'\320': No such file or directory

In my perspective, it's okay to skip these problematic files when ignore
error flag is specified, but official Git terminates entire git add command
when such files come up. But with proposed patch it is the desired behavior:

$ git add --ignore-errors . error: ййй... can only add regular files,
symbolic links or git-directories

All other files have been added correctly.
==========================================

Signed-off-by: Qusielle <qusielle@gmail.com>

qusielle (1):
  add: respect `--ignore-errors` when `lstat()` reports errors

 read-cache.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)


base-commit: 566a1439f6f56c2171b8853ddbca0ad3f5098770
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-432%2Fqusielle%2Fmaster-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-432/qusielle/master-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/432

Range-diff vs v1:

 1:  fd022f88f5 ! 1:  d88ea544d9 add: respect `--ignore-errors` when `lstat()` reports errors
     @@ -46,7 +46,13 @@
       {
       	struct stat st;
      -	if (lstat(path, &st))
     -+	if (lstat(path, &st) && !(flags & ADD_CACHE_IGNORE_ERRORS))
     - 		die_errno(_("unable to stat '%s'"), path);
     +-		die_errno(_("unable to stat '%s'"), path);
     ++	if (lstat(path, &st)) {
     ++		if (flags & ADD_CACHE_IGNORE_ERRORS)
     ++			return -1;
     ++		else
     ++			die_errno(_("unable to stat '%s'"), path);
     ++	}
       	return add_to_index(istate, path, &st, flags);
       }
     + 

-- 
gitgitgadget

```

## qusielle via GitGitGadget, 2019-11-03 20:26

Subject: [PATCH v2 1/1] add: respect `--ignore-errors` when `lstat()` reports errors
Message-ID: <d88ea544d93ffc9b222ac3a4974df4c499e76430.1572812819.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/d88ea544d93ffc9b222ac3a4974df4c499e76430.1572812819.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.432.v2.git.1572812819.gitgitgadget@gmail.com>

```
From: qusielle <31454380+qusielle@users.noreply.github.com>

"git add --ignore-errors" command fails immediately when lstat returns
an error, despite the ignore errors' flag is enabled.

There could be files that triggers an error on stat(), when other files
proceed correctly.
Issue can be reproduced when running git under Cygwin and some target files
have utf-8 long names (200+ utf chars). Windows can handle them, but all
operations on them failed under Cygwin.
Issue can not be reproduced with usual latin/numeric only names.
For example, create a file with 220 'й' letters by Windows Explorer,
then in Cygwin:

 # Here and below "ййй..." means the line of й copied 220 times.
$ echo -n 'ййй...' | wc -c  # check the real size
440

$ ls -la
ls: cannot access 'ййй...'$'\320': No such file or directory
-????????? ? ?              ?        ?            ? 'ййй...'$'\320'

$ ls й*
ls: cannot access 'ййй...'$'\320': No such file or directory

$ stat й*
stat: cannot stat 'ййй...'$'\320': No such file or directory

In my perspective, it's okay to skip these problematic files when ignore
error flag is specified, but official Git terminates entire git add command
when such files come up. But with proposed patch it is the desired behavior:

$ git add --ignore-errors .
error: ййй... can only add regular files, symbolic links or git-directories
 # All other files have been added correctly.

Signed-off-by: Qusielle <qusielle@gmail.com>
---
 read-cache.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/read-cache.c b/read-cache.c
index 133f790fa4..791ef65bac 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -801,8 +801,12 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 int add_file_to_index(struct index_state *istate, const char *path, int flags)
 {
 	struct stat st;
-	if (lstat(path, &st))
-		die_errno(_("unable to stat '%s'"), path);
+	if (lstat(path, &st)) {
+		if (flags & ADD_CACHE_IGNORE_ERRORS)
+			return -1;
+		else
+			die_errno(_("unable to stat '%s'"), path);
+	}
 	return add_to_index(istate, path, &st, flags);
 }
 
-- 
gitgitgadget

```
