Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: 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: References: 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= Bytes: 3028 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"