Skip to content

Commit

Permalink
Add futil_mtime_compare function test
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Mar 21, 2013
1 parent 3d09037 commit d637ce0
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
76 changes: 72 additions & 4 deletions php_fileutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ static const zend_function_entry fileutil_functions[] = {
PHP_FE(futil_paths_filter_file, NULL)
PHP_FE(futil_lastmtime, arginfo_futil_lastmtime)
PHP_FE(futil_lastctime, arginfo_futil_lastctime)
PHP_FE(futil_mtime_compare, NULL)
PHP_FE(futil_ctime_compare, NULL)
PHP_FE(futil_unlink_if_exists, NULL)
PHP_FE(futil_rmdir_if_exists, NULL)
PHP_FE(futil_mkdir_if_not_exists, NULL)
Expand Down Expand Up @@ -283,6 +285,72 @@ PHP_FUNCTION(futil_scanpath)
}



PHP_FUNCTION(futil_ctime_compare)
{
char *filename1, *filename2;
int filename1_len, filename2_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
&filename1, &filename1_len, &filename2, &filename2_len
) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong parameters.");
RETURN_FALSE;
}
zval time1;
zval time2;
php_stat(filename1, filename1_len, FS_CTIME, &time1 TSRMLS_CC);
php_stat(filename2, filename2_len, FS_CTIME, &time2 TSRMLS_CC);

if (Z_LVAL(time1) > Z_LVAL(time2) ) {
RETURN_LONG(1);
} else if (Z_LVAL(time1) == Z_LVAL(time2)) {
RETURN_LONG(0);
} else if (Z_LVAL(time1) < Z_LVAL(time2) ) {
RETURN_LONG(-1);
}
RETURN_FALSE;

}

PHP_FUNCTION(futil_mtime_compare)
{
char *filename1, *filename2;
int filename1_len, filename2_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
&filename1, &filename1_len, &filename2, &filename2_len
) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong parameters.");
RETURN_FALSE;
}
zval time1;
zval time2;
php_stat(filename1, filename1_len, FS_MTIME, &time1 TSRMLS_CC);
php_stat(filename2, filename2_len, FS_MTIME, &time2 TSRMLS_CC);

if (Z_LVAL(time1) > Z_LVAL(time2) ) {
RETURN_LONG(1);
} else if (Z_LVAL(time1) == Z_LVAL(time2)) {
RETURN_LONG(0);
} else if (Z_LVAL(time1) < Z_LVAL(time2) ) {
RETURN_LONG(-1);
}
RETURN_FALSE;
}















PHP_FUNCTION(futil_lastctime)
{
zval *zarr;
Expand All @@ -293,7 +361,7 @@ PHP_FUNCTION(futil_lastctime)
long lastctime = 0;
char *path;
int path_len;
zval mtime;
zval time;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a",
&zarr
Expand All @@ -317,9 +385,9 @@ PHP_FUNCTION(futil_lastctime)
path = Z_STRVAL_PP(entry_data);
path_len = Z_STRLEN_PP(entry_data);

php_stat(path, path_len, FS_MTIME, &mtime TSRMLS_CC);
if (mtime.value.lval > lastctime ) {
lastctime = mtime.value.lval;
php_stat(path, path_len, FS_CTIME, &time TSRMLS_CC);
if ( Z_LVAL(time) > lastctime ) {
lastctime = Z_LVAL(time);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions php_fileutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ PHP_FUNCTION(futil_paths_filter_file);

PHP_FUNCTION(futil_lastmtime);
PHP_FUNCTION(futil_lastctime);
PHP_FUNCTION(futil_mtime_compare);
PHP_FUNCTION(futil_ctime_compare);

PHP_FUNCTION(futil_unlink_if_exists);
PHP_FUNCTION(futil_rmdir_if_exists);
Expand Down
28 changes: 28 additions & 0 deletions src/FileUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@ function futil_scanpath_dir($path)
return $newlist;
}

function futil_ctime_compare($filename1, $filename2)
{
$t1 = filectime($filename1);
$t2 = filectime($filename2);
if ( $t1 > $t2 ) {
return 1;
} elseif ( $t1 == $t2 ) {
return 0;
} elseif ( $t1 < $t2 ) {
return -1;
}
return false;
}

function futil_mtime_compare($filename1, $filename2)
{
$t1 = filemtime($filename1);
$t2 = filemtime($filename2);
if ( $t1 > $t2 ) {
return 1;
} elseif ( $t1 == $t2 ) {
return 0;
} elseif ( $t1 < $t2 ) {
return -1;
}
return false;
}

function futil_lastmtime($filelist)
{
$lastmtime = 0;
Expand Down
8 changes: 8 additions & 0 deletions tests/FileUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ public function testJoinWithArray()
}


public function testMTimeCompare()
{
touch('tests/data/new');
is(1, futil_mtime_compare('tests/data/new','tests/data/old') );
is(-1, futil_mtime_compare('tests/data/old','tests/data/new') );
}


public function testLastmtime()
{
touch(".last");
Expand Down
Empty file added tests/data/new
Empty file.
Empty file added tests/data/old
Empty file.

0 comments on commit d637ce0

Please sign in to comment.