1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
//# Directory.cc: Class to define a Directory
//# Copyright (C) 1996,1997,1999,2000,2001,2002
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library 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 Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: aips2-request@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
//#
//# $Id$
// we NEED to include aips(env).h before using any AIPS_xyz defines
#include <casacore/casa/OS/Directory.h>
#if defined(AIPS_SOLARIS) || defined(AIPS_OSF)
# if defined(AIPS_OSF)
extern "C" { // missing in system include file
# endif
# if defined(AIPS_SOLARIS)
# undef _FILE_OFFSET_BITS
# undef _LARGEFILE_SOURCE
# endif
# include <sys/statvfs.h> // needed for statvfs
# if defined(AIPS_OSF)
}
# endif
# define statfs statvfs
#elif defined(AIPS_DARWIN) || defined(AIPS_BSD)
#include <sys/param.h>
#include <sys/mount.h>
#else
# include <sys/vfs.h>
# if defined(AIPS_IRIX)
# include <sys/statfs.h>
# define f_bavail f_bfree
# endif
#endif
#include <casacore/casa/OS/DirectoryIterator.h>
#include <casacore/casa/OS/RegularFile.h>
#include <casacore/casa/OS/SymLink.h>
#include <casacore/casa/Arrays/Vector.h>
#include <casacore/casa/Arrays/Slice.h>
#include <casacore/casa/Arrays/ArrayMath.h>
#include <casacore/casa/Exceptions/Error.h>
#include <casacore/casa/stdexcept.h>
#include <unistd.h> // needed for rmdir, unlink
#include <sys/stat.h> // needed for mkdir
#include <errno.h> // needed for errno
#include <casacore/casa/string.h> // needed for strerror
// Shouldn't be needed, but is needed to get rename under linux. The
// man page claims it's in unistd.h.
#include <casacore/casa/stdio.h>
namespace casacore { //# NAMESPACE CASACORE - BEGIN
Directory::Directory()
: File() // sets to working directory
{}
Directory::Directory (const Path& name)
: File(name)
{
checkPath();
}
Directory::Directory (const String& name)
: File(name)
{
checkPath();
}
Directory::Directory (const File& name)
: File(name)
{
checkPath();
}
Directory::Directory (const Directory& directory)
: File (directory),
itsFile (directory.itsFile)
{}
Directory::~Directory()
{}
Directory& Directory::operator= (const Directory& directory)
{
if (this != &directory) {
File::operator= (directory);
itsFile = directory.itsFile;
}
return *this;
}
void Directory::checkPath()
{
itsFile = *this;
// If exists, check if it is a directory.
// If the file is a symlink, resolve the entire symlink chain.
// Otherwise check if it can be created.
if (exists()) {
if (isSymLink()) {
itsFile = SymLink(*this).followSymLink();
// Error if no directory and exists or cannot be created.
if (!itsFile.isDirectory()) {
if (itsFile.exists() || !itsFile.canCreate()) {
throw (AipsError ("Directory: " + path().expandedName() +
" is a symbolic link not"
" pointing to a valid directory"));
}
}
} else if (!isDirectory()) {
throw (AipsError ("Directory: " + path().expandedName() +
" exists, but is no directory"));
}
} else {
if (!canCreate()) {
throw (AipsError ("Directory: " + path().expandedName() +
" does not exist and cannot be created"));
}
}
}
uInt Directory::nEntries() const
{
uInt nentries = 0;
DirectoryIterator iter(*this);
while (! iter.pastEnd()) {
nentries++;
iter++;
}
return nentries;
}
Bool Directory::isEmpty() const
{
DirectoryIterator iter(*this);
while (! iter.pastEnd()) {
String nm (iter.name());
if (nm.size() < 5 || nm.before(4) != ".nfs") {
/// cout <<"iter at "<<iter.name()<<endl;
return False;
}
iter++;
}
return True;
}
Double Directory::freeSpace() const
{
#if defined(AIPS_CRAY_PGI)
return 1e37;
#else
struct statfs buf;
#if defined(AIPS_IRIX)
if (statfs (itsFile.path().expandedName().chars(),
&buf, sizeof(buf), 0) < 0) {
#else
if (statfs (itsFile.path().expandedName().chars(), &buf) < 0) {
#endif
throw (AipsError ("Directory::freeSpace error on " +
itsFile.path().expandedName() +
": " + strerror(errno)));
}
Double bsize = buf.f_bsize;
#if defined(AIPS_SOLARIS) || defined(AIPS_OSF)
//# The fragment size usually contains the true block size.
if (buf.f_frsize > 0) {
bsize = buf.f_frsize;
}
#endif
return bsize * buf.f_bavail;
#endif
}
void Directory::create (Bool overwrite)
{
// If overwrite is False the directory will not be overwritten.
if (exists()) {
if (!itsFile.isDirectory()) {
throw (AipsError ("Directory::create: " +
itsFile.path().expandedName() +
" already exists as a non-directory"));
}
if (!overwrite) {
throw (AipsError ("Directory::create: " +
itsFile.path().expandedName() +
" already exists"));
}
// Keep the directory, so special allocation on Lustre is preserved.
Directory(itsFile).removeRecursive(True);
} else {
if (mkdir (itsFile.path().expandedName().chars(), 0777) < 0) {
throw (AipsError ("Directory::create error on " +
itsFile.path().expandedName() +
": " + strerror(errno)));
}
}
}
void Directory::remove()
{
// If the directory is not empty it cannot be removed.
if (! isEmpty()) {
throw (AipsError ("Directory::remove: " + path().expandedName() +
" is not empty"));
}
if (isSymLink()) {
removeSymLinks();
}
rmdir (itsFile.path().absoluteName().chars());
}
void Directory::removeFiles()
{
DirectoryIterator iter(*this);
while (! iter.pastEnd()) {
File file = iter.file();
if (! file.isDirectory (False)) {
unlink (file.path().originalName().chars());
}
iter++;
}
}
void Directory::removeRecursive (Bool keepDir)
{
DirectoryIterator iter(*this);
while (! iter.pastEnd()) {
File file = iter.file();
if (file.isDirectory (False)) {
Directory(file).removeRecursive();
} else {
unlink (file.path().originalName().chars());
}
iter++;
}
if (!keepDir) {
remove();
}
}
Int64 Directory::size() const
{
Int64 totSize=0;
DirectoryIterator iter(*this);
while (! iter.pastEnd()) {
File file = iter.file();
if (file.isDirectory ()) {
totSize+=Directory(file).size();
} else {
totSize+=file.size();
}
iter++;
}
return totSize;
}
void Directory::copy (const Path& target, Bool overwrite,
Bool setUserWritePermission) const
{
Path targetName(target);
checkTarget (targetName, overwrite, True);
// Remove the target if it already exists.
File targetFile(targetName);
if (targetFile.isRegular (False)) {
RegularFile(targetFile).remove();
} else if (targetFile.isDirectory (False)) {
Directory(targetFile).removeRecursive();
} else {
SymLink(targetFile).remove();
}
// Copy the entire directory recursively using the system function cp.
#if defined(AIPS_CRAY_PGI)
// On the Cray XT3 the system call is not supported, so we have to
// do it ourselves.
copyRecursive (targetName.expandedName());
#else
String command("cp -r '");
command += itsFile.path().expandedName() + "' '" +
targetName.expandedName() + "'";
AlwaysAssert (system(command.chars()) == 0, AipsError);
// Give write permission to user if needed.
if (setUserWritePermission) {
#if defined(__hpux__) || defined(AIPS_IRIX)
command = "chmod -R u+w '";
#else
command = "chmod -Rf u+w '";
#endif
command += targetName.expandedName() + "'";
AlwaysAssert (system(command.chars()) == 0, AipsError);
}
#endif
}
void Directory::copyRecursive (const String& target) const
{
// First create the directory.
Directory dir(target);
dir.create (True);
// Now loop over all files and copy.
DirectoryIterator iter(*this);
while (! iter.pastEnd()) {
File file = iter.file();
String outName = target + '/' + file.path().baseName();
if (file.isDirectory (False)) {
Directory(file).copyRecursive (outName);
} else {
RegularFile::manualCopy (file.path().originalName(), outName);
}
iter++;
}
}
void Directory::move (const Path& target, Bool overwrite)
{
Path targetPath(target);
checkTarget (targetPath, overwrite, True);
// Start trying to rename.
// If source and target are the same directory, rename does nothing
// and returns a success status.
if (rename (path().expandedName().chars(),
targetPath.expandedName().chars()) == 0) {
return;
}
// The rename failed for one reason or another.
// Remove the target if it already exists.
Bool alrExist = False;
if (errno == EEXIST) {
alrExist = True;
}
#if defined(ENOTEMPTY)
if (errno == ENOTEMPTY) {
alrExist = True;
}
#endif
#if defined(EBUSY)
if (errno == EBUSY) {
alrExist = True;
}
#endif
if (alrExist) {
Directory(targetPath).removeRecursive();
} else if (errno == ENOTDIR) {
unlink (targetPath.expandedName().chars());
}
// Try again.
if (rename (path().expandedName().chars(),
targetPath.expandedName().chars()) == 0) {
return;
}
// Throw an exception if not "different file systems" error.
if (errno != EXDEV) {
throw (AipsError ("Directory::move error on " +
path().expandedName() + " to " +
targetPath.expandedName() +
": " + strerror(errno)));
}
// Copy the directory and remove it thereafter.
copy (targetPath, overwrite, False);
removeRecursive();
}
// Vector<String> Directory::find (const String& fileName,
// Bool followSymLinks) const
// {
// //#// return find ('^' + Regex::fromString (fileName) + '$', followSymLinks);
// return find (Regex (Regex::fromString (fileName)), followSymLinks);
// }
Vector<String> Directory::find (const Regex& regexp, Bool followSymLinks,
Bool recursive) const
{
DirectoryIterator iter(*this);
Vector<String> myentries(10);
uInt count=0;
while (!iter.pastEnd()) {
//#// if (iter.name().contains (regexp)) {
if (iter.name().matches (regexp)) {
if (count + 1 >= myentries.nelements()) {
// More entries have been added - have to resize
myentries.resize (2*myentries.nelements(), True);
}
myentries(count) = iter.name();
count++;
}
iter++;
}
myentries.resize (count, True); // Trim the trailing entries
// Now recursively add all the ones we find in subdirectories, prepending
// the pathname.
if (recursive) {
iter.reset();
while (!iter.pastEnd()) {
File file = iter.file();
if (file.isDirectory (followSymLinks)) {
Directory subdir = file;
Vector<String> subentries = subdir.find (regexp);
String basename = iter.name() + "/";
subentries = basename + subentries;
uInt oldsize = myentries.nelements();
myentries.resize (oldsize + subentries.nelements(), True);
myentries(Slice(oldsize, subentries.nelements())) = subentries;
}
iter++;
}
}
return myentries;
}
Vector<String> Directory::shellExpand (const Vector<String>& files, Bool stripPath)
//
// Take a list of potentially wild-carded file names, and expand
// them into (optional) absolute path and name. Some more development
// needed to expand the path as well, if there are any wild cards in that.
//
{
Vector<String> expInNames;
uInt nExpInNames = 0;
uInt k = 0;
Regex exp;
//
for (uInt i=0; i<files.nelements(); i++) {
// Find the directory of this file.
Path path(files(i));
Directory dir(path.dirName());
// Try and expand it (minus path)
try {
exp = Regex::fromPattern(path.baseName());
} catch (invalid_argument& x) {
String msg = String("Error parsing file pattern ") + files(i);
throw (AipsError(msg));
}
// Find all the matched files
Vector<String> expFiles = dir.find(exp, True, False);
nExpInNames += expFiles.nelements();
expInNames.resize(nExpInNames, True);
// Add the path back on to each name
if (stripPath) {
for (uInt j=0; j<expFiles.nelements(); j++) {
expInNames(k) = expFiles(j);
k++;
}
} else {
for (uInt j=0; j<expFiles.nelements(); j++) {
expInNames(k) = Path::addDirectory("./"+expFiles(j), path.absoluteName());
k++;
}
}
}
// Return result
return expInNames;
}
#if defined(__linux__)
#include <sys/vfs.h>
#include <linux/nfs_fs.h>
#elif defined( __APPLE__)
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#elif defined(__FreeBSD_kernel__)
#include <sys/mount.h>
#include <sys/param.h>
#include <stdlib.h>
#endif
Bool Directory::isNFSMounted() const
{
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__)
struct statfs buf;
if (statfs (itsFile.path().expandedName().chars(), &buf) < 0) {
throw (AipsError ("Directory::isNFSMounted error on " +
itsFile.path().expandedName() +
": " + strerror(errno)));
}
#endif
#if defined(__linux__)
return buf.f_type == NFS_SUPER_MAGIC;
#elif defined(__APPLE__)
return buf.f_type == VT_NFS;
#elif defined(__FreeBSD_kernel__)
return strcmp (buf.f_fstypename, "nfs") == 0;
#else
return False;
#endif
}
} //# NAMESPACE CASACORE - END
|