static bool CheckFileOperations()

in Source/kwsys/testSystemTools.cxx [107:531]


static bool CheckFileOperations()
{
  bool res = true;
  std::string const testNonExistingFile(TEST_SYSTEMTOOLS_SOURCE_DIR
                                        "/testSystemToolsNonExistingFile");
  std::string const testDotFile(TEST_SYSTEMTOOLS_SOURCE_DIR "/.");
  std::string const testBinFile(TEST_SYSTEMTOOLS_SOURCE_DIR
                                "/testSystemTools.bin");
  std::string const testTxtFile(TEST_SYSTEMTOOLS_SOURCE_DIR
                                "/testSystemTools.cxx");
  std::string const testNewDir(TEST_SYSTEMTOOLS_BINARY_DIR
                               "/testSystemToolsNewDir");
  std::string const testNewFile(testNewDir + "/testNewFile.txt");

  if (kwsys::SystemTools::DetectFileType(testNonExistingFile.c_str()) !=
      kwsys::SystemTools::FileTypeUnknown) {
    std::cerr << "Problem with DetectFileType - failed to detect type of: "
              << testNonExistingFile << std::endl;
    res = false;
  }

  if (kwsys::SystemTools::DetectFileType(testDotFile.c_str()) !=
      kwsys::SystemTools::FileTypeUnknown) {
    std::cerr << "Problem with DetectFileType - failed to detect type of: "
              << testDotFile << std::endl;
    res = false;
  }

  if (kwsys::SystemTools::DetectFileType(testBinFile.c_str()) !=
      kwsys::SystemTools::FileTypeBinary) {
    std::cerr << "Problem with DetectFileType - failed to detect type of: "
              << testBinFile << std::endl;
    res = false;
  }

  if (kwsys::SystemTools::DetectFileType(testTxtFile.c_str()) !=
      kwsys::SystemTools::FileTypeText) {
    std::cerr << "Problem with DetectFileType - failed to detect type of: "
              << testTxtFile << std::endl;
    res = false;
  }

  if (kwsys::SystemTools::FileLength(testBinFile) != 766) {
    std::cerr << "Problem with FileLength - incorrect length for: "
              << testBinFile << std::endl;
    res = false;
  }

  kwsys::SystemTools::Stat_t buf;
  if (kwsys::SystemTools::Stat(testTxtFile.c_str(), &buf) != 0) {
    std::cerr << "Problem with Stat - unable to stat text file: "
              << testTxtFile << std::endl;
    res = false;
  }

  if (kwsys::SystemTools::Stat(testBinFile, &buf) != 0) {
    std::cerr << "Problem with Stat - unable to stat bin file: " << testBinFile
              << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
    std::cerr << "Problem with MakeDirectory for: " << testNewDir << std::endl;
    res = false;
  }
  // calling it again should just return true
  if (!kwsys::SystemTools::MakeDirectory(testNewDir)) {
    std::cerr << "Problem with second call to MakeDirectory for: "
              << testNewDir << std::endl;
    res = false;
  }
  // calling with 0 pointer should return false
  if (kwsys::SystemTools::MakeDirectory(nullptr)) {
    std::cerr << "Problem with MakeDirectory(0)" << std::endl;
    res = false;
  }
  // calling with an empty string should return false
  if (kwsys::SystemTools::MakeDirectory(std::string())) {
    std::cerr << "Problem with MakeDirectory(std::string())" << std::endl;
    res = false;
  }
  // check existence
  if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
    std::cerr << "Problem with FileExists as C string and not file for: "
              << testNewDir << std::endl;
    res = false;
  }
  // check existence
  if (!kwsys::SystemTools::PathExists(testNewDir)) {
    std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
    res = false;
  }
  // remove it
  if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
    std::cerr << "Problem with RemoveADirectory for: " << testNewDir
              << std::endl;
    res = false;
  }
  // check existence
  if (kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
    std::cerr << "After RemoveADirectory: "
              << "Problem with FileExists as C string and not file for: "
              << testNewDir << std::endl;
    res = false;
  }
  // check existence
  if (kwsys::SystemTools::PathExists(testNewDir)) {
    std::cerr << "After RemoveADirectory: "
              << "Problem with PathExists for: " << testNewDir << std::endl;
    res = false;
  }
  // create it using the char* version
  if (!kwsys::SystemTools::MakeDirectory(testNewDir.c_str())) {
    std::cerr << "Problem with second call to MakeDirectory as C string for: "
              << testNewDir << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::Touch(testNewFile, true)) {
    std::cerr << "Problem with Touch for: " << testNewFile << std::endl;
    res = false;
  }
  // calling MakeDirectory with something that is no file should fail
  if (kwsys::SystemTools::MakeDirectory(testNewFile)) {
    std::cerr << "Problem with to MakeDirectory for: " << testNewFile
              << std::endl;
    res = false;
  }

  // calling with 0 pointer should return false
  if (kwsys::SystemTools::FileExists(nullptr)) {
    std::cerr << "Problem with FileExists(0)" << std::endl;
    res = false;
  }
  if (kwsys::SystemTools::FileExists(nullptr, true)) {
    std::cerr << "Problem with FileExists(0) as file" << std::endl;
    res = false;
  }
  // calling with an empty string should return false
  if (kwsys::SystemTools::FileExists(std::string())) {
    std::cerr << "Problem with FileExists(std::string())" << std::endl;
    res = false;
  }
  // FileExists(x, true) should return false on a directory
  if (kwsys::SystemTools::FileExists(testNewDir, true)) {
    std::cerr << "Problem with FileExists as file for: " << testNewDir
              << std::endl;
    res = false;
  }
  if (kwsys::SystemTools::FileExists(testNewDir.c_str(), true)) {
    std::cerr << "Problem with FileExists as C string and file for: "
              << testNewDir << std::endl;
    res = false;
  }
  // FileExists(x, false) should return true even on a directory
  if (!kwsys::SystemTools::FileExists(testNewDir, false)) {
    std::cerr << "Problem with FileExists as not file for: " << testNewDir
              << std::endl;
    res = false;
  }
  if (!kwsys::SystemTools::FileExists(testNewDir.c_str(), false)) {
    std::cerr << "Problem with FileExists as C string and not file for: "
              << testNewDir << std::endl;
    res = false;
  }
  // should work, was created as new file before
  if (!kwsys::SystemTools::FileExists(testNewFile)) {
    std::cerr << "Problem with FileExists for: " << testNewFile << std::endl;
    res = false;
  }
  if (!kwsys::SystemTools::FileExists(testNewFile.c_str())) {
    std::cerr << "Problem with FileExists as C string for: " << testNewFile
              << std::endl;
    res = false;
  }
  if (!kwsys::SystemTools::FileExists(testNewFile, true)) {
    std::cerr << "Problem with FileExists as file for: " << testNewFile
              << std::endl;
    res = false;
  }
  if (!kwsys::SystemTools::FileExists(testNewFile.c_str(), true)) {
    std::cerr << "Problem with FileExists as C string and file for: "
              << testNewFile << std::endl;
    res = false;
  }

  // calling with an empty string should return false
  if (kwsys::SystemTools::PathExists(std::string())) {
    std::cerr << "Problem with PathExists(std::string())" << std::endl;
    res = false;
  }
  // PathExists(x) should return true on a directory
  if (!kwsys::SystemTools::PathExists(testNewDir)) {
    std::cerr << "Problem with PathExists for: " << testNewDir << std::endl;
    res = false;
  }
  // should work, was created as new file before
  if (!kwsys::SystemTools::PathExists(testNewFile)) {
    std::cerr << "Problem with PathExists for: " << testNewFile << std::endl;
    res = false;
  }

  std::cerr << std::oct;
// Reset umask
#ifdef __MSYS__
  mode_t fullMask = S_IWRITE;
  mode_t testPerm = S_IREAD;
#elif defined(_WIN32) && !defined(__CYGWIN__)
  // NOTE:  Windows doesn't support toggling _S_IREAD.
  mode_t fullMask = _S_IWRITE;
  mode_t testPerm = 0;
#else
  // On a normal POSIX platform, we can toggle all permissions.
  mode_t fullMask = S_IRWXU | S_IRWXG | S_IRWXO;
  mode_t testPerm = S_IRUSR;
#endif

  // Test file permissions without umask
  mode_t origPerm, thisPerm;
  if (!kwsys::SystemTools::GetPermissions(testNewFile, origPerm)) {
    std::cerr << "Problem with GetPermissions (1) for: " << testNewFile
              << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::SetPermissions(testNewFile, testPerm)) {
    std::cerr << "Problem with SetPermissions (1) for: " << testNewFile
              << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
    std::cerr << "Problem with GetPermissions (2) for: " << testNewFile
              << std::endl;
    res = false;
  }

  if ((thisPerm & fullMask) != testPerm) {
    std::cerr << "SetPermissions failed to set permissions (1) for: "
              << testNewFile << ": actual = " << thisPerm
              << "; expected = " << testPerm << std::endl;
    res = false;
  }

  // While we're at it, check proper TestFileAccess functionality.
  bool do_write_test = true;
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) ||     \
  defined(__NetBSD__) || defined(__DragonFly__) || defined(__HOS_AIX__)
  // If we are running as root on POSIX-ish systems (Linux and the BSDs,
  // at least), ignore this check, as root can always write to files.
  do_write_test = (getuid() != 0);
#endif
  if (do_write_test &&
      kwsys::SystemTools::TestFileAccess(testNewFile,
                                         kwsys::TEST_FILE_WRITE)) {
    std::cerr
      << "TestFileAccess incorrectly indicated that this is a writable file:"
      << testNewFile << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::TestFileAccess(testNewFile, kwsys::TEST_FILE_OK)) {
    std::cerr
      << "TestFileAccess incorrectly indicated that this file does not exist:"
      << testNewFile << std::endl;
    res = false;
  }

  // Test restoring/setting full permissions.
  if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask)) {
    std::cerr << "Problem with SetPermissions (2) for: " << testNewFile
              << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
    std::cerr << "Problem with GetPermissions (3) for: " << testNewFile
              << std::endl;
    res = false;
  }

  if ((thisPerm & fullMask) != fullMask) {
    std::cerr << "SetPermissions failed to set permissions (2) for: "
              << testNewFile << ": actual = " << thisPerm
              << "; expected = " << fullMask << std::endl;
    res = false;
  }

  mode_t orig_umask = umask(fullMask);
  // Test setting file permissions while honoring umask
  if (!kwsys::SystemTools::SetPermissions(testNewFile, fullMask, true)) {
    std::cerr << "Problem with SetPermissions (3) for: " << testNewFile
              << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::GetPermissions(testNewFile, thisPerm)) {
    std::cerr << "Problem with GetPermissions (4) for: " << testNewFile
              << std::endl;
    res = false;
  }

  if ((thisPerm & fullMask) != 0) {
    std::cerr << "SetPermissions failed to honor umask for: " << testNewFile
              << ": actual = " << thisPerm << "; expected = " << 0
              << std::endl;
    res = false;
  }

  // Restore umask
  umask(orig_umask);

  // Restore file permissions
  if (!kwsys::SystemTools::SetPermissions(testNewFile, origPerm)) {
    std::cerr << "Problem with SetPermissions (4) for: " << testNewFile
              << std::endl;
    res = false;
  }

  // Remove the test file
  if (!kwsys::SystemTools::RemoveFile(testNewFile)) {
    std::cerr << "Problem with RemoveFile: " << testNewFile << std::endl;
    res = false;
  }

  std::string const testFileMissing(testNewDir + "/testMissingFile.txt");
  if (!kwsys::SystemTools::RemoveFile(testFileMissing)) {
    std::string const& msg = kwsys::SystemTools::GetLastSystemError();
    std::cerr << "RemoveFile(\"" << testFileMissing << "\") failed: " << msg
              << "\n";
    res = false;
  }

  std::string const testFileMissingDir(testNewDir + "/missing/file.txt");
  if (!kwsys::SystemTools::RemoveFile(testFileMissingDir)) {
    std::string const& msg = kwsys::SystemTools::GetLastSystemError();
    std::cerr << "RemoveFile(\"" << testFileMissingDir << "\") failed: " << msg
              << "\n";
    res = false;
  }

  std::string const testBadSymlink(testNewDir + "/badSymlink.txt");
  std::string const testBadSymlinkTgt(testNewDir + "/missing/symlinkTgt.txt");
  kwsys::Status const symlinkStatus =
    kwsys::SystemTools::CreateSymlink(testBadSymlinkTgt, testBadSymlink);
#if defined(_WIN32)
  // Under Windows, the user may not have enough privileges to create symlinks
  if (symlinkStatus.GetWindows() != ERROR_PRIVILEGE_NOT_HELD)
#endif
  {
    if (!symlinkStatus.IsSuccess()) {
      std::cerr << "CreateSymlink for: " << testBadSymlink << " -> "
                << testBadSymlinkTgt
                << " failed: " << symlinkStatus.GetString() << std::endl;
      res = false;
    }

    if (!kwsys::SystemTools::Touch(testBadSymlink, false)) {
      std::cerr << "Problem with Touch (no create) for: " << testBadSymlink
                << std::endl;
      res = false;
    }
  }

  if (!kwsys::SystemTools::Touch(testNewDir, false)) {
    std::cerr << "Problem with Touch (no create) for: " << testNewDir
              << std::endl;
    res = false;
  }

  kwsys::SystemTools::Touch(testNewFile, true);
  if (!kwsys::SystemTools::RemoveADirectory(testNewDir)) {
    std::cerr << "Problem with RemoveADirectory for: " << testNewDir
              << std::endl;
    res = false;
  }

#ifdef KWSYS_TEST_SYSTEMTOOLS_LONG_PATHS
  // Perform the same file and directory creation and deletion tests but
  // with paths > 256 characters in length.

  std::string const testNewLongDir(
    TEST_SYSTEMTOOLS_BINARY_DIR
    "/"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "01234567890123");
  std::string const testNewLongFile(
    testNewLongDir +
    "/"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "012345678901234567890123456789012345678901234567890123456789"
    "0123456789.txt");

  if (!kwsys::SystemTools::MakeDirectory(testNewLongDir)) {
    std::cerr << "Problem with MakeDirectory for: " << testNewLongDir
              << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::Touch(testNewLongFile.c_str(), true)) {
    std::cerr << "Problem with Touch for: " << testNewLongFile << std::endl;
    res = false;
  }

  if (!kwsys::SystemTools::RemoveFile(testNewLongFile)) {
    std::cerr << "Problem with RemoveFile: " << testNewLongFile << std::endl;
    res = false;
  }

  kwsys::SystemTools::Touch(testNewLongFile.c_str(), true);
  if (!kwsys::SystemTools::RemoveADirectory(testNewLongDir)) {
    std::cerr << "Problem with RemoveADirectory for: " << testNewLongDir
              << std::endl;
    res = false;
  }
#endif

  std::cerr << std::dec;
  return res;
}