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 | |
57c28b61 |
15 | #ifndef _WIN32 |
7fd59977 |
16 | |
42cf5bc1 |
17 | #include <OSD_Directory.hxx> |
18 | #include <OSD_Path.hxx> |
7fd59977 |
19 | #include <OSD_Protection.hxx> |
42cf5bc1 |
20 | #include <OSD_WhoAmI.hxx> |
7fd59977 |
21 | |
7fd59977 |
22 | #include <errno.h> |
7fd59977 |
23 | #include <stdio.h> |
42cf5bc1 |
24 | #include <sys/stat.h> |
03155c18 |
25 | #include <unistd.h> |
7fd59977 |
26 | const OSD_WhoAmI Iam = OSD_WDirectory; |
27 | |
28 | |
29 | OSD_Directory::OSD_Directory():OSD_FileNode() { |
30 | } |
31 | |
32 | OSD_Directory::OSD_Directory(const OSD_Path& Name):OSD_FileNode(Name){ |
33 | } |
34 | |
35 | // Create physically a directory |
36 | |
37 | void OSD_Directory::Build(const OSD_Protection& Protect){ |
38 | int status; |
39 | Standard_Integer internal_prot; |
40 | TCollection_AsciiString aBuffer; |
41 | |
42 | internal_prot = Protect.Internal(); |
43 | myPath.SystemName(aBuffer); |
44 | umask ( 0 ); |
45 | status = mkdir (aBuffer.ToCString(), (mode_t)internal_prot); |
73594f72 |
46 | if (status == -1 && errno == ENOENT) |
47 | { |
48 | OSD_Path aSupPath = myPath; |
49 | aSupPath.UpTrek(); |
50 | aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required! |
51 | OSD_Directory aSupDir (aSupPath); |
52 | aSupDir.Build (Protect); |
53 | if (aSupDir.Failed()) |
54 | { |
55 | myError = aSupDir.myError; |
56 | return; |
57 | } |
58 | status = mkdir (aBuffer.ToCString(), (mode_t)internal_prot); |
59 | } |
60 | if (status == -1 && errno != EEXIST) { |
61 | Standard_Character err_message[2048]; |
62 | Sprintf (err_message, "OSD_Directory::Build Directory \"%.2000s\"", |
7fd59977 |
63 | aBuffer.ToCString()); |
73594f72 |
64 | myError.SetValue (errno, Iam, err_message); |
65 | } |
7fd59977 |
66 | } |
67 | |
68 | OSD_Directory OSD_Directory::BuildTemporary(){ |
7fd59977 |
69 | OSD_Directory aDirectoryToReturn; |
95e05159 |
70 | char name[] = "/tmp/CSFXXXXXX"; |
7fd59977 |
71 | |
95e05159 |
72 | // Create a temporary directory with 0700 permissions. |
73 | if (NULL == mkdtemp( name )) |
74 | return aDirectoryToReturn; // Can't create a directory |
75 | |
76 | unlink(name);//Destroys link but directory still exists while |
7fd59977 |
77 | //current process lives. |
78 | |
95e05159 |
79 | TCollection_AsciiString aString (name); |
7fd59977 |
80 | aDirectoryToReturn.SetPath ( aString ); |
81 | return aDirectoryToReturn; |
82 | |
83 | } |
84 | |
85 | #else |
86 | |
87 | //------------------------------------------------------------------------ |
88 | //------------------- WNT Sources of OSD_Diretory ----------------------- |
89 | //------------------------------------------------------------------------ |
90 | |
91 | #include <OSD_Directory.hxx> |
92 | #include <OSD_Protection.hxx> |
93 | #include <Standard_ProgramError.hxx> |
d9ff84e8 |
94 | #include <TCollection_ExtendedString.hxx> |
742cc8b0 |
95 | #include <NCollection_String.hxx> |
7fd59977 |
96 | |
97 | #include <OSD_WNT_1.hxx> |
98 | |
99 | #include <stdio.h> |
100 | |
101 | #ifndef _INC_TCHAR |
102 | # include <tchar.h> |
103 | #endif // _INC_TCHAR |
104 | |
105 | #ifdef _UNICODE |
106 | # define tctmpnam _wtmpnam |
107 | #else |
108 | # define tctmpnam tmpnam |
109 | #endif // _UNICODE |
110 | |
111 | void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... ); |
112 | |
113 | OSD_Directory :: OSD_Directory () { |
114 | |
115 | } // end constructor ( 1 ) |
116 | |
117 | OSD_Directory :: OSD_Directory ( const OSD_Path& Name ) : |
118 | OSD_FileNode ( Name ) { |
119 | |
120 | } // end constructor ( 2 ) |
121 | |
742cc8b0 |
122 | void OSD_Directory :: Build (const OSD_Protection& Protect) { |
7fd59977 |
123 | |
124 | TCollection_AsciiString dirName; |
125 | |
126 | myPath.SystemName ( dirName ); |
127 | |
128 | if ( dirName.IsEmpty () ) |
129 | |
742cc8b0 |
130 | Standard_ProgramError :: Raise ( "OSD_Directory :: Build (): incorrect call - no directory name"); |
73594f72 |
131 | |
132 | Standard_Boolean isOK = Exists(); |
133 | if (! isOK) |
134 | { |
135 | // myError will be set to fail by Exists() if intermediate dirs do not exist |
136 | myError.Reset(); |
137 | |
138 | // create directory if it does not exist; |
139 | TCollection_ExtendedString dirNameW(dirName); |
140 | if (CreateDirectoryW (dirNameW.ToWideString(), NULL)) |
141 | { |
142 | isOK = Standard_True; |
143 | } |
144 | // if failed due to absence of intermediate directories, create them recursively |
145 | else if (GetLastError() == ERROR_PATH_NOT_FOUND) |
146 | { |
147 | OSD_Path aSupPath = myPath; |
148 | aSupPath.UpTrek(); |
149 | aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required! |
150 | OSD_Directory aSupDir (aSupPath); |
151 | aSupDir.Build (Protect); |
152 | if (aSupDir.Failed()) |
153 | { |
154 | myError = aSupDir.myError; |
155 | return; |
156 | } |
157 | isOK = (CreateDirectoryW (dirNameW.ToWideString(), NULL) != 0); |
158 | } |
159 | } |
160 | |
161 | if (isOK) |
162 | { |
742cc8b0 |
163 | #ifndef OCCT_UWP |
73594f72 |
164 | SetProtection(Protect); |
742cc8b0 |
165 | #else |
73594f72 |
166 | (void)Protect; |
742cc8b0 |
167 | #endif |
73594f72 |
168 | } |
169 | else |
170 | { |
171 | _osd_wnt_set_error ( myError, OSD_WDirectory ); |
172 | } |
7fd59977 |
173 | } // end OSD_Directory :: Build |
174 | |
175 | OSD_Directory OSD_Directory :: BuildTemporary () { |
176 | |
177 | OSD_Directory retVal; |
178 | OSD_Protection prt; |
742cc8b0 |
179 | |
180 | wchar_t* aName = _wtmpnam(NULL); |
fb0b0531 |
181 | OSD_Path dirPath (TCollection_AsciiString (aName != NULL ? aName : L"")); |
7fd59977 |
182 | |
183 | retVal.SetPath ( dirPath ); |
184 | retVal.Build ( prt ); |
185 | |
186 | return retVal; |
187 | |
188 | } // end OSD_Directory :: BuildTemporary |
189 | |
190 | #endif |