Line data Source code
1 : //========= Copyright Valve Corporation ============//
2 : #include "dirtools_public.h"
3 : #include "strtools_public.h"
4 : #include "pathtools_public.h"
5 :
6 : #include <errno.h>
7 : #include <string.h>
8 :
9 : #ifdef _WIN32
10 : #include "windows.h"
11 : #else
12 : #include <stdlib.h>
13 : #include <stdio.h>
14 : #include <sys/stat.h>
15 : #include <sys/types.h>
16 : #endif
17 :
18 : #if defined( OSX )
19 : #include <sys/syslimits.h>
20 : #endif
21 :
22 :
23 : //-----------------------------------------------------------------------------
24 : // Purpose: utility function to create dirs & subdirs
25 : //-----------------------------------------------------------------------------
26 0 : bool BCreateDirectoryRecursive( const char *pchPath )
27 : {
28 : // Does it already exist?
29 0 : if ( Path_IsDirectory( pchPath ) )
30 0 : return true;
31 :
32 : // copy the path into something we can munge
33 0 : int len = (int)strlen( pchPath );
34 0 : char *path = (char *)malloc( len + 1 );
35 0 : strcpy( path, pchPath );
36 :
37 : // Walk backwards to first non-existing dir that we find
38 0 : char *s = path + len - 1;
39 :
40 0 : const char slash = Path_GetSlash();
41 0 : while ( s > path )
42 : {
43 0 : if ( *s == slash )
44 : {
45 0 : *s = '\0';
46 0 : bool bExists = Path_IsDirectory( path );
47 0 : *s = slash;
48 :
49 0 : if ( bExists )
50 : {
51 0 : ++s;
52 0 : break;
53 : }
54 : }
55 0 : --s;
56 : }
57 :
58 : // and then move forwards from there
59 :
60 0 : while ( *s )
61 : {
62 0 : if ( *s == slash )
63 : {
64 0 : *s = '\0';
65 0 : BCreateDirectory( path );
66 0 : *s = slash;
67 : }
68 0 : s++;
69 : }
70 :
71 0 : bool bRetVal = BCreateDirectory( path );
72 0 : free( path );
73 0 : return bRetVal;
74 : }
75 :
76 :
77 : //-----------------------------------------------------------------------------
78 : // Purpose: Creates the directory, returning true if it is created, or if it already existed
79 : //-----------------------------------------------------------------------------
80 0 : bool BCreateDirectory( const char *pchPath )
81 : {
82 : #ifdef WIN32
83 : std::wstring wPath = UTF8to16( pchPath );
84 : if ( ::CreateDirectoryW( wPath.c_str(), NULL ) )
85 : return true;
86 :
87 : if ( ::GetLastError() == ERROR_ALREADY_EXISTS )
88 : return true;
89 :
90 : return false;
91 : #else
92 0 : int i = mkdir( pchPath, S_IRWXU | S_IRWXG | S_IRWXO );
93 0 : if ( i == 0 )
94 0 : return true;
95 0 : if ( errno == EEXIST )
96 0 : return true;
97 :
98 0 : return false;
99 : #endif
100 : }
101 :
|