OutputFileHandler.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00030 #include "OutputFileHandler.hpp"
00031 
00032 #include <cstdlib>
00033 #include <sys/stat.h>
00034 
00035 #include "PetscTools.hpp"
00036 #include "Exception.hpp"
00037 #include "ArchiveLocationInfo.hpp"
00038 #include "GetCurrentWorkingDirectory.hpp"
00039 #include "FileFinder.hpp"
00040 
00041 OutputFileHandler::OutputFileHandler(const std::string &rDirectory,
00042                                      bool cleanOutputDirectory)
00043 {
00044     //Is it a valid request for a directory?
00045     if (rDirectory.find("..") != std::string::npos)
00046     {
00047         EXCEPTION("Will not create directory: " + rDirectory +
00048                 " due to it potentially being above, and cleaning, CHASTE_TEST_OUTPUT.");
00049     }
00050 
00051     mDirectory = MakeFoldersAndReturnFullPath(rDirectory);
00052 
00053     // Clean the directory (default)
00054     if (rDirectory != "" && cleanOutputDirectory) // Don't clean CHASTE_TEST_OUTPUT
00055     {
00056         std::string command = "test -e " + mDirectory + ".chaste_deletable_folder";
00057         int return_value = system(command.c_str());
00058         if (return_value != 0)
00059         {
00060             EXCEPTION("Cannot delete " + mDirectory + " because signature file \".chaste_deletable_folder\" is not present.");
00061         }
00062 
00063         // Are we the master process?  Only the master should delete files
00064         if (PetscTools::AmMaster())
00065         {
00066             //Remove whatever was there before
00067             //Note that the /* part prevents removal of hidden files (.filename), which is useful in NFS systems
00068             MPIABORTIFNON0(system, "rm -rf " + mDirectory + "/*");
00069         }
00070         // Wait for master to finish before going on to use the directory.
00071         PetscTools::Barrier("OutputFileHandler");
00072     }
00073 }
00074 
00075 std::string OutputFileHandler::GetChasteTestOutputDirectory()
00076 {
00077     char *chaste_test_output = getenv("CHASTE_TEST_OUTPUT");
00078     std::string directory_root;
00079     if (chaste_test_output == NULL || *chaste_test_output == 0)
00080     {
00081         // Default to 'testoutput' folder within the current directory
00082         directory_root = GetCurrentWorkingDirectory() + "/testoutput/";
00083     }
00084     else
00085     {
00086         if (*chaste_test_output != '/')
00087         {
00088             // It's a relative path; prepend with the CWD
00089             directory_root = GetCurrentWorkingDirectory() + "/" + chaste_test_output;
00090         }
00091         else
00092         {
00093 #define COVERAGE_IGNORE
00094             // This branch is never taken on the build machines, because CHASTE_TEST_OUTPUT is set to a relative path.
00095             directory_root = std::string(chaste_test_output);
00096 #undef COVERAGE_IGNORE
00097         }
00098     }
00099     AddTrailingSlash(directory_root);
00100 
00101     return directory_root;
00102 }
00103 
00104 
00105 std::string OutputFileHandler::MakeFoldersAndReturnFullPath(const std::string& rDirectory) const
00106 {
00107     std::string directory_root = GetChasteTestOutputDirectory();
00108     std::string directories_to_add = rDirectory; // Get from a const to something we can mess with.
00109     AddTrailingSlash(directories_to_add);
00110     std::string directory = directory_root + directories_to_add;
00111 
00112     // Are we the master process?  Only the master should make any new directories
00113     if (PetscTools::AmMaster())
00114     {
00115         // If necessary make the ChasteTestOutputDirectory - don't make it deleteable by Chaste.
00116         std::string command = "test -d " + directory_root;
00117         int return_value = system(command.c_str());
00118         if (return_value!=0)
00119         {
00120             // We make as many folders as necessary here.
00121             MPIABORTIFNON0(system,"mkdir -p " + directory_root);
00122         }
00123 
00124         // Now make all the sub-folders requested one-by-one and add the .chaste_deletable_folder file to them.
00125         std::string remaining_directories = directories_to_add;
00126         std::string directory_to_add = "";
00127 
00128         // Create the output directory structure one folder at a time.
00129         while (remaining_directories.find("/") != std::string::npos)
00130         {
00131             size_t found = remaining_directories.find_first_of("/");
00132             directory_to_add += remaining_directories.substr(0,found+1);
00133             remaining_directories = remaining_directories.substr(found+1);
00134 
00135             command = "test -d " + directory_root + directory_to_add;
00136             return_value = system(command.c_str());
00137             if (return_value!=0)
00138             {
00139                 // We make only the next folder here
00140                 MPIABORTIFNON0(system,"mkdir -p " + directory_root + directory_to_add);
00141                 // Put the Chaste signature file into this folder
00142                 MPIABORTIFNON0(system,"touch " + directory_root + directory_to_add + ".chaste_deletable_folder");
00143             }
00144         }
00145     }
00146 
00147     // Wait for master to finish before going on to use the directory.
00148     PetscTools::Barrier("OutputFileHandler::MakeFoldersAndReturnFullPath");
00149 
00150     return directory;
00151 }
00152 
00153 
00154 std::string OutputFileHandler::GetOutputDirectoryFullPath() const
00155 {
00156     return mDirectory;
00157 }
00158 
00159 out_stream OutputFileHandler::OpenOutputFile(const std::string& rFileName,
00160                                              std::ios_base::openmode mode) const
00161 {
00162     out_stream p_output_file(new std::ofstream((mDirectory+rFileName).c_str(), mode));
00163     if (!p_output_file->is_open())
00164     {
00165         EXCEPTION("Could not open file \"" + rFileName + "\" in " + mDirectory);
00166     }
00167     return p_output_file;
00168 }
00169 
00170 
00171 out_stream OutputFileHandler::OpenOutputFile(const std::string& rFileName,
00172                                              unsigned number,
00173                                              const std::string& rFileFormat,
00174                                              std::ios_base::openmode mode) const
00175 {
00176     std::stringstream string_stream;
00177     string_stream << rFileName << number << rFileFormat;
00178     return OpenOutputFile(string_stream.str(), mode);
00179 }
00180 
00181 void OutputFileHandler::SetArchiveDirectory() const
00182 {
00183     FileFinder dir(GetOutputDirectoryFullPath(), RelativeTo::Absolute);
00184     ArchiveLocationInfo::SetArchiveDirectory(dir);
00185 }
00186 
00187 void OutputFileHandler::AddTrailingSlash(std::string& rDirectory)
00188 {
00189     // Add a trailing slash if not already there
00190     if (rDirectory!="" && !( *(rDirectory.end()-1) == '/'))
00191     {
00192         rDirectory = rDirectory + "/";
00193     }
00194 }
00195 
00196 FileFinder OutputFileHandler::CopyFileTo(const FileFinder& rSourceFile) const
00197 {
00198     if (!rSourceFile.IsFile())
00199     {
00200         EXCEPTION("Can only copy single files.");
00201     }
00202     if (PetscTools::AmMaster())
00203     {
00204         MPIABORTIFNON0(system, "cp " + rSourceFile.GetAbsolutePath() + " " + GetOutputDirectoryFullPath());
00205     }
00206     PetscTools::Barrier("OutputFileHandler::CopyFileTo");
00207     return FindFile(rSourceFile.GetLeafName());
00208 }
00209 
00210 FileFinder OutputFileHandler::FindFile(std::string leafName) const
00211 {
00212     return FileFinder(GetOutputDirectoryFullPath() + leafName, RelativeTo::Absolute);
00213 }

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5