2003-08-01 20:28:55 +00:00
|
|
|
//===- Support/FileUtilities.h - File System Utilities ----------*- C++ -*-===//
|
2003-10-20 19:46:57 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-01 20:28:55 +00:00
|
|
|
//
|
|
|
|
// This file defines a family of utility functions which are useful for doing
|
|
|
|
// various things with files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef SUPPORT_FILEUTILITIES_H
|
|
|
|
#define SUPPORT_FILEUTILITIES_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2003-11-11 21:53:29 +00:00
|
|
|
/// CheckMagic - Returns true IFF the file named FN begins with Magic. FN must
|
|
|
|
/// name a readable file.
|
|
|
|
///
|
|
|
|
bool CheckMagic (const std::string &FN, const std::string &Magic);
|
|
|
|
|
|
|
|
/// IsArchive - Returns true IFF the file named FN appears to be a "ar" library
|
|
|
|
/// archive. The file named FN must exist.
|
|
|
|
///
|
|
|
|
bool IsArchive (const std::string &FN);
|
|
|
|
|
|
|
|
/// IsBytecode - Returns true IFF the file named FN appears to be an LLVM
|
|
|
|
/// bytecode file. The file named FN must exist.
|
|
|
|
///
|
|
|
|
bool IsBytecode (const std::string &FN);
|
2003-11-24 05:28:39 +00:00
|
|
|
|
|
|
|
/// IsSharedObject - Returns trus IFF the file named FN appears to be a shared
|
|
|
|
/// object with an ELF header. The file named FN must exist.
|
|
|
|
///
|
|
|
|
bool IsSharedObject(const std::string &FN);
|
2003-11-11 21:53:29 +00:00
|
|
|
|
|
|
|
/// FileOpenable - Returns true IFF Filename names an existing regular file
|
|
|
|
/// which we can successfully open.
|
2003-11-11 18:27:11 +00:00
|
|
|
///
|
|
|
|
bool FileOpenable (const std::string &Filename);
|
|
|
|
|
2003-08-01 20:28:55 +00:00
|
|
|
/// DiffFiles - Compare the two files specified, returning true if they are
|
|
|
|
/// different or if there is a file error. If you specify a string to fill in
|
|
|
|
/// for the error option, it will set the string to an error message if an error
|
|
|
|
/// occurs, allowing the caller to distinguish between a failed diff and a file
|
|
|
|
/// system error.
|
|
|
|
///
|
|
|
|
bool DiffFiles(const std::string &FileA, const std::string &FileB,
|
|
|
|
std::string *Error = 0);
|
|
|
|
|
|
|
|
|
|
|
|
/// MoveFileOverIfUpdated - If the file specified by New is different than Old,
|
|
|
|
/// or if Old does not exist, move the New file over the Old file. Otherwise,
|
|
|
|
/// remove the New file.
|
|
|
|
///
|
|
|
|
void MoveFileOverIfUpdated(const std::string &New, const std::string &Old);
|
|
|
|
|
2003-08-07 21:28:50 +00:00
|
|
|
/// removeFile - Delete the specified file
|
|
|
|
///
|
|
|
|
void removeFile(const std::string &Filename);
|
|
|
|
|
|
|
|
/// getUniqueFilename - Return a filename with the specified prefix. If the
|
|
|
|
/// file does not exist yet, return it, otherwise add a suffix to make it
|
|
|
|
/// unique.
|
|
|
|
///
|
|
|
|
std::string getUniqueFilename(const std::string &FilenameBase);
|
|
|
|
|
2003-12-30 07:35:47 +00:00
|
|
|
/// MakeFileExecutable - This method turns on whatever access attributes are
|
|
|
|
/// needed to make the specified file executable. It returns true on success.
|
|
|
|
/// In case of failure, the file's access attributes are unspecified.
|
2003-09-02 20:14:57 +00:00
|
|
|
///
|
2003-12-30 07:35:47 +00:00
|
|
|
bool MakeFileExecutable(const std::string &Filename);
|
2003-09-02 21:09:30 +00:00
|
|
|
|
2003-12-30 07:35:47 +00:00
|
|
|
/// MakeFileReadable - This method turns on whatever access attributes are
|
|
|
|
/// needed to make the specified file readable. It returns true on success.
|
|
|
|
/// In case of failure, the file's access attributes are unspecified.
|
2003-09-02 21:09:30 +00:00
|
|
|
///
|
2003-12-30 07:35:47 +00:00
|
|
|
bool MakeFileReadable(const std::string &Filename);
|
|
|
|
|
|
|
|
/// getFileSize - Return the size of the specified file in bytes, or -1 if the
|
|
|
|
/// file cannot be read or does not exist.
|
|
|
|
long long getFileSize(const std::string &Filename);
|
|
|
|
|
2003-09-02 21:09:30 +00:00
|
|
|
|
2003-12-31 06:16:02 +00:00
|
|
|
/// getFileTimestamp - Get the last modified time for the specified file in an
|
|
|
|
/// unspecified format. This is useful to allow checking to see if a file was
|
|
|
|
/// updated since that last time the timestampt was aquired. If the file does
|
|
|
|
/// not exist or there is an error getting the time-stamp, zero is returned.
|
|
|
|
unsigned long long getFileTimestamp(const std::string &Filename);
|
|
|
|
|
2003-12-29 21:35:05 +00:00
|
|
|
|
|
|
|
/// FDHandle - Simple handle class to make sure a file descriptor gets closed
|
2003-12-30 02:45:16 +00:00
|
|
|
/// when the object is destroyed. This handle acts similarly to an
|
|
|
|
/// std::auto_ptr, in that the copy constructor and assignment operators
|
|
|
|
/// transfer ownership of the handle. This means that FDHandle's do not have
|
|
|
|
/// value semantics.
|
2003-12-29 21:35:05 +00:00
|
|
|
///
|
|
|
|
class FDHandle {
|
|
|
|
int FD;
|
|
|
|
public:
|
|
|
|
FDHandle() : FD(-1) {}
|
|
|
|
FDHandle(int fd) : FD(fd) {}
|
2003-12-30 02:45:16 +00:00
|
|
|
FDHandle(FDHandle &RHS) : FD(RHS.FD) {
|
|
|
|
RHS.FD = -1; // Transfer ownership
|
|
|
|
}
|
|
|
|
|
2003-12-29 21:43:58 +00:00
|
|
|
~FDHandle() throw();
|
2003-12-29 21:35:05 +00:00
|
|
|
|
2003-12-30 02:45:16 +00:00
|
|
|
/// get - Get the current file descriptor, without releasing ownership of it.
|
|
|
|
int get() const { return FD; }
|
2003-12-29 21:35:05 +00:00
|
|
|
operator int() const { return FD; }
|
|
|
|
|
2003-12-29 21:43:58 +00:00
|
|
|
FDHandle &operator=(int fd) throw();
|
2003-12-29 21:35:05 +00:00
|
|
|
|
2003-12-30 02:45:16 +00:00
|
|
|
FDHandle &operator=(FDHandle &RHS) {
|
|
|
|
int fd = RHS.FD;
|
|
|
|
RHS.FD = -1; // Transfer ownership
|
|
|
|
return operator=(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// release - Take ownership of the file descriptor away from the FDHandle
|
2003-12-29 21:35:05 +00:00
|
|
|
/// object, so that the file is not closed when the FDHandle is destroyed.
|
2003-12-30 02:45:16 +00:00
|
|
|
int release() {
|
|
|
|
int Ret = FD;
|
|
|
|
FD = -1;
|
|
|
|
return Ret;
|
|
|
|
}
|
2003-12-29 21:35:05 +00:00
|
|
|
};
|
2004-02-18 17:16:17 +00:00
|
|
|
|
|
|
|
/// FileRemover - This class is a simple object meant to be stack allocated.
|
|
|
|
/// If an exception is thrown from a region, the object removes the filename
|
|
|
|
/// specified (if deleteIt is true).
|
|
|
|
///
|
|
|
|
class FileRemover {
|
|
|
|
std::string Filename;
|
2004-02-18 20:20:52 +00:00
|
|
|
bool DeleteIt;
|
2004-02-18 17:16:17 +00:00
|
|
|
public:
|
2004-02-18 20:20:52 +00:00
|
|
|
FileRemover(const std::string &filename, bool deleteIt = true)
|
|
|
|
: Filename(filename), DeleteIt(deleteIt) {}
|
2004-02-18 17:16:17 +00:00
|
|
|
|
|
|
|
~FileRemover() {
|
|
|
|
if (DeleteIt) removeFile(Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// releaseFile - Take ownership of the file away from the FileRemover so it
|
|
|
|
/// will not be removed when the object is destroyed.
|
|
|
|
void releaseFile() { DeleteIt = false; }
|
|
|
|
};
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-08-01 20:28:55 +00:00
|
|
|
#endif
|