Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Are you making all this up as you go along?


comp / comp.lang.c / Re: how copy file on linux?

SubjectAuthor
* how copy file on linux?Thiago Adams
+- Re: how copy file on linux?Janis Papanagnou
+* Re: how copy file on linux?Keith Thompson
|`- Re: how copy file on linux?Lew Pitcher
+* Re: how copy file on linux?Lawrence D'Oliveiro
|`* Re: how copy file on linux?Scott Lurndal
| `- Re: how copy file on linux?Bonita Montero
`* Re: how copy file on linux?Bonita Montero
 +- Re: how copy file on linux?Scott Lurndal
 `* Re: how copy file on linux?Malcolm McLean
  `* Re: how copy file on linux?Lew Pitcher
   `- Re: how copy file on linux?Lew Pitcher

1
Subject: how copy file on linux?
From: Thiago Adams
Newsgroups: comp.lang.c
Organization: A noiseless patient Spider
Date: Wed, 26 Jun 2024 20:44 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.adams@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: how copy file on linux?
Date: Wed, 26 Jun 2024 17:44:48 -0300
Organization: A noiseless patient Spider
Lines: 4
Message-ID: <v5huk1$2anbd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 26 Jun 2024 22:44:49 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="dbf16983e326375e449550695938eef6";
logging-data="2448749"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19x9EvkHqdhrQ7c+ciqnbyXLmUaE1Ajzz8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xQgxaFSfGf9PREhDquHS6Z2mf4U=
Content-Language: en-US
View all headers

How to copy a file on linux keeping the original file information?
timestamp etc?

Or in other words the equivalent of CopyFileA from windows.

Subject: Re: how copy file on linux?
From: Janis Papanagnou
Newsgroups: comp.lang.c
Organization: A noiseless patient Spider
Date: Wed, 26 Jun 2024 21:48 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Date: Wed, 26 Jun 2024 23:48:15 +0200
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <v5i2b0$2bd8m$1@dont-email.me>
References: <v5huk1$2anbd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 26 Jun 2024 23:48:16 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="9841f2839e540ec00f6dbca835a22b86";
logging-data="2471190"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+ONrxOg3qQchij+zwS0l1"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:WruF6fQDZ2THgcdydipvpgJNLnA=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <v5huk1$2anbd$1@dont-email.me>
View all headers

On 26.06.2024 22:44, Thiago Adams wrote:
> How to copy a file on linux keeping the original file information?
> timestamp etc?

This is an OS question and I'm not sure you want the shell command
or the library function.

On a Linux shell I do

cp -p # to preserve attributes
cp -a # incl. -p, to do a lot like copying recursive a directory

>
> Or in other words the equivalent of CopyFileA from windows.

And what exactly does it do? (Windows has - for example, since you
mentioned time stamps above - other time information than Unix
systems. So it may not be what you expect.)

Janis

Subject: Re: how copy file on linux?
From: Keith Thompson
Newsgroups: comp.lang.c
Organization: None to speak of
Date: Wed, 26 Jun 2024 22:35 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Date: Wed, 26 Jun 2024 15:35:00 -0700
Organization: None to speak of
Lines: 16
Message-ID: <87v81vs57v.fsf@nosuchdomain.example.com>
References: <v5huk1$2anbd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Thu, 27 Jun 2024 00:35:00 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2dc86e450632c25820fa91c1ec368267";
logging-data="2469342"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CjX7rSeZHucO2xW8ZE3ML"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:I/mU8oAKiV3S7nEG+FU32x3xX3Y=
sha1:aYdUGJSewR5G/6IGUlyu+l07eQc=
View all headers

Thiago Adams <thiago.adams@gmail.com> writes:
> How to copy a file on linux keeping the original file information?
> timestamp etc?
>
> Or in other words the equivalent of CopyFileA from windows.

comp.unix.programmer is likely to give you better answers. I don't
think POSIX defines any functions that copy files.

If I uunderstand correctly, you want to do the equivalent of "cp -p",
but from C rather than from a shell. You might consider using system(),
but that has some drawbacks, and there are probably better ways.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Subject: Re: how copy file on linux?
From: Lew Pitcher
Newsgroups: comp.lang.c
Followup: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Wed, 26 Jun 2024 23:24 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Followup-To: comp.unix.programmer
Date: Wed, 26 Jun 2024 23:24:47 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <v5i7vu$28id7$1@dont-email.me>
References: <v5huk1$2anbd$1@dont-email.me>
<87v81vs57v.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 27 Jun 2024 01:24:47 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="455a3cb0350599fb9065298926fa125c";
logging-data="2378151"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XTWfGfALLOd9/cOq9Y8v2mvtV0resU70="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:undLkcZ8MWEG+jmySlTh9NA6mWw=
View all headers

(Followup set to comp.unix.programmer)

On Wed, 26 Jun 2024 15:35:00 -0700, Keith Thompson wrote:

> Thiago Adams <thiago.adams@gmail.com> writes:
>> How to copy a file on linux keeping the original file information?
>> timestamp etc?
>>
>> Or in other words the equivalent of CopyFileA from windows.
>
> comp.unix.programmer is likely to give you better answers. I don't
> think POSIX defines any functions that copy files.

No, AFAIK, POSIX doesn't define an "all-in-one" file copy function.
However, Linux (the OS Thiago asks about) defines a number of them.

> If I uunderstand correctly, you want to do the equivalent of "cp -p",

Which /can/ be accomplished with POSIX calls
- the standard I/O functions (fopen()/open(), fread()/read(),
fwrite()/write(), fclose()/close() ) to copy the file data
- stat() to obtain the "original file information", and
- utime() to set the copy timestamps,
- chmod() to set the copy file permissions
- chown() to set the file ownership

But, perhaps Thiago would be satisfied with just a hardlinked file.

> but from C rather than from a shell. You might consider using system(),
> but that has some drawbacks, and there are probably better ways.

HTH
--
Lew Pitcher
"In Skills We Trust"

Subject: Re: how copy file on linux?
From: Lawrence D'Oliv
Newsgroups: comp.lang.c
Organization: A noiseless patient Spider
Date: Wed, 26 Jun 2024 23:43 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Date: Wed, 26 Jun 2024 23:43:04 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <v5i927$2cjv1$2@dont-email.me>
References: <v5huk1$2anbd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 27 Jun 2024 01:43:04 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="080a009ffce1dc4f5355e621231567e1";
logging-data="2510817"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/thHIrTcZ9iaBUBimx3c2p"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:O0u21jhee4Jh/w/+/cg7M9Xf1sE=
View all headers

On Wed, 26 Jun 2024 17:44:48 -0300, Thiago Adams wrote:

> How to copy a file on linux keeping the original file information?
> timestamp etc?

Last-modified is usually the only timestamp people care about.

Subject: Re: how copy file on linux?
From: Scott Lurndal
Newsgroups: comp.lang.c
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 27 Jun 2024 00:36 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!news.mixmin.net!news.neodome.net!npeer.as286.net!npeer-ng0.as286.net!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx36.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: how copy file on linux?
Newsgroups: comp.lang.c
References: <v5huk1$2anbd$1@dont-email.me> <v5i927$2cjv1$2@dont-email.me>
Lines: 11
Message-ID: <5u2fO.22271$7zcd.3336@fx36.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 27 Jun 2024 00:36:17 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 27 Jun 2024 00:36:17 GMT
X-Received-Bytes: 989
View all headers

Lawrence D'Oliveiro <ldo@nz.invalid> writes:
>On Wed, 26 Jun 2024 17:44:48 -0300, Thiago Adams wrote:
>
>> How to copy a file on linux keeping the original file information?
>> timestamp etc?
>
>Last-modified is usually the only timestamp people care about.

Do you think before you post?

That's not true.

Subject: Re: how copy file on linux?
From: Bonita Montero
Newsgroups: comp.lang.c
Organization: A noiseless patient Spider
Date: Fri, 28 Jun 2024 08:19 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Date: Fri, 28 Jun 2024 10:19:33 +0200
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <v5lrmj$38p28$1@raubtier-asyl.eternal-september.org>
References: <v5huk1$2anbd$1@dont-email.me> <v5i927$2cjv1$2@dont-email.me>
<5u2fO.22271$7zcd.3336@fx36.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 28 Jun 2024 10:19:31 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="f8388e8694381a01406bbb7a108535eb";
logging-data="3433544"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OwGVtrDMKdb0zf7M14/krNw3ACoII8yw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lHlBMOGjP1zYkhcgxCdI/wag1VQ=
Content-Language: de-DE
In-Reply-To: <5u2fO.22271$7zcd.3336@fx36.iad>
View all headers

Am 27.06.2024 um 02:36 schrieb Scott Lurndal:
> Lawrence D'Oliveiro <ldo@nz.invalid> writes:
>> On Wed, 26 Jun 2024 17:44:48 -0300, Thiago Adams wrote:
>>
>>> How to copy a file on linux keeping the original file information?
>>> timestamp etc?
>>
>> Last-modified is usually the only timestamp people care about.
>
> Do you think before you post?
>
> That's not true.

No, he forgot he posted that thousand times because he didn't
copy the creation time of is objection. ;-)

Subject: Re: how copy file on linux?
From: Bonita Montero
Newsgroups: comp.lang.c
Organization: A noiseless patient Spider
Date: Fri, 28 Jun 2024 18:15 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Date: Fri, 28 Jun 2024 20:15:20 +0200
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <v5mujo$3f10p$1@raubtier-asyl.eternal-september.org>
References: <v5huk1$2anbd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 28 Jun 2024 20:15:20 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="f8388e8694381a01406bbb7a108535eb";
logging-data="3638297"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188v2ZzXx1crq0slAPiVU3o2LHWarAiV6I="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Pc4xNjP9Q6UsKyL0itfxoVBs28A=
Content-Language: de-DE
In-Reply-To: <v5huk1$2anbd$1@dont-email.me>
View all headers

I tried to implement that in C++. Unfortunately an error branch of the
code is taken. What Do I have to change ?

bool CopyFileA( char const *from, char const *to, bool overwrite )
{ struct stat attrFrom, attrTo;
if( stat( from, &attrFrom ) )
return false;
if( !overwrite && !stat( to, &attrTo ) && errno != ENOENT )
return false;
union ndi { char c; };
vector<char> buf( attrFrom.st_size >= 0x100000 ? 0x100000 :
attrFrom.st_size );
int fromFile = open( from, O_RDONLY | O_NOATIME );
if( !fromFile )
return false;
invoke_on_destruct closeFrom( [&] { close( fromFile ); } );
int toFile = open( to, O_CREAT );
if( !toFile )
return false;
invoke_on_destruct closeTo( [&] { close( toFile ); } );
invoke_on_destruct delTo( [&] { unlink( to ); } );
for( int64_t remaining = attrFrom.st_size; remaining > 0; remaining -=
0x100000 )
{
size_t n = (size_t)(remaining >= 0x100000 ? 0x100000 : remaining);
buf.resize( n );
if( read( fromFile, buf.data(), n ) )
{
// this branch is taken
cout << strerror( errno ) << endl;
return false;
}
if( write( toFile, buf.data(), n ) )
return false;
}
closeTo();
utimbuf utb;
utb.actime = attrFrom.st_atime;
utb.modtime = attrFrom.st_mtime;
if( utime( to, &utb ) )
return false;
if( chmod( to, attrFrom.st_mode ) )
return false;
if( chown( to, attrFrom.st_uid, attrFrom.st_gid ) && errno != EACCES )
return false;
delTo.disable();
return to;
}

If everything works fine at last I'll make a C-rewrite.

Subject: Re: how copy file on linux?
From: Scott Lurndal
Newsgroups: comp.lang.c
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 28 Jun 2024 19:22 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!news.mb-net.net!open-news-network.org!news.mind.de!bolzen.all.de!npeer.as286.net!npeer-ng0.as286.net!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: how copy file on linux?
Newsgroups: comp.lang.c
References: <v5huk1$2anbd$1@dont-email.me> <v5mujo$3f10p$1@raubtier-asyl.eternal-september.org>
Lines: 9
Message-ID: <j4EfO.173370$7NFd.67630@fx16.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 28 Jun 2024 19:22:55 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 28 Jun 2024 19:22:55 GMT
X-Received-Bytes: 1007
View all headers

Bonita Montero <Bonita.Montero@gmail.com> writes:
>I tried to implement that in C++. Unfortunately an error branch of the
>code is taken. What Do I have to change ?

You need to figure out which error branch is taken and
look at errno and the surrounding logic at that point
to see why the call failed.

Subject: Re: how copy file on linux?
From: Malcolm McLean
Newsgroups: comp.lang.c
Organization: A noiseless patient Spider
Date: Fri, 28 Jun 2024 23:04 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm.arthur.mclean@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Date: Sat, 29 Jun 2024 00:04:00 +0100
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <v5nfh0$3i4n8$1@dont-email.me>
References: <v5huk1$2anbd$1@dont-email.me>
<v5mujo$3f10p$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 29 Jun 2024 01:04:00 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="15fbe87ffc9d5dbfd6651f6740cf6176";
logging-data="3740392"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tAaagx0nBzw6yWV1hxaSMNIieX83rI/s="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kJ4UXOedcr/oTpOyEOmLGAiPolU=
In-Reply-To: <v5mujo$3f10p$1@raubtier-asyl.eternal-september.org>
Content-Language: en-GB
View all headers

On 28/06/2024 19:15, Bonita Montero wrote:
> int fromFile = open( from, O_RDONLY | O_NOATIME );
>     if( !fromFile )
>         return false;
>    invoke_on_destruct closeFrom( [&] { close( fromFile ); } );
> //    int toFile = open( to, O_CREAT );
> //    if( !toFile )
> //        return false;
>     invoke_on_destruct closeTo( [&] { close( toFile ); } );
> //     invoke_on_destruct delTo( [&] { unlink( to ); } );
>     for( int64_t remaining = attrFrom.st_size; remaining > 0; remaining
> -= 0x100000 )
>     {
>         size_t n = (size_t)(remaining >= 0x100000 ? 0x100000 : remaining);
>         buf.resize( n );
>         if( read( fromFile, buf.data(), n ) )
>         {
>             // this branch is taken
>             cout << strerror( errno ) << endl;
>             return false;
>         }

So it claims the file opens, then won't read it?

Check the call. Is 0 the success return?

Then I must admit I'm stumped. "to" doesn't alias "from" I suppose?

I'd also wonder if somehow the C++ compiler has invoked the on destruct
code, and closed the file.

--
Check out my hobby project.
http://malcolmmclean.github.io/babyxrc

Subject: Re: how copy file on linux?
From: Lew Pitcher
Newsgroups: comp.lang.c
Organization: A noiseless patient Spider
Date: Fri, 28 Jun 2024 23:25 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Date: Fri, 28 Jun 2024 23:25:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <v5ngp0$3ehss$2@dont-email.me>
References: <v5huk1$2anbd$1@dont-email.me>
<v5mujo$3f10p$1@raubtier-asyl.eternal-september.org>
<v5nfh0$3i4n8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 29 Jun 2024 01:25:21 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a6c2d2db8ef704f1aabece0e415918ba";
logging-data="3622812"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+K2Ic1NvtBg/yNXBDWyIfyAV5SnOz+omo="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:VYAxrykvzE/vBRcQEzP5CAi1dRU=
View all headers

On Sat, 29 Jun 2024 00:04:00 +0100, Malcolm McLean wrote:

> On 28/06/2024 19:15, Bonita Montero wrote:
>> int fromFile = open( from, O_RDONLY | O_NOATIME );
>>     if( !fromFile )
>>         return false;

open() returns -1 on failure, and some non-negative number
on success. That non-negative number /can be/ 0 and still
be a valid success value.

The above test /will not/ detect an open() failure, as !(-1) == 0
However, the above /will/ falsely call a valid open() that returns
fd 0 (slim chance unless you've previously closed stdin).

>>    invoke_on_destruct closeFrom( [&] { close( fromFile ); } );
>> //    int toFile = open( to, O_CREAT );
>> //    if( !toFile )
>> //        return false;
>>     invoke_on_destruct closeTo( [&] { close( toFile ); } );
>> //     invoke_on_destruct delTo( [&] { unlink( to ); } );
>>     for( int64_t remaining = attrFrom.st_size; remaining > 0; remaining
>> -= 0x100000 )
>>     {
>>         size_t n = (size_t)(remaining >= 0x100000 ? 0x100000 : remaining);
>>         buf.resize( n );
>>         if( read( fromFile, buf.data(), n ) )
>>         {
>>             // this branch is taken
>>             cout << strerror( errno ) << endl;
>>             return false;
>>         }
>
> So it claims the file opens, then won't read it?
>
> Check the call. Is 0 the success return?
>
> Then I must admit I'm stumped. "to" doesn't alias "from" I suppose?
>
> I'd also wonder if somehow the C++ compiler has invoked the on destruct
> code, and closed the file.

--
Lew Pitcher
"In Skills We Trust"

Subject: Re: how copy file on linux?
From: Lew Pitcher
Newsgroups: comp.lang.c
Organization: A noiseless patient Spider
Date: Fri, 28 Jun 2024 23:27 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: how copy file on linux?
Date: Fri, 28 Jun 2024 23:27:49 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <v5ngtl$3ehss$3@dont-email.me>
References: <v5huk1$2anbd$1@dont-email.me>
<v5mujo$3f10p$1@raubtier-asyl.eternal-september.org>
<v5nfh0$3i4n8$1@dont-email.me> <v5ngp0$3ehss$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 29 Jun 2024 01:27:50 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a6c2d2db8ef704f1aabece0e415918ba";
logging-data="3622812"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/7minrkKleuYxPcScNfTUvr5/3pzmmQYA="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:P20CY+Aos5EQGVGKRMHN2YBBASw=
View all headers

Oops. missed part of my edit. See unquoted line below

On Fri, 28 Jun 2024 23:25:20 +0000, Lew Pitcher wrote:

> On Sat, 29 Jun 2024 00:04:00 +0100, Malcolm McLean wrote:
>
>> On 28/06/2024 19:15, Bonita Montero wrote:
>>> int fromFile = open( from, O_RDONLY | O_NOATIME );
>>>     if( !fromFile )
>>>         return false;
>
> open() returns -1 on failure, and some non-negative number
> on success. That non-negative number /can be/ 0 and still
> be a valid success value.
>
> The above test /will not/ detect an open() failure, as !(-1) == 0
> However, the above /will/ falsely call
a FAILURE (and return a "false" from the function)
> a valid open() that returns
> fd 0 (slim chance unless you've previously closed stdin).
>
>
>>>    invoke_on_destruct closeFrom( [&] { close( fromFile ); } );
>>> //    int toFile = open( to, O_CREAT );
>>> //    if( !toFile )
>>> //        return false;
>>>     invoke_on_destruct closeTo( [&] { close( toFile ); } );
>>> //     invoke_on_destruct delTo( [&] { unlink( to ); } );
>>>     for( int64_t remaining = attrFrom.st_size; remaining > 0; remaining
>>> -= 0x100000 )
>>>     {
>>>         size_t n = (size_t)(remaining >= 0x100000 ? 0x100000 : remaining);
>>>         buf.resize( n );
>>>         if( read( fromFile, buf.data(), n ) )
>>>         {
>>>             // this branch is taken
>>>             cout << strerror( errno ) << endl;
>>>             return false;
>>>         }
>>
>> So it claims the file opens, then won't read it?
>>
>> Check the call. Is 0 the success return?
>>
>> Then I must admit I'm stumped. "to" doesn't alias "from" I suppose?
>>
>> I'd also wonder if somehow the C++ compiler has invoked the on destruct
>> code, and closed the file.

--
Lew Pitcher
"In Skills We Trust"

1

rocksolid light 0.9.8
clearnet tor