added mvBak() to POSIX

- Rename to a corresponding backup file
  If the backup file already exists, attempt with "01" .. "99" suffix.

- git ignore indexed backup files too
This commit is contained in:
Mark Olesen 2009-07-20 11:04:48 +02:00
parent 359f14ae91
commit 8ff026095d
6 changed files with 142 additions and 12 deletions

13
.gitignore vendored
View File

@ -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

View File

@ -0,0 +1,3 @@
mvBakTest.C
EXE = $(FOAM_USER_APPBIN)/mvBakTest

View File

@ -0,0 +1,2 @@
/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */
/* EXE_LIBS = -lfiniteVolume */

View File

@ -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;
}
// ************************************************************************* //

View File

@ -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)
{

View File

@ -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