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.
15 #include <OSD_Directory.hxx>
17 #include <OSD_Path.hxx>
18 #include <OSD_Protection.hxx>
19 #include <OSD_WhoAmI.hxx>
20 #include <Standard_ProgramError.hxx>
21 #include <TCollection_ExtendedString.hxx>
22 #include <NCollection_String.hxx>
25 #include <OSD_WNT.hxx>
32 void _osd_wnt_set_error (OSD_Error&, Standard_Integer, ... );
39 const OSD_WhoAmI Iam = OSD_WDirectory;
42 // =======================================================================
43 // function : OSD_Directory
45 // =======================================================================
46 OSD_Directory::OSD_Directory()
51 // =======================================================================
52 // function : OSD_Directory
54 // =======================================================================
55 OSD_Directory::OSD_Directory (const OSD_Path& theName)
56 : OSD_FileNode (theName)
61 // =======================================================================
64 // =======================================================================
65 void OSD_Directory::Build (const OSD_Protection& theProtect)
68 TCollection_AsciiString aDirName;
69 myPath.SystemName (aDirName);
70 if (aDirName.IsEmpty())
72 throw Standard_ProgramError ( "OSD_Directory::Build(): incorrect call - no directory name");
75 Standard_Boolean isOK = Exists();
78 // myError will be set to fail by Exists() if intermediate dirs do not exist
81 // create directory if it does not exist;
82 TCollection_ExtendedString aDirNameW (aDirName);
83 if (CreateDirectoryW (aDirNameW.ToWideString(), NULL))
87 // if failed due to absence of intermediate directories, create them recursively
88 else if (GetLastError() == ERROR_PATH_NOT_FOUND)
90 OSD_Path aSupPath = myPath;
92 aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
93 OSD_Directory aSupDir (aSupPath);
94 aSupDir.Build (theProtect);
97 myError = aSupDir.myError;
100 isOK = (CreateDirectoryW (aDirNameW.ToWideString(), NULL) != 0);
107 SetProtection (theProtect);
114 _osd_wnt_set_error (myError, OSD_WDirectory);
118 TCollection_AsciiString aBuffer;
119 mode_t anInternalProt = (mode_t )theProtect.Internal();
120 myPath.SystemName (aBuffer);
122 int aStatus = mkdir (aBuffer.ToCString(), anInternalProt);
123 if (aStatus == -1 && errno == ENOENT)
125 OSD_Path aSupPath = myPath;
127 aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
128 OSD_Directory aSupDir (aSupPath);
129 aSupDir.Build (theProtect);
130 if (aSupDir.Failed())
132 myError = aSupDir.myError;
135 aStatus = mkdir (aBuffer.ToCString(), anInternalProt);
137 if (aStatus == -1 && errno != EEXIST)
140 Sprintf (anErrMsg, "OSD_Directory::Build Directory \"%.2000s\"", aBuffer.ToCString());
141 myError.SetValue (errno, Iam, anErrMsg);
146 // =======================================================================
147 // function : BuildTemporary
149 // =======================================================================
150 OSD_Directory OSD_Directory::BuildTemporary()
153 wchar_t* aTmpNameW = _wtmpnam (NULL);
154 if (aTmpNameW == NULL)
156 return OSD_Directory();
159 TCollection_AsciiString aTmpName (aTmpNameW);
160 OSD_Path aDirPath (aTmpName);
162 aDir.SetPath (aDirPath);
163 aDir.Build (OSD_Protection());
166 // create a temporary directory with 0700 permissions
167 char aTmpName[] = "/tmp/CSFXXXXXX";
168 if (NULL == mkdtemp (aTmpName))
170 return OSD_Directory(); // can't create a directory
173 unlink (aTmpName); // destroys link but directory still exists while current process lives
175 aDir.SetPath (TCollection_AsciiString (aTmpName));