-#endif
-
- std::string Result(FNBuffer);
- delete[] FNBuffer;
- return Result;
-}
-
-static bool AddPermissionsBits (const std::string &Filename, int bits) {
- // Get the umask value from the operating system. We want to use it
- // when changing the file's permissions. Since calling umask() sets
- // the umask and returns its old value, we must call it a second
- // time to reset it to the user's preference.
- int mask = umask(0777); // The arg. to umask is arbitrary.
- umask(mask); // Restore the umask.
-
- // Get the file's current mode.
- struct stat st;
- if ((stat(Filename.c_str(), &st)) == -1)
- return false;
-
- // Change the file to have whichever permissions bits from 'bits'
- // that the umask would not disable.
- if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
- return false;
-
- return true;
-}
-
-/// MakeFileExecutable - Make the file named Filename executable by
-/// setting whichever execute permissions bits the process's current
-/// umask would allow. Filename must name an existing file or
-/// directory. Returns true on success, false on error.
-///
-bool llvm::MakeFileExecutable(const std::string &Filename) {
- return AddPermissionsBits(Filename, 0111);
-}
-
-/// MakeFileReadable - Make the file named Filename readable by
-/// setting whichever read permissions bits the process's current
-/// umask would allow. Filename must name an existing file or
-/// directory. Returns true on success, false on error.
-///
-bool llvm::MakeFileReadable(const std::string &Filename) {
- return AddPermissionsBits(Filename, 0444);
-}
-
-/// getFileSize - Return the size of the specified file in bytes, or -1 if the
-/// file cannot be read or does not exist.
-long long llvm::getFileSize(const std::string &Filename) {
- struct stat StatBuf;
- if (stat(Filename.c_str(), &StatBuf) == -1)
- return -1;
- return StatBuf.st_size;
-}
-
-/// 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 llvm::getFileTimestamp(const std::string &Filename) {
- struct stat StatBuf;
- if (stat(Filename.c_str(), &StatBuf) == -1)
- return 0;
- return StatBuf.st_mtime;
-}
-
-/// ReadFileIntoAddressSpace - Attempt to map the specific file into the
-/// address space of the current process for reading. If this succeeds,
-/// return the address of the buffer and the length of the file mapped. On
-/// failure, return null.
-void *llvm::ReadFileIntoAddressSpace(const std::string &Filename,
- unsigned &Length) {
-#if defined(HAVE_MMAP_FILE) && !defined(_MSC_VER)
- Length = (unsigned)getFileSize(Filename);
- if ((int)Length == -1) return 0;
-
- FDHandle FD(open(Filename.c_str(), O_RDONLY));
- if (FD == -1) return 0;
-
- // If the file has a length of zero, mmap might return a null pointer. In
- // this case, allocate a single byte of memory and return it instead.
- if (Length == 0)
- return malloc(1);
-
- // mmap in the file all at once...
- void *Buffer = (void*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
-
- if (Buffer == (void*)MAP_FAILED)
- return 0;
-
- return Buffer;
-#else
- // FIXME: implement with read/write
- return 0;
-#endif
-}