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