summaryrefslogtreecommitdiffstats
path: root/gfx/vr/service/openvr/src/dirtools_public.cpp
blob: e5cfc02e0c42ade8047b26c0847ddf308c24bb38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//========= Copyright Valve Corporation ============//
#include "dirtools_public.h"
#include "strtools_public.h"
#include "pathtools_public.h"

#include <errno.h>
#include <string.h>

#ifdef _WIN32
#include "windows.h"
#else
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif

#if defined( OSX )
#include <sys/syslimits.h>
#endif


//-----------------------------------------------------------------------------
// Purpose: utility function to create dirs & subdirs
//-----------------------------------------------------------------------------
bool BCreateDirectoryRecursive( const char *pchPath )
{
	// Does it already exist?
	if ( Path_IsDirectory( pchPath ) )
		return true;

	// copy the path into something we can munge
	int len = (int)strlen( pchPath );
	char *path = (char *)malloc( len + 1 );
	strcpy( path, pchPath );

	// Walk backwards to first non-existing dir that we find
	char *s = path + len - 1;

	const char slash = Path_GetSlash();
	while ( s > path )
	{
		if ( *s == slash )
		{
			*s = '\0';
			bool bExists = Path_IsDirectory( path );
			*s = slash;

			if ( bExists )
			{
				++s;
				break;
			}
		}
		--s;
	}

	// and then move forwards from there

	while ( *s )
	{
		if ( *s == slash )
		{
			*s = '\0';
			BCreateDirectory( path );
			*s = slash;
		}
		s++;
	}

	bool bRetVal = BCreateDirectory( path );
	free( path );
	return bRetVal;
}


//-----------------------------------------------------------------------------
// Purpose: Creates the directory, returning true if it is created, or if it already existed
//-----------------------------------------------------------------------------
bool BCreateDirectory( const char *pchPath )
{
#ifdef WIN32
	std::wstring wPath = UTF8to16( pchPath );
	if ( ::CreateDirectoryW( wPath.c_str(), NULL ) )
		return true;

	if ( ::GetLastError() == ERROR_ALREADY_EXISTS )
		return true;

	return false;
#else
	int i = mkdir( pchPath, S_IRWXU | S_IRWXG | S_IRWXO );
	if ( i == 0 )
		return true;
	if ( errno == EEXIST )
		return true;

	return false;
#endif
}