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_1.hxx>
32 void _osd_wnt_set_error (OSD_Error&, OSD_WhoAmI, ... );
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);
117 TCollection_AsciiString aBuffer;
118 mode_t anInternalProt = (mode_t )theProtect.Internal();
119 myPath.SystemName (aBuffer);
121 int aStatus = mkdir (aBuffer.ToCString(), anInternalProt);
122 if (aStatus == -1 && errno == ENOENT)
124 OSD_Path aSupPath = myPath;
126 aSupPath.SetName (myPath.TrekValue (myPath.TrekLength())); // incredible, but required!
127 OSD_Directory aSupDir (aSupPath);
128 aSupDir.Build (theProtect);
129 if (aSupDir.Failed())
131 myError = aSupDir.myError;
134 aStatus = mkdir (aBuffer.ToCString(), anInternalProt);
136 if (aStatus == -1 && errno != EEXIST)
139 Sprintf (anErrMsg, "OSD_Directory::Build Directory \"%.2000s\"", aBuffer.ToCString());
140 myError.SetValue (errno, Iam, anErrMsg);
145 // =======================================================================
146 // function : BuildTemporary
148 // =======================================================================
149 OSD_Directory OSD_Directory::BuildTemporary()
152 wchar_t* aTmpNameW = _wtmpnam (NULL);
153 if (aTmpNameW == NULL)
155 return OSD_Directory();
158 TCollection_AsciiString aTmpName (aTmpNameW);
159 OSD_Path aDirPath (aTmpName);
161 aDir.SetPath (aDirPath);
162 aDir.Build (OSD_Protection());
165 // create a temporary directory with 0700 permissions
166 char aTmpName[] = "/tmp/CSFXXXXXX";
167 if (NULL == mkdtemp (aTmpName))
169 return OSD_Directory(); // can't create a directory
172 unlink (aTmpName); // destroys link but directory still exists while current process lives
174 aDir.SetPath (TCollection_AsciiString (aTmpName));