diff --git a/.gitignore b/.gitignore index 032fac43eb..bcbd35cfdf 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,13 @@ # editor and misc backup files - anywhere *~ .*~ -*.orig *.bak +*.bak[0-9][0-9] +*.orig +*.orig[0-9][0-9] \#*\# + +# file-browser settings - anywhere .directory # CVS recovered versions - anywhere @@ -18,7 +22,7 @@ *.so *.jar -# ignore derived files +# derived files lex.yy.c # Corefiles @@ -30,7 +34,7 @@ core # lnInclude (symlink) folders - anywhere lnInclude -# build folder(s) - anywhere +# build folders - anywhere linux*Gcc*/ linux*Icc*/ linuxming*/ @@ -38,9 +42,6 @@ SiCortex*Gcc*/ solaris*Gcc*/ SunOS*Gcc*/ -# all of the wmake wmkdep and dirToString binaries -wmake/bin - # doxygen generated documentation doc/[Dd]oxygen/html doc/[Dd]oxygen/latex diff --git a/applications/test/mvBak/Make/files b/applications/test/mvBak/Make/files new file mode 100644 index 0000000000..6cf0b98c5b --- /dev/null +++ b/applications/test/mvBak/Make/files @@ -0,0 +1,3 @@ +mvBakTest.C + +EXE = $(FOAM_USER_APPBIN)/mvBakTest diff --git a/applications/test/mvBak/Make/options b/applications/test/mvBak/Make/options new file mode 100644 index 0000000000..6a9e9810b3 --- /dev/null +++ b/applications/test/mvBak/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */ +/* EXE_LIBS = -lfiniteVolume */ diff --git a/applications/test/mvBak/mvBakTest.C b/applications/test/mvBak/mvBakTest.C new file mode 100644 index 0000000000..6580ed421c --- /dev/null +++ b/applications/test/mvBak/mvBakTest.C @@ -0,0 +1,81 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. + \\/ 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 2 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, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Description + +\*---------------------------------------------------------------------------*/ + +#include "OSspecific.H" +#include "argList.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noParallel(); + argList::validArgs.insert("file .. fileN"); + argList::validOptions.erase("case"); + argList::validOptions.insert("ext", "bak"); + + argList args(argc, argv, false, true); + + if (args.additionalArgs().empty()) + { + args.printUsage(); + } + + label ok = 0; + + forAll(args.additionalArgs(), argI) + { + const string& srcFile = args.additionalArgs()[argI]; + + if (args.optionFound("ext")) + { + if (mvBak(srcFile, args.option("ext"))) + { + ok++; + } + } + else + { + if (mvBak(srcFile)) + { + ok++; + } + } + } + + Info<< "mvBak called for " << args.additionalArgs().size() + << " files (moved " << ok << ")\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 8496225b29..3f64d413c3 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -781,6 +781,45 @@ bool Foam::mv(const fileName& src, const fileName& dst) } +//- Rename to a corresponding backup file +// If the backup file already exists, attempt with "01" .. "99" index +bool Foam::mvBak(const fileName& src, const std::string& ext) +{ + if (POSIX::debug) + { + Info<< "mvBak : " << src << " to extension " << ext << endl; + } + + if (exists(src, false)) + { + const int maxIndex = 99; + char index[3]; + + for (int n = 0; n <= maxIndex; n++) + { + fileName dstName(src + "." + ext); + if (n) + { + sprintf(index, "%02d", n); + dstName += index; + } + + // avoid overwriting existing files, except for the last + // possible index where we have no choice + if (!exists(dstName, false) || n == maxIndex) + { + return rename(src.c_str(), dstName.c_str()) == 0; + } + + } + } + + // fall-through: nothing to do + return false; +} + + + // Remove a file, returning true if successful otherwise false bool Foam::rm(const fileName& file) { diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 946cf0f86d..b34d374d72 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -144,15 +144,19 @@ fileNameList readDir ); //- Copy, recursively if necessary, the source to the destination -bool cp(const fileName& srcFile, const fileName& destFile); +bool cp(const fileName& src, const fileName& dst); -//- Create a softlink. destFile should not exist. Returns true if successful. -bool ln(const fileName& srcFile, const fileName& destFile); +//- Create a softlink. dst should not exist. Returns true if successful. +bool ln(const fileName& src, const fileName& dst); -//- Rename srcFile destFile -bool mv(const fileName& srcFile, const fileName& destFile); +//- Rename src to dst +bool mv(const fileName& src, const fileName& dst); -//- Remove a file returning true if successful otherwise false +//- Rename to a corresponding backup file +// If the backup file already exists, attempt with "01" .. "99" suffix +bool mvBak(const fileName&, const std::string& ext = "bak"); + +//- Remove a file, returning true if successful otherwise false bool rm(const fileName&); //- Remove a dirctory and its contents