Salome HOME
23514: EDF 16031 - SMESH freezes
[modules/kernel.git] / src / Basics / Basics_DirUtils.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  File   : Basics_DirUtils.cxx
21 //  Autor  : Alexander A. BORODIN
22 //  Module : SALOME
23 //
24 #include "Basics_DirUtils.hxx"
25 #include <stdio.h>
26 #include <errno.h>
27 #include <stdlib.h>
28
29 #ifndef WIN32
30 # include <sys/stat.h>
31 # include <dirent.h>
32 # include <unistd.h>
33 #else
34 #include <io.h>
35 #define F_OK 0
36 #define access _access
37 # include <windows.h>
38 # include <time.h>
39 #endif
40
41 #ifdef WIN32
42 # define _separator_ '\\'
43 #else
44 # define _separator_ '/'
45 #endif
46
47 namespace Kernel_Utils
48 {
49   std::string GetBaseName( const std::string& file_path )
50   {
51     int pos = file_path.rfind( _separator_ );
52     if ( pos >= 0 )
53       return pos < (int)file_path.size()-1 ? file_path.substr( pos+1 ) : "";
54     return file_path;
55   }
56
57   std::string GetDirName( const std::string& file_path )
58   {
59     int pos = file_path.rfind( _separator_ );
60     if ( pos >= 0 )
61       return pos < (int)file_path.size()-1 ? file_path.substr(0, pos ) : "";
62     return std::string(".");
63   }
64
65   std::string GetTmpDirByEnv( const std::string& tmp_path_env )
66   {
67     char* val = getenv( tmp_path_env.c_str() );
68     std::string dir = val ? val : "";
69     return GetTmpDirByPath( dir );
70   }
71
72   std::string GetTmpDirByPath( const std::string& tmp_path )
73   {
74     std::string aTmpDir = tmp_path;
75     if ( aTmpDir == "" )
76       {
77 #ifdef WIN32
78         char *Tmp_dir = getenv("TEMP");
79         if( Tmp_dir == NULL )
80           {
81             Tmp_dir = getenv("TMP");
82             if (Tmp_dir == NULL)
83               aTmpDir = std::string("C:\\");
84             else 
85               aTmpDir = std::string(Tmp_dir);
86           }
87         else
88           aTmpDir = std::string(Tmp_dir);
89 #else
90         aTmpDir = std::string("/tmp/");
91 #endif
92       }
93     
94     if(aTmpDir[aTmpDir.size()-1] != _separator_)
95       aTmpDir+=_separator_;
96     
97     srand((unsigned int)time(NULL));
98     int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
99     char buffer[127];
100     sprintf(buffer, "%d", aRND);
101     std::string aSubDir(buffer);
102     if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
103     
104     aTmpDir += aSubDir; //Get RND sub directory
105     
106     std::string aDir = aTmpDir;
107     
108     if(IsExists(aDir)) {
109       for(aRND = 0; IsExists(aDir); aRND++) {
110         sprintf(buffer, "%d", aRND);
111         aDir = aTmpDir+buffer;  //Build a unique directory name
112       }
113     }
114     
115     if(aDir[aDir.size()-1] != _separator_) aDir += _separator_;
116     
117 #ifdef WIN32
118     CreateDirectory(aDir.c_str(), NULL);
119 #else
120     mkdir(aDir.c_str(), 0x1ff); 
121 #endif
122     
123     return aDir;
124   }
125   
126   //============================================================================
127   // function : GetTempDir
128   // purpose  : Returns a temp directory to store created files like "/tmp/sub_dir/" 
129   //============================================================================ 
130   std::string GetTmpDir()
131   {
132     return GetTmpDirByPath( "" );
133   }
134
135   //============================================================================
136   // function : GetTempFileName
137   // purpose  : Returns the unique temporary file name without any extension /tmp/something/file for Unix or c:\something\file for WIN32
138   //============================================================================ 
139   std::string GetTmpFileName()
140   {
141     std::string tmpDir = GetTmpDir();
142     std::string aFilePath = "";
143     if(IsExists(tmpDir)) {
144       srand((unsigned int)time(NULL));
145       int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
146       char buffer[127];
147       sprintf(buffer, "%d", aRND);
148       std::string aSubDir(buffer);
149       if(aSubDir.size() <= 1) aSubDir = std::string("123409876");
150       
151       aFilePath = tmpDir;
152       for(aRND = 0; IsExists(aFilePath); aRND++) {
153         sprintf(buffer, "%d", aRND);
154         aFilePath = tmpDir+buffer;  //Build a unique file name
155       }
156     }
157     return aFilePath;
158   }
159   
160   //============================================================================
161   // function : IsExists
162   // purpose  : Returns True(False) if the path (not)exists
163   //============================================================================ 
164   bool IsExists(const std::string& thePath) 
165   {
166     int status = access ( thePath.c_str() , F_OK ); 
167     if (status != 0) return false;
168     return true;
169   }
170
171   //============================================================================
172   // function : IsWritable
173   // purpose  : Returns True(False) if the path is (not) writable
174   //============================================================================ 
175   bool IsWritable(const std::string& thePath) 
176   {
177 #ifdef WIN32 
178     if (  GetFileAttributes (  thePath.c_str()  ) == 0xFFFFFFFF  ) { 
179       if (  GetLastError () == FILE_ATTRIBUTE_READONLY ) {
180         return false;
181       }
182     }
183 #else 
184     int status = access(thePath.c_str(),W_OK); 
185     if (status != 0) return false;
186 #endif
187     return true;
188   }
189
190
191   //============================================================================
192   // function : GetDirByPath
193   // purpose  : Returns directory by path and converts it to native system format
194   //============================================================================ 
195   std::string GetDirByPath(const std::string& thePath)
196   {
197     if (thePath.empty())
198       return "";
199     std::string path = thePath;
200     std::string::size_type length = path.length();
201
202     //detect all separators in Unix format
203     for ( unsigned int i = 0; i < length; i++ )
204     {
205       if( path[i] == '/' )
206         path[i] = '|';
207     }
208
209     //detect all separators in Windows format
210     for ( unsigned int i = 0; i < length; i++ )
211     {
212       if( path[i] == '\\' )
213         path[i] = '|';
214     }
215
216
217     std::string::size_type pos = path.rfind('|');
218     if ( pos == std::string::npos )
219     {
220 #ifdef WIN32
221       //check for disk letter ( C: )
222       if ( path.length() == 2 && path[1] == ':' )
223         path += _separator_;
224 #else
225       //not valid path
226       return "";
227 #endif
228     }
229     else
230     {
231       //remove right subdirectory or filename from path
232       path = path.substr( 0, pos );
233     }
234
235     length = path.length();
236     for ( unsigned int i = 0; i < length; i++ )
237     {
238       if( path[i] == '|' )
239         path[i] = _separator_;
240     }
241     return path;
242   }
243
244   //============================================================================
245   // function : IsEmptyDir
246   // purpose  : Returns True(False) if the path (not) empty
247   //            Also returns False if the path is not valid
248   //============================================================================ 
249   bool IsEmptyDir(const std::string& thePath) 
250   {
251     if ( thePath.empty() || !IsExists(thePath))
252       return false;
253
254     bool result = false;
255
256 #ifdef WIN32
257     WIN32_FIND_DATA aFileData;
258     HANDLE hFile = FindFirstFile( thePath.c_str(), &aFileData );
259     if ( hFile == INVALID_HANDLE_VALUE )
260     {
261       //empty dir
262       result = true;
263     }
264     else
265     {
266       //close serching. path is not empty
267       FindClose( hFile );
268     }
269 #else
270     DIR *dp;
271     struct dirent *dirp;
272     if((dp  = opendir(thePath.c_str())) == NULL)
273     {
274       //Could not open directory
275       return false;
276     }
277     else
278     {
279       result = true; //empty if no file found
280       while ((dirp = readdir(dp)) != NULL && result )
281         {
282           std::string file_name(dirp->d_name);
283           result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false
284         }
285         closedir(dp);
286     }
287 #endif
288     return result;
289   }
290 }