Skip to content

Commit 883bd2b

Browse files
committed
- Fix virtual cwd bug
- Add more V_STAT() V_LSTAT() changes
1 parent 7412bd5 commit 883bd2b

File tree

11 files changed

+52
-16
lines changed

11 files changed

+52
-16
lines changed

ext/db/db.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ dbm_info *php_dbm_open(char *filename, char *mode) {
319319
strcat(lockfn, ".lck");
320320

321321
#if NFS_HACK
322-
while((last_try = stat(lockfn,&sb))==0) {
322+
while((last_try = V_STAT(lockfn,&sb))==0) {
323323
retries++;
324324
php_sleep(1);
325325
if (retries>30) break;

ext/dba/dba_db2.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ DBA_OPEN_FUNC(db2)
6161

6262
type = info->mode == DBA_READER ? DB_UNKNOWN :
6363
info->mode == DBA_TRUNC ? DB_BTREE :
64-
stat(info->path, &check_stat) ? DB_BTREE : DB_UNKNOWN;
64+
V_STAT(info->path, &check_stat) ? DB_BTREE : DB_UNKNOWN;
6565

6666
gmode = info->mode == DBA_READER ? DB_RDONLY :
6767
info->mode == DBA_CREAT ? DB_CREATE :

ext/dba/dba_db3.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ DBA_OPEN_FUNC(db3)
6161

6262
type = info->mode == DBA_READER ? DB_UNKNOWN :
6363
info->mode == DBA_TRUNC ? DB_BTREE :
64-
stat(info->path, &check_stat) ? DB_BTREE : DB_UNKNOWN;
64+
V_STAT(info->path, &check_stat) ? DB_BTREE : DB_UNKNOWN;
6565

6666
gmode = info->mode == DBA_READER ? DB_RDONLY :
6767
info->mode == DBA_CREAT ? DB_CREATE :

ext/session/mod_files.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static int _ps_files_cleanup_dir(const char *dirname, int maxlifetime)
175175
snprintf(buf, MAXPATHLEN, "%s%c%s", dirname, DIR_DELIMITER,
176176
entry->d_name) > 0 &&
177177
/* stat the directory entry */
178-
stat(buf, &sbuf) == 0 &&
178+
V_STAT(buf, &sbuf) == 0 &&
179179
/* is it expired? */
180180
(now - sbuf.st_atime) > maxlifetime) {
181181
unlink(buf);

ext/session/session.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static void last_modified(void)
497497

498498
path = SG(request_info).path_translated;
499499
if (path) {
500-
if (stat(path, &sb) == -1) {
500+
if (V_STAT(path, &sb) == -1) {
501501
return;
502502
}
503503

ext/standard/filestat.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ PHP_FUNCTION(touch)
385385
RETURN_FALSE;
386386

387387
/* create the file if it doesn't exist already */
388-
ret = stat((*filename)->value.str.val, &sb);
388+
ret = V_STAT((*filename)->value.str.val, &sb);
389389
if (ret == -1) {
390390
file = V_FOPEN((*filename)->value.str.val, "w");
391391
if (file == NULL) {
@@ -439,7 +439,7 @@ static void php_stat(const char *filename, int type, pval *return_value)
439439
#if HAVE_SYMLINK
440440
BG(lsb).st_mode = 0; /* mark lstat buf invalid */
441441
#endif
442-
if (stat(BG(CurrentStatFile),&BG(sb))==-1) {
442+
if (V_STAT(BG(CurrentStatFile),&BG(sb))==-1) {
443443
if (type != 15 || errno != ENOENT) { /* fileexists() test must print no error */
444444
php_error(E_NOTICE,"stat failed for %s (errno=%d - %s)",BG(CurrentStatFile),errno,strerror(errno));
445445
}
@@ -457,7 +457,7 @@ static void php_stat(const char *filename, int type, pval *return_value)
457457
/* do lstat if the buffer is empty */
458458

459459
if (!BG(lsb).st_mode) {
460-
if (lstat(BG(CurrentStatFile),&BG(lsb)) == -1) {
460+
if (V_LSTAT(BG(CurrentStatFile),&BG(lsb)) == -1) {
461461
php_error(E_NOTICE,"lstat failed for %s (errno=%d - %s)",BG(CurrentStatFile),errno,strerror(errno));
462462
RETURN_FALSE;
463463
}

ext/standard/link.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ PHP_FUNCTION(linkinfo)
8888
}
8989
convert_to_string_ex(filename);
9090

91-
ret = lstat((*filename)->value.str.val, &sb);
91+
ret = V_LSTAT((*filename)->value.str.val, &sb);
9292
if (ret == -1) {
9393
php_error(E_WARNING, "LinkInfo failed (%s)", strerror(errno));
9494
RETURN_LONG(-1L);

ext/zlib/zlib.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static gzFile *php_gzopen_with_path(char *filename, char *mode, char *path, char
206206
if(PG(doc_root)) {
207207
snprintf(trypath, MAXPATHLEN, "%s%s", PG(doc_root), filename);
208208
} else {
209-
strncpy(trypath,filename,sizeof(trypath));
209+
strlcpy(trypath,filename,sizeof(trypath));
210210
}
211211
if (!php_checkuid(trypath,2)) {
212212
return(NULL);
@@ -251,7 +251,7 @@ static gzFile *php_gzopen_with_path(char *filename, char *mode, char *path, char
251251
}
252252
snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename);
253253
if (PG(safe_mode)) {
254-
if (stat(trypath,&sb) == 0 &&(!php_checkuid(trypath,2))) {
254+
if (V_STAT(trypath,&sb) == 0 &&(!php_checkuid(trypath,2))) {
255255
efree(pathbuf);
256256
return(NULL);
257257
}

main/php.h

+2
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,15 @@ PHPAPI int cfg_get_string(char *varname, char **result);
295295
#define V_CHDIR_FILE(path) virtual_chdir_file(path)
296296
#define V_GETWD(buf)
297297
#define V_STAT(path, buff) virtual_stat(path, buff)
298+
#define V_LSTAT(path, buff) virtual_lstat(path, buff)
298299
#else
299300
#define V_GETCWD(buff, size) getcwd(buff,size)
300301
#define V_FOPEN(path, mode) fopen(path, mode)
301302
#define V_CHDIR(path) chdir(path)
302303
#define V_CHDIR_FILE(path) chdir_file(path)
303304
#define V_GETWD(buf) getwd(buf)
304305
#define V_STAT(path, buff) stat(path, buff)
306+
#define V_LSTAT(path, buff) lstat(path, buff)
305307
#endif
306308

307309
#include "zend_constants.h"

main/php_virtual_cwd.c

+38-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
#include <stdlib.h>
88
#include <ctype.h>
99

10-
1110
#include "php_virtual_cwd.h"
1211

12+
#define VIRTUAL_CWD_DEBUG 0
13+
1314
#ifdef ZTS
1415
#include "TSRM.h"
1516
#endif
@@ -148,6 +149,9 @@ CWD_API void virtual_cwd_startup()
148149

149150
CWD_API void virtual_cwd_activate(char *filename)
150151
{
152+
#if VIRTUAL_CWD_DEBUG
153+
fprintf(stderr, "Changing dir to %s\n", filename);
154+
#endif
151155
if (filename) {
152156
virtual_chdir_file(filename);
153157
}
@@ -238,7 +242,9 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
238242

239243
old_state = (cwd_state *) malloc(sizeof(cwd_state));
240244
CWD_STATE_COPY(old_state, state);
241-
245+
#if VIRTUAL_CWD_DEBUG
246+
fprintf(stderr,"cwd = %s path = %s\n", state->cwd, path);
247+
#endif
242248
if (IS_ABSOLUTE_PATH(path_copy, path_length)) {
243249
copy_amount = COPY_WHEN_ABSOLUTE;
244250
is_absolute = 1;
@@ -310,7 +316,9 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
310316
free(old_state);
311317

312318
efree(free_path);
313-
319+
#if VIRTUAL_CWD_DEBUG
320+
fprintf (stderr, "virtual_file_ex() = %s\n",state->cwd);
321+
#endif
314322
return (ret);
315323
}
316324

@@ -324,6 +332,8 @@ CWD_API int virtual_chdir(char *path)
324332
CWD_API int virtual_chdir_file(char *path)
325333
{
326334
int length = strlen(path);
335+
char *temp;
336+
int retval;
327337

328338
if (length == 0) {
329339
return 1; /* Can't cd to empty string */
@@ -334,8 +344,16 @@ CWD_API int virtual_chdir_file(char *path)
334344
if (length == -1) {
335345
return virtual_chdir(path);
336346
}
337-
path[length] = DEFAULT_SLASH;
338-
return virtual_chdir(path);
347+
348+
temp = (char *) malloc(length+1);
349+
memcpy(temp, path, length);
350+
temp[length] = 0;
351+
#if VIRTUAL_CWD_DEBUG
352+
fprintf (stderr, "Changing directory to %s\n", temp);
353+
#endif
354+
retval = virtual_chdir(temp);
355+
free(temp);
356+
return retval;
339357
}
340358

341359

@@ -382,6 +400,21 @@ CWD_API int virtual_stat(const char *path, struct stat *buf)
382400
return retval;
383401
}
384402

403+
CWD_API int virtual_lstat(const char *path, struct stat *buf)
404+
{
405+
cwd_state new_state;
406+
int retval;
407+
CWDLS_FETCH();
408+
409+
CWD_STATE_COPY(&new_state, &CWDG(cwd));
410+
411+
virtual_file_ex(&new_state, path, NULL);
412+
413+
retval = lstat(new_state.cwd, buf);
414+
CWD_STATE_FREE(&new_state);
415+
return retval;
416+
}
417+
385418
#if 0
386419

387420
main(void)

main/php_virtual_cwd.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ CWD_API int virtual_chdir_file(char *path);
4343
CWD_API int virtual_filepath(char *path, char **filepath);
4444
CWD_API FILE *virtual_fopen(const char *path, const char *mode);
4545
CWD_API int virtual_stat(const char *path, struct stat *buf);
46+
CWD_API int virtual_lstat(const char *path, struct stat *buf);
4647
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path);
4748

4849
ZEND_BEGIN_MODULE_GLOBALS(cwd)

0 commit comments

Comments
 (0)