0022651: Impossible to build OCC as static library due to using Standard_EXPORT inste...
[occt.git] / src / OSD / OSD_Directory.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
7fd59977 14
42cf5bc1 15#include <OSD_Directory.hxx>
cda06ac0 16
42cf5bc1 17#include <OSD_Path.hxx>
7fd59977 18#include <OSD_Protection.hxx>
42cf5bc1 19#include <OSD_WhoAmI.hxx>
7fd59977 20#include <Standard_ProgramError.hxx>
d9ff84e8 21#include <TCollection_ExtendedString.hxx>
742cc8b0 22#include <NCollection_String.hxx>
7fd59977 23
cda06ac0 24#ifdef _WIN32
68df8478 25 #include <OSD_WNT.hxx>
cda06ac0 26 #include <stdio.h>
7fd59977 27
cda06ac0 28 #ifndef _INC_TCHAR
29 #include <tchar.h>
30 #endif
7fd59977 31
cda06ac0 32 void _osd_wnt_set_error (OSD_Error&, OSD_WhoAmI, ... );
7fd59977 33#else
cda06ac0 34 #include <errno.h>
35 #include <stdio.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
7fd59977 38
cda06ac0 39 const OSD_WhoAmI Iam = OSD_WDirectory;
40#endif
7fd59977 41
cda06ac0 42// =======================================================================
43// function : OSD_Directory
44// purpose :
45// =======================================================================
46OSD_Directory::OSD_Directory()
47{
48 //
49}
7fd59977 50
cda06ac0 51// =======================================================================
52// function : OSD_Directory
53// purpose :
54// =======================================================================
55OSD_Directory::OSD_Directory (const OSD_Path& theName)
56: OSD_FileNode (theName)
57{
58 //
59}
7fd59977 60
cda06ac0 61// =======================================================================
62// function : Build
63// purpose :
64// =======================================================================
65void OSD_Directory::Build (const OSD_Protection& theProtect)
66{
67#ifdef _WIN32
68 TCollection_AsciiString aDirName;
69 myPath.SystemName (aDirName);
70 if (aDirName.IsEmpty())
71 {
72 throw Standard_ProgramError ( "OSD_Directory::Build(): incorrect call - no directory name");
73 }
73594f72 74
75 Standard_Boolean isOK = Exists();
cda06ac0 76 if (!isOK)
73594f72 77 {
78 // myError will be set to fail by Exists() if intermediate dirs do not exist
79 myError.Reset();
80
81 // create directory if it does not exist;
cda06ac0 82 TCollection_ExtendedString aDirNameW (aDirName);
83 if (CreateDirectoryW (aDirNameW.ToWideString(), NULL))
73594f72 84 {
85 isOK = Standard_True;
86 }
87 // if failed due to absence of intermediate directories, create them recursively
88 else if (GetLastError() == ERROR_PATH_NOT_FOUND)
89 {
90 OSD_Path aSupPath = myPath;
91 aSupPath.UpTrek();
92 aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
93 OSD_Directory aSupDir (aSupPath);
cda06ac0 94 aSupDir.Build (theProtect);
73594f72 95 if (aSupDir.Failed())
96 {
97 myError = aSupDir.myError;
98 return;
99 }
cda06ac0 100 isOK = (CreateDirectoryW (aDirNameW.ToWideString(), NULL) != 0);
73594f72 101 }
102 }
103
104 if (isOK)
105 {
742cc8b0 106#ifndef OCCT_UWP
cda06ac0 107 SetProtection (theProtect);
742cc8b0 108#else
cda06ac0 109 (void)theProtect;
742cc8b0 110#endif
73594f72 111 }
112 else
113 {
cda06ac0 114 _osd_wnt_set_error (myError, OSD_WDirectory);
73594f72 115 }
cda06ac0 116#else
117 TCollection_AsciiString aBuffer;
118 mode_t anInternalProt = (mode_t )theProtect.Internal();
119 myPath.SystemName (aBuffer);
120 umask (0);
121 int aStatus = mkdir (aBuffer.ToCString(), anInternalProt);
122 if (aStatus == -1 && errno == ENOENT)
123 {
124 OSD_Path aSupPath = myPath;
125 aSupPath.UpTrek();
126 aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
127 OSD_Directory aSupDir (aSupPath);
128 aSupDir.Build (theProtect);
129 if (aSupDir.Failed())
130 {
131 myError = aSupDir.myError;
132 return;
133 }
134 aStatus = mkdir (aBuffer.ToCString(), anInternalProt);
135 }
136 if (aStatus == -1 && errno != EEXIST)
137 {
138 char anErrMsg[2048];
139 Sprintf (anErrMsg, "OSD_Directory::Build Directory \"%.2000s\"", aBuffer.ToCString());
140 myError.SetValue (errno, Iam, anErrMsg);
141 }
142#endif
143}
7fd59977 144
cda06ac0 145// =======================================================================
146// function : BuildTemporary
147// purpose :
148// =======================================================================
149OSD_Directory OSD_Directory::BuildTemporary()
150{
151#ifdef _WIN32
152 wchar_t* aTmpNameW = _wtmpnam (NULL);
153 if (aTmpNameW == NULL)
154 {
155 return OSD_Directory();
156 }
7fd59977 157
cda06ac0 158 TCollection_AsciiString aTmpName (aTmpNameW);
159 OSD_Path aDirPath (aTmpName);
160 OSD_Directory aDir;
161 aDir.SetPath (aDirPath);
162 aDir.Build (OSD_Protection());
163 return aDir;
164#else
165 // create a temporary directory with 0700 permissions
166 char aTmpName[] = "/tmp/CSFXXXXXX";
167 if (NULL == mkdtemp (aTmpName))
168 {
169 return OSD_Directory(); // can't create a directory
170 }
7fd59977 171
cda06ac0 172 unlink (aTmpName); // destroys link but directory still exists while current process lives
173 OSD_Directory aDir;
174 aDir.SetPath (TCollection_AsciiString (aTmpName));
175 return aDir;
7fd59977 176#endif
cda06ac0 177}