1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
17 #include <OSD_Directory.hxx>
18 #include <OSD_Path.hxx>
19 #include <OSD_Protection.hxx>
20 #include <OSD_WhoAmI.hxx>
26 const OSD_WhoAmI Iam = OSD_WDirectory;
29 OSD_Directory::OSD_Directory():OSD_FileNode() {
32 OSD_Directory::OSD_Directory(const OSD_Path& Name):OSD_FileNode(Name){
35 // Create physically a directory
37 void OSD_Directory::Build(const OSD_Protection& Protect){
39 Standard_Integer internal_prot;
40 TCollection_AsciiString aBuffer;
42 internal_prot = Protect.Internal();
43 myPath.SystemName(aBuffer);
45 status = mkdir (aBuffer.ToCString(), (mode_t)internal_prot);
46 if (status == -1 && errno == ENOENT)
48 OSD_Path aSupPath = myPath;
50 aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
51 OSD_Directory aSupDir (aSupPath);
52 aSupDir.Build (Protect);
55 myError = aSupDir.myError;
58 status = mkdir (aBuffer.ToCString(), (mode_t)internal_prot);
60 if (status == -1 && errno != EEXIST) {
61 Standard_Character err_message[2048];
62 Sprintf (err_message, "OSD_Directory::Build Directory \"%.2000s\"",
64 myError.SetValue (errno, Iam, err_message);
68 OSD_Directory OSD_Directory::BuildTemporary(){
69 OSD_Directory aDirectoryToReturn;
70 char name[] = "/tmp/CSFXXXXXX";
72 // Create a temporary directory with 0700 permissions.
73 if (NULL == mkdtemp( name ))
74 return aDirectoryToReturn; // Can't create a directory
76 unlink(name);//Destroys link but directory still exists while
77 //current process lives.
79 TCollection_AsciiString aString (name);
80 aDirectoryToReturn.SetPath ( aString );
81 return aDirectoryToReturn;
87 //------------------------------------------------------------------------
88 //------------------- WNT Sources of OSD_Diretory -----------------------
89 //------------------------------------------------------------------------
91 #include <OSD_Directory.hxx>
92 #include <OSD_Protection.hxx>
93 #include <Standard_ProgramError.hxx>
94 #include <TCollection_ExtendedString.hxx>
95 #include <NCollection_String.hxx>
97 #include <OSD_WNT_1.hxx>
106 # define tctmpnam _wtmpnam
108 # define tctmpnam tmpnam
111 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
113 OSD_Directory :: OSD_Directory () {
115 } // end constructor ( 1 )
117 OSD_Directory :: OSD_Directory ( const OSD_Path& Name ) :
118 OSD_FileNode ( Name ) {
120 } // end constructor ( 2 )
122 void OSD_Directory :: Build (const OSD_Protection& Protect) {
124 TCollection_AsciiString dirName;
126 myPath.SystemName ( dirName );
128 if ( dirName.IsEmpty () )
130 throw Standard_ProgramError ( "OSD_Directory :: Build (): incorrect call - no directory name");
132 Standard_Boolean isOK = Exists();
135 // myError will be set to fail by Exists() if intermediate dirs do not exist
138 // create directory if it does not exist;
139 TCollection_ExtendedString dirNameW(dirName);
140 if (CreateDirectoryW (dirNameW.ToWideString(), NULL))
142 isOK = Standard_True;
144 // if failed due to absence of intermediate directories, create them recursively
145 else if (GetLastError() == ERROR_PATH_NOT_FOUND)
147 OSD_Path aSupPath = myPath;
149 aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
150 OSD_Directory aSupDir (aSupPath);
151 aSupDir.Build (Protect);
152 if (aSupDir.Failed())
154 myError = aSupDir.myError;
157 isOK = (CreateDirectoryW (dirNameW.ToWideString(), NULL) != 0);
164 SetProtection(Protect);
171 _osd_wnt_set_error ( myError, OSD_WDirectory );
173 } // end OSD_Directory :: Build
175 OSD_Directory OSD_Directory :: BuildTemporary () {
177 OSD_Directory retVal;
180 wchar_t* aName = _wtmpnam(NULL);
181 OSD_Path dirPath (TCollection_AsciiString (aName != NULL ? aName : L""));
183 retVal.SetPath ( dirPath );
184 retVal.Build ( prt );
188 } // end OSD_Directory :: BuildTemporary