Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmWriteFileCommand.cxx
blob925cf5939cf1a62e3a723c5f1983bed91b8d955a
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmWriteFileCommand.cxx,v $
5 Language: C++
6 <<<<<<< cmWriteFileCommand.cxx
7 Date: $Date: 2008/01/23 15:27:59 $
8 Version: $Revision: 1.16 $
9 =======
10 Date: $Date: 2008-04-30 17:42:39 $
11 Version: $Revision: 1.17 $
12 >>>>>>> 1.17
14 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
15 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
17 This software is distributed WITHOUT ANY WARRANTY; without even
18 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 PURPOSE. See the above copyright notices for more information.
21 =========================================================================*/
22 #include "cmWriteFileCommand.h"
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 // cmLibraryCommand
28 bool cmWriteFileCommand
29 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
31 if(args.size() < 2 )
33 this->SetError("called with incorrect number of arguments");
34 return false;
36 std::string message;
37 std::vector<std::string>::const_iterator i = args.begin();
39 std::string fileName = *i;
40 bool overwrite = true;
41 i++;
43 for(;i != args.end(); ++i)
45 if ( *i == "APPEND" )
47 overwrite = false;
49 else
51 message += *i;
55 if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
57 std::string e = "attempted to write a file: " + fileName
58 + " into a source directory.";
59 this->SetError(e.c_str());
60 cmSystemTools::SetFatalErrorOccured();
61 return false;
64 std::string dir = cmSystemTools::GetFilenamePath(fileName);
65 cmSystemTools::MakeDirectory(dir.c_str());
67 mode_t mode =
68 #if defined( _MSC_VER ) || defined( __MINGW32__ )
69 S_IREAD | S_IWRITE
70 #elif defined( __BORLANDC__ )
71 S_IRUSR | S_IWUSR
72 #else
73 S_IRUSR | S_IWUSR |
74 S_IRGRP |
75 S_IROTH
76 #endif
79 // Set permissions to writable
80 if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
82 cmSystemTools::SetPermissions(fileName.c_str(),
83 #if defined( _MSC_VER ) || defined( __MINGW32__ )
84 S_IREAD | S_IWRITE
85 #else
86 S_IRUSR | S_IWUSR
87 #endif
90 // If GetPermissions fails, pretend like it is ok. File open will fail if
91 // the file is not writable
92 std::ofstream file(fileName.c_str(),
93 overwrite?std::ios::out : std::ios::app);
94 if ( !file )
96 std::string error = "Internal CMake error when trying to open file: ";
97 error += fileName.c_str();
98 error += " for writing.";
99 this->SetError(error.c_str());
100 return false;
102 file << message << std::endl;
103 file.close();
104 cmSystemTools::SetPermissions(fileName.c_str(), mode);
106 return true;