1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 #include <vector>
23 #include <string>
24 #include <iostream>
25 #include "treeconfig.hxx"
26 #include "export.hxx"
27 #ifdef WNT
28 #include <direct.h>
29 #include <io.h>
30 #else
31 #include <dirent.h>
32 #endif
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 using namespace std;
39
40 namespace transex3
41 {
42
parseConfig()43 bool Treeconfig::parseConfig(){
44
45 string source_config_file = string( static_cast<ByteString>( Export::GetEnv("SOURCE_ROOT_DIR") ).GetBuffer() );
46 if( source_config_file.empty() )
47 {
48 cerr << "Error: no suitable environment set?!?";
49 exit( -1 );
50 }
51 source_config_file += string("/source_config");
52 if( isConfigFilePresent() )
53 {
54 inireader.read( map , source_config_file );
55 return true;
56 }
57 else return false;
58 }
59
60 // ALWAYS add all repositories from source_config file to the container active_repos
61 // if a config_file is present ALWAYS return false
62 // if you are in the root of a repository also add it to the container active_repos
63 // if you are far inside a repository /my/path/ooo/sw/source then don't add it to the container but return true
64 // if you are in some misc place like /tmp then return true
65 // => the application can decide what to do in case the function returns true thus how to handle pwd() path
getActiveRepositories(vector<string> & active_repos)66 bool Treeconfig::getActiveRepositories( vector<string>& active_repos ){
67
68 bool isPresent = isConfigFilePresent();
69 bool hasPath = false;
70 string pwd;
71 string guessedRepo;
72 Export::getCurrentDir( pwd );
73 string source_root = Export::GetEnv( "SOURCE_ROOT_DIR" );
74 string solarsrc = Export::GetEnv( "SOLARSRC" );
75 string partial;
76
77 // if we are inside of a repository root then active it otherwise let the app handle the return!
78 string::size_type pos = pwd.find_first_of( source_root );
79 if( pos != string::npos && ( pos + source_root.length() +1 ) < pwd.length()){ // I am within SOURCE_ROOT_DIR
80 partial = pwd.substr( pos + source_root.length() +1 , pwd.length());
81 string::size_type nextPart = partial.find_first_of( "/" );
82 if( nextPart != string::npos )
83 hasPath = true;
84 else
85 guessedRepo = partial;
86 }
87 else // I am NOT within SOURCE_ROOT_DIR
88 hasPath = true;
89
90 if( isPresent )
91 {
92 hasPath = false; // if config_file is present don't care about pwd
93 stringmap* repos = static_cast<stringmap*>( map[ string("repositories") ] );
94 if( repos != 0 )
95 {
96 for( stringmap::iterator iter = repos->begin() ; iter != repos->end() ; ++iter )
97 {
98 if( static_cast<string>( iter->second ) == string( "active" ) )
99 {
100 active_repos.push_back( iter->first );
101 if( static_cast<string>( iter->first ) == guessedRepo )
102 {
103 guessedRepo.clear(); // don't add double in case it is present in config_file
104 }
105 }
106 }
107 }
108 else
109 {
110 cerr << "Error: source_config files doesn't contain a 'repositories' section ?!?";
111 exit( -1 );
112 }
113 }
114 if( !guessedRepo.empty() ){
115 active_repos.push_back( guessedRepo ); // add myrepo
116 }
117 return hasPath; // are we deep inside of a source tree or outside of SOURCE_ROOT_DIR?
118 }
119
getCurrentDir(string & dir)120 void Treeconfig::getCurrentDir( string& dir )
121 {
122 char buffer[64000];
123 if( getcwd( buffer , sizeof( buffer ) ) == 0 ){
124 cerr << "Error: getcwd failed!\n";
125 exit( -1 );
126 }
127 dir = string( buffer );
128 }
129
isConfigFilePresent()130 bool Treeconfig::isConfigFilePresent()
131 {
132 string config_file = Export::GetEnv( "SOURCE_ROOT_DIR" );
133 config_file += "/source_config";
134
135 struct stat status;
136 if( stat( config_file.c_str() , &status ) < 0 )
137 {
138 return false;
139 }
140 #ifdef WNT
141 return ( status.st_mode & _S_IFREG ) && ( _access( config_file.c_str() , 4 ) >= 0 ) ;
142 #else
143 return ( status.st_mode & S_IFREG ) && ( access( config_file.c_str() , R_OK ) >= 0 ) ;
144 #endif
145 }
146
147
148
149 }
150