This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
forked from couchbase/php-ext-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccache.c
103 lines (94 loc) · 2.92 KB
/
ccache.c
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
102
103
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright 2012 Couchbase, Inc. |
+----------------------------------------------------------------------+
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| https://www.apache.org/licenses/LICENSE-2.0 |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| permissions and limitations under the License. |
+----------------------------------------------------------------------+
*/
#include "internal.h"
#include <errno.h>
static int is_working(const char *path)
{
char buffer[1024];
FILE *fp;
snprintf(buffer, sizeof(buffer), "%s/phpcache.test", path);
fp = fopen(buffer, "w");
if (fp != NULL) {
fclose(fp);
remove(buffer);
return 1;
}
return 0;
}
int try_setup_cache_dir(const char *path, char **emsg)
{
free(*emsg);
*emsg = NULL;
#ifdef _WIN32
if (!CreateDirectory(path, NULL)) {
DWORD err = GetLastError();
if (err != ERROR_ALREADY_EXISTS) {
char errmsg[1024];
LPVOID message;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, 0, (LPTSTR)&message,
0, NULL) != 0) {
snprintf(errmsg, sizeof(errmsg),
"Failed to create cache directory \"%s\": \"%s\"."
"\nFix this and try again"
" extension", path, message);
LocalFree(message);
} else {
snprintf(errmsg, sizeof(errmsg),
"Failed to create cache directory \"%s\".\nFix "
"this and try again",
path, strerror(errno));
}
*emsg = strdup(errmsg);
return -1;
}
}
#else
if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP) == -1) {
if (errno != EEXIST) {
char errmsg[1024];
snprintf(errmsg, sizeof(errmsg),
"Failed to create cache directory \"%s\": \"%s\".\nFix "
"this and try again",
path, strerror(errno));
*emsg = strdup(errmsg);
return -1;
}
}
#endif
if (is_working(path) == 0) {
char errmsg[1024];
snprintf(errmsg, sizeof(errmsg),
"Missing write permission to \"%s\".\nFix "
"this and try to reload the Couchbase PHP extension",
path);
*emsg = strdup(errmsg);
return -1;
}
return 0;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/