Revert r28526 for now until a problem for the livecd breakage has been found
[reactos.git] / reactos / tools / rbuild / directory.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "pch.h"
19 #include <assert.h>
20
21 #include "rbuild.h"
22 #ifdef _MSC_VER
23 #include <errno.h>
24 #else
25 #include <dirent.h>
26 #endif//_MSC_VER
27
28 #ifdef WIN32
29 #define MKDIR(s) mkdir(s)
30 #else
31 #define MKDIR(s) mkdir(s, 0755)
32 #endif
33
34 using std::string;
35 using std::vector;
36
37 Directory::Directory ( const string& name_ )
38 : name(name_)
39 {
40 }
41
42 void
43 Directory::Add ( const char* subdir )
44 {
45 size_t i;
46 string s1 = string ( subdir );
47 if ( ( i = s1.find ( '$' ) ) != string::npos )
48 {
49 throw InvalidOperationException ( __FILE__,
50 __LINE__,
51 "No environment variables can be used here. Path was %s",
52 subdir );
53 }
54
55 const char* p = strpbrk ( subdir, "/\\" );
56 if ( subdir == p || ( *subdir && subdir[1] == ':' ) )
57 {
58 throw InvalidOperationException ( __FILE__,
59 __LINE__,
60 "Invalid relative path '%s'",
61 subdir );
62 }
63
64 if ( !p )
65 p = subdir + strlen(subdir);
66 string s ( subdir, p-subdir );
67 if ( subdirs.find(s) == subdirs.end() )
68 subdirs[s] = new Directory(s);
69 if ( *p && *++p )
70 subdirs[s]->Add ( p );
71 }
72
73 bool
74 Directory::mkdir_p ( const char* path )
75 {
76 #ifndef _MSC_VER
77 DIR *directory;
78 directory = opendir ( path );
79 if ( directory != NULL )
80 {
81 closedir ( directory );
82 return false;
83 }
84 #endif//_MSC_VER
85
86 if ( MKDIR ( path ) != 0 )
87 {
88 #ifdef _MSC_VER
89 if ( errno == EEXIST )
90 return false;
91 #endif//_MSC_VER
92 throw AccessDeniedException ( string ( path ) );
93 }
94 return true;
95 }
96
97 bool
98 Directory::CreateDirectory ( string path )
99 {
100 size_t index = 0;
101 size_t nextIndex;
102 if ( isalpha ( path[0] ) && path[1] == ':' && path[2] == cSep )
103 {
104 nextIndex = path.find ( cSep, 3);
105 }
106 else
107 nextIndex = path.find ( cSep );
108
109 bool directoryWasCreated = false;
110 while ( nextIndex != string::npos )
111 {
112 nextIndex = path.find ( cSep, index + 1 );
113 directoryWasCreated = mkdir_p ( path.substr ( 0, nextIndex ).c_str () );
114 index = nextIndex;
115 }
116 return directoryWasCreated;
117 }
118
119 string
120 Directory::ReplaceVariable ( const string& name,
121 const string& value,
122 string path )
123 {
124 size_t i = path.find ( name );
125 if ( i != string::npos )
126 return path.replace ( i, name.length (), value );
127 else
128 return path;
129 }
130
131 void
132 Directory::ResolveVariablesInPath ( char* buf,
133 const string& path )
134 {
135 string s = ReplaceVariable ( "$(INTERMEDIATE)", Environment::GetIntermediatePath (), path );
136 s = ReplaceVariable ( "$(OUTPUT)", Environment::GetOutputPath (), s );
137 s = ReplaceVariable ( "$(INSTALL)", Environment::GetInstallPath (), s );
138 strcpy ( buf, s.c_str () );
139 }
140
141 void
142 Directory::GenerateTree ( const string& parent,
143 bool verbose )
144 {
145 string path;
146
147 if ( parent.size () > 0 )
148 {
149 char buf[256];
150
151 if ( name.size () > 0 )
152 path = parent + sSep + name;
153 else
154 path = parent;
155 ResolveVariablesInPath ( buf, path );
156 if ( CreateDirectory ( buf ) && verbose )
157 printf ( "Created %s\n", buf );
158 }
159 else
160 path = name;
161
162 for ( directory_map::iterator i = subdirs.begin ();
163 i != subdirs.end ();
164 ++i )
165 {
166 i->second->GenerateTree ( path, verbose );
167 }
168 }
169
170 string
171 Directory::EscapeSpaces ( string path )
172 {
173 string newpath;
174 char* p = &path[0];
175 while ( *p != 0 )
176 {
177 if ( *p == ' ' )
178 newpath = newpath + "\\ ";
179 else
180 newpath = newpath + *p;
181 *p++;
182 }
183 return newpath;
184 }
185
186 void
187 Directory::CreateRule ( FILE* f,
188 const string& parent )
189 {
190 string path;
191
192 if ( parent.size() > 0 )
193 {
194 string escapedParent = EscapeSpaces ( parent );
195 fprintf ( f,
196 "%s%c%s: | %s\n",
197 escapedParent.c_str (),
198 cSep,
199 EscapeSpaces ( name ).c_str (),
200 escapedParent.c_str () );
201
202 fprintf ( f,
203 "\t$(ECHO_MKDIR)\n" );
204
205 fprintf ( f,
206 "\t${mkdir} $@\n" );
207
208 path = parent + sSep + name;
209 }
210 else
211 path = name;
212
213 for ( directory_map::iterator i = subdirs.begin();
214 i != subdirs.end();
215 ++i )
216 {
217 i->second->CreateRule ( f, path );
218 }
219 }