2006-09-19 02:49:20 +04:00
/*
test readdir / unlink pattern that OS / 2 uses
tridge @ samba . org July 2005
*/
# include <stdio.h>
# include <stdlib.h>
# include <sys/stat.h>
# include <unistd.h>
# include <sys/types.h>
# include <dirent.h>
# include <errno.h>
# include <string.h>
# include <fcntl.h>
2011-03-19 02:44:47 +03:00
# include "replace-test.h"
2006-09-19 02:49:20 +04:00
# define NUM_FILES 700
# define READDIR_SIZE 100
# define DELETE_SIZE 4
# define TESTDIR "test.dir"
static int test_readdir_os2_delete_ret ;
2014-07-31 05:03:59 +04:00
# define FAILED(d) (printf("failure: readdir [\nFailed for %s - %d = %s\n]\n", d, errno, strerror(errno)), test_readdir_os2_delete_ret = 1)
2006-09-19 02:49:20 +04:00
# ifndef MIN
# define MIN(a,b) ((a)<(b)?(a):(b))
# endif
2009-04-20 17:39:48 +04:00
# ifdef _WIN32
# define mkdir(d,m) _mkdir(d)
# endif
2006-09-19 02:49:20 +04:00
static void cleanup ( void )
{
/* I'm a lazy bastard */
2009-01-02 22:38:24 +03:00
if ( system ( " rm -rf " TESTDIR ) ) {
FAILED ( " system " ) ;
}
2006-09-19 02:49:20 +04:00
mkdir ( TESTDIR , 0700 ) = = 0 | | FAILED ( " mkdir " ) ;
}
static void create_files ( void )
{
int i ;
for ( i = 0 ; i < NUM_FILES ; i + + ) {
char fname [ 40 ] ;
2008-03-27 13:26:33 +03:00
int fd ;
2010-10-22 16:41:47 +04:00
snprintf ( fname , sizeof ( fname ) , TESTDIR " /test%u.txt " , i ) ;
2008-03-27 13:26:33 +03:00
fd = open ( fname , O_CREAT | O_RDWR , 0600 ) ;
if ( fd < 0 ) {
FAILED ( " open " ) ;
}
if ( close ( fd ) ! = 0 ) {
FAILED ( " close " ) ;
}
2006-09-19 02:49:20 +04:00
}
}
static int os2_delete ( DIR * d )
{
off_t offsets [ READDIR_SIZE ] ;
int i , j ;
struct dirent * de ;
char names [ READDIR_SIZE ] [ 30 ] ;
/* scan, remembering offsets */
for ( i = 0 , de = readdir ( d ) ;
de & & i < READDIR_SIZE ;
de = readdir ( d ) , i + + ) {
offsets [ i ] = telldir ( d ) ;
2015-03-07 14:24:33 +03:00
/* strlcpy not available here */
snprintf ( names [ i ] , sizeof ( names [ i ] ) , " %s " , de - > d_name ) ;
2006-09-19 02:49:20 +04:00
}
if ( i = = 0 ) {
return 0 ;
}
/* delete the first few */
for ( j = 0 ; j < MIN ( i , DELETE_SIZE ) ; j + + ) {
char fname [ 40 ] ;
2010-10-22 16:41:47 +04:00
snprintf ( fname , sizeof ( fname ) , TESTDIR " /%s " , names [ j ] ) ;
2006-09-19 02:49:20 +04:00
unlink ( fname ) = = 0 | | FAILED ( " unlink " ) ;
}
/* seek to just after the deletion */
seekdir ( d , offsets [ j - 1 ] ) ;
/* return number deleted */
return j ;
}
int test_readdir_os2_delete ( void )
{
int total_deleted = 0 ;
DIR * d ;
struct dirent * de ;
test_readdir_os2_delete_ret = 0 ;
cleanup ( ) ;
create_files ( ) ;
2006-10-02 13:21:20 +04:00
d = opendir ( TESTDIR " /test0.txt " ) ;
if ( d ! = NULL ) FAILED ( " opendir() on file succeed " ) ;
if ( errno ! = ENOTDIR ) FAILED ( " opendir() on file didn't give ENOTDIR " ) ;
2006-09-19 02:49:20 +04:00
d = opendir ( TESTDIR ) ;
/* skip past . and .. */
de = readdir ( d ) ;
strcmp ( de - > d_name , " . " ) = = 0 | | FAILED ( " match . " ) ;
de = readdir ( d ) ;
strcmp ( de - > d_name , " .. " ) = = 0 | | FAILED ( " match .. " ) ;
while ( 1 ) {
int n = os2_delete ( d ) ;
if ( n = = 0 ) break ;
total_deleted + = n ;
}
closedir ( d ) ;
2007-01-15 22:10:54 +03:00
fprintf ( stderr , " Deleted %d files of %d \n " , total_deleted , NUM_FILES ) ;
2006-09-19 02:49:20 +04:00
rmdir ( TESTDIR ) = = 0 | | FAILED ( " rmdir " ) ;
2009-01-02 22:38:24 +03:00
if ( system ( " rm -rf " TESTDIR ) = = - 1 ) {
FAILED ( " system " ) ;
}
2007-04-20 03:47:55 +04:00
2006-09-19 02:49:20 +04:00
return test_readdir_os2_delete_ret ;
}