View Issue Details

IDProjectCategoryView StatusLast Update
0003772OpenFOAMBugpublic2021-12-28 15:17
Reporterniklas.wikstrom Assigned Tohenry  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Platformx86_64OSfedoraOS Version35
Product Versiondev 
Fixed in Versiondev 
Summary0003772: Foam::cp() (POSIX) always returns false for file copy
DescriptionWhen using Foam::cp() to copy a file, the function always returns false. This appears to be a wrong behaviour, since it makes it more difficult to assert success.

The error might(!) be that std::ifstream::rdbuf() does not set the eof() flag: srcStream.eof() returns 0 both before and after the << srcStream.rdbuf()
Steps To ReproduceBuild and run the attached application

# ./testFiles someFile copiedFile
Additional InformationAlso tested on the current docker image release.
TagsNo tags attached.

Activities

niklas.wikstrom

2021-12-28 12:09

reporter  

testFiles.tgz (1,101 bytes)

henry

2021-12-28 13:18

manager   ~0012351

What do you propose to resolve this issue?

henry

2021-12-28 14:23

manager   ~0012352

I looked into the options for error handling and there is nothing obvious with is system independent. When we move to C++17 we will replace the current implementation of cp with filesystem::copy_file which provides error handling but in the meantime I think the best we can do is remove the EOF test.

niklas.wikstrom

2021-12-28 14:31

reporter   ~0012353

The attached variant is probably naive, but it is at least faster (and it can be "eof checked").
I suppose one can remove the check altoghether, since it is not used in recursive cp (same function) anyway.
This is ofcourse a tiny issue, but I wanted to report it, since I stumbled on it for a while.

Happy new year!
testFiles.C (2,237 bytes)   
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2019 EQUA Simulation AB
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Application
    testFiles

Description

\*---------------------------------------------------------------------------*/

#include "argList.H"
#include "OSspecific.H"
#include "clockTime.H"
#include <fstream>

using namespace Foam;

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//  Main program:

int main(int argc, char *argv[])
{
    argList::validArgs.insert("file1");
    argList::validArgs.insert("file2");
    argList args(argc, argv, false, true);

    const fileName file1 = args[1];
    const fileName file2("out1");
    const fileName file3("out2");

    clockTime T;
    T.timeIncrement();

    Foam::cp(file1,file2);

    Info << "Foam::cp elapsed time = " << T.timeIncrement() << endl;

    std::ifstream f1(file1);
    std::ofstream f3(file3);

    const size_t L = 256;
    char* buffer = new char[L];
    while (!f1.eof()) {
  	f1.read(buffer, L);
  	f3.write(buffer, f1.gcount());
    }
    Info << "Buffered rd/wt elapsed time = " << T.timeIncrement() << endl;
    Info << "after rdbuf eof = " << f1.eof() << endl;

    return 0;
}


// ************************************************************************* //
testFiles.C (2,237 bytes)   

henry

2021-12-28 15:17

manager   ~0012354

Resolved by commit 9f6e8e5effac9804af850bbb67bef660713d9c40

Issue History

Date Modified Username Field Change
2021-12-28 12:09 niklas.wikstrom New Issue
2021-12-28 12:09 niklas.wikstrom File Added: testFiles.tgz
2021-12-28 13:18 henry Note Added: 0012351
2021-12-28 14:23 henry Note Added: 0012352
2021-12-28 14:31 niklas.wikstrom File Added: testFiles.C
2021-12-28 14:31 niklas.wikstrom Note Added: 0012353
2021-12-28 15:17 henry Assigned To => henry
2021-12-28 15:17 henry Status new => resolved
2021-12-28 15:17 henry Resolution open => fixed
2021-12-28 15:17 henry Fixed in Version => dev
2021-12-28 15:17 henry Note Added: 0012354