Skip to content

Commit 0548dbc

Browse files
committed
merge directory traversal loops
1 parent 49b45d7 commit 0548dbc

5 files changed

Lines changed: 33 additions & 35 deletions

File tree

ext/session/mod_files.c

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -302,39 +302,37 @@ static int ps_files_cleanup_dir(const zend_string *dirname, zend_long maxlifetim
302302

303303
if (remaining_depth == 0) {
304304
time(&now);
305-
while ((entry = readdir(dir))) {
306-
if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1)) {
307-
size_t entry_len = strlen(entry->d_name);
308-
if (entry_len + ZSTR_LEN(dirname) + 2 < MAXPATHLEN) {
309-
memcpy(buf + ZSTR_LEN(dirname) + 1, entry->d_name, entry_len);
310-
buf[ZSTR_LEN(dirname) + entry_len + 1] = '\0';
311-
if (VCWD_STAT(buf, &sbuf) == 0 &&
312-
(now - sbuf.st_mtime) > maxlifetime) {
313-
VCWD_UNLINK(buf);
314-
nrdels++;
315-
}
316-
}
317-
}
305+
}
306+
307+
while ((entry = readdir(dir))) {
308+
if (entry->d_name[0] == '.' &&
309+
(entry->d_name[1] == '\0' ||
310+
(entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) {
311+
continue;
318312
}
319-
} else {
320-
while ((entry = readdir(dir))) {
321-
if (entry->d_name[0] == '.' &&
322-
(entry->d_name[1] == '\0' ||
323-
(entry->d_name[1] == '.' && entry->d_name[2] == '\0'))) {
324-
continue;
313+
if (remaining_depth == 0 && strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1) != 0) {
314+
continue;
315+
}
316+
size_t entry_len = strlen(entry->d_name);
317+
if (ZSTR_LEN(dirname) + 1 + entry_len >= MAXPATHLEN) {
318+
continue;
319+
}
320+
memcpy(buf + ZSTR_LEN(dirname) + 1, entry->d_name, entry_len);
321+
buf[ZSTR_LEN(dirname) + 1 + entry_len] = '\0';
322+
if (VCWD_STAT(buf, &sbuf) != 0) {
323+
continue;
324+
}
325+
if (remaining_depth == 0) {
326+
if ((now - sbuf.st_mtime) > maxlifetime) {
327+
VCWD_UNLINK(buf);
328+
nrdels++;
325329
}
326-
size_t entry_len = strlen(entry->d_name);
327-
if (ZSTR_LEN(dirname) + 1 + entry_len < MAXPATHLEN) {
328-
memcpy(buf + ZSTR_LEN(dirname) + 1, entry->d_name, entry_len);
329-
buf[ZSTR_LEN(dirname) + 1 + entry_len] = '\0';
330-
if (VCWD_STAT(buf, &sbuf) == 0 && S_ISDIR(sbuf.st_mode)) {
331-
zend_string *subdir = zend_string_init(buf, ZSTR_LEN(dirname) + 1 + entry_len, 0);
332-
int n = ps_files_cleanup_dir(subdir, maxlifetime, remaining_depth - 1);
333-
zend_string_release(subdir);
334-
if (n >= 0) {
335-
nrdels += n;
336-
}
337-
}
330+
} else if (S_ISDIR(sbuf.st_mode)) {
331+
zend_string *subdir = zend_string_init(buf, ZSTR_LEN(dirname) + 1 + entry_len, 0);
332+
int n = ps_files_cleanup_dir(subdir, maxlifetime, remaining_depth - 1);
333+
zend_string_release(subdir);
334+
if (n >= 0) {
335+
nrdels += n;
338336
}
339337
}
340338
}

ext/session/tests/mod_files/gc_dirdepth2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ session GC cleans expired sessions with save_path dirdepth=2 (two subdir levels)
33
--EXTENSIONS--
44
session
55
--SKIPIF--
6-
<?php include('skipif.inc'); ?>
6+
<?php include(__DIR__ . '/../skipif.inc'); ?>
77
--INI--
88
session.gc_probability=0
99
session.gc_maxlifetime=10

ext/session/tests/mod_files/gc_dirdepth_disabled.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ session GC correctly cleans expired sessions when save_path dirdepth > 0
33
--EXTENSIONS--
44
session
55
--SKIPIF--
6-
<?php include('skipif.inc'); ?>
6+
<?php include(__DIR__ . '/../skipif.inc'); ?>
77
--INI--
88
session.gc_probability=0
99
session.gc_maxlifetime=1

ext/session/tests/mod_files/gc_dirdepth_multi_subdir_count.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ session GC accumulates correct total count across multiple subdirs, including em
33
--EXTENSIONS--
44
session
55
--SKIPIF--
6-
<?php include('skipif.inc'); ?>
6+
<?php include(__DIR__ . '/../skipif.inc'); ?>
77
--INI--
88
session.gc_probability=0
99
session.gc_maxlifetime=10

ext/session/tests/mod_files/gc_dirdepth_selective.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ session GC deletes only expired sess_* files and leaves all other files untouche
33
--EXTENSIONS--
44
session
55
--SKIPIF--
6-
<?php include('skipif.inc'); ?>
6+
<?php include(__DIR__ . '/../skipif.inc'); ?>
77
--INI--
88
session.gc_probability=0
99
session.gc_maxlifetime=10

0 commit comments

Comments
 (0)