2017-11-18 19:09:20 +03:00
/* SPDX-License-Identifier: LGPL-2.1+ */
2016-03-03 02:35:36 +03:00
# include <errno.h>
# include <fcntl.h>
# include <sys/types.h>
# include <sys/xattr.h>
# include <unistd.h>
# include "alloc-util.h"
# include "fd-util.h"
2018-02-20 14:48:33 +03:00
# include "fileio.h"
2016-03-03 02:35:36 +03:00
# include "fs-util.h"
# include "macro.h"
# include "string-util.h"
2018-09-13 15:31:13 +03:00
# include "tests.h"
2016-03-03 02:35:36 +03:00
# include "xattr-util.h"
static void test_fgetxattrat_fake ( void ) {
char t [ ] = " /var/tmp/xattrtestXXXXXX " ;
_cleanup_close_ int fd = - 1 ;
const char * x ;
basic/xattr-util: do not cast ssize_t to int
gcc warns about unitialized memory access because it notices that ssize_t which
is < 0 could be cast to positive int value. We know that this can't really
happen because only -1 can be returned, but OTOH, in principle a large
*positive* value cannot be cast properly. This is unlikely too, since xattrs
cannot be too large, but it seems cleaner to just use a size_t to return the
value and avoid the cast altoghter. This makes the code simpler and gcc is
happy too.
The following warning goes away:
[113/1502] Compiling C object 'src/basic/basic@sta/xattr-util.c.o'.
In file included from ../src/basic/alloc-util.h:28:0,
from ../src/basic/xattr-util.c:30:
../src/basic/xattr-util.c: In function ‘fd_getcrtime_at’:
../src/basic/macro.h:207:60: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
UNIQ_T(A,aq) < UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
^
../src/basic/xattr-util.c:155:19: note: ‘b’ was declared here
usec_t a, b;
^
2018-02-25 23:25:33 +03:00
char v [ 3 ] ;
2016-03-03 02:35:36 +03:00
int r ;
basic/xattr-util: do not cast ssize_t to int
gcc warns about unitialized memory access because it notices that ssize_t which
is < 0 could be cast to positive int value. We know that this can't really
happen because only -1 can be returned, but OTOH, in principle a large
*positive* value cannot be cast properly. This is unlikely too, since xattrs
cannot be too large, but it seems cleaner to just use a size_t to return the
value and avoid the cast altoghter. This makes the code simpler and gcc is
happy too.
The following warning goes away:
[113/1502] Compiling C object 'src/basic/basic@sta/xattr-util.c.o'.
In file included from ../src/basic/alloc-util.h:28:0,
from ../src/basic/xattr-util.c:30:
../src/basic/xattr-util.c: In function ‘fd_getcrtime_at’:
../src/basic/macro.h:207:60: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
UNIQ_T(A,aq) < UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
^
../src/basic/xattr-util.c:155:19: note: ‘b’ was declared here
usec_t a, b;
^
2018-02-25 23:25:33 +03:00
size_t size ;
2016-03-03 02:35:36 +03:00
assert_se ( mkdtemp ( t ) ) ;
x = strjoina ( t , " /test " ) ;
assert_se ( touch ( x ) > = 0 ) ;
r = setxattr ( x , " user.foo " , " bar " , 3 , 0 ) ;
if ( r < 0 & & errno = = EOPNOTSUPP ) /* no xattrs supported on /var/tmp... */
goto cleanup ;
assert_se ( r > = 0 ) ;
fd = open ( t , O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOCTTY ) ;
assert_se ( fd > = 0 ) ;
basic/xattr-util: do not cast ssize_t to int
gcc warns about unitialized memory access because it notices that ssize_t which
is < 0 could be cast to positive int value. We know that this can't really
happen because only -1 can be returned, but OTOH, in principle a large
*positive* value cannot be cast properly. This is unlikely too, since xattrs
cannot be too large, but it seems cleaner to just use a size_t to return the
value and avoid the cast altoghter. This makes the code simpler and gcc is
happy too.
The following warning goes away:
[113/1502] Compiling C object 'src/basic/basic@sta/xattr-util.c.o'.
In file included from ../src/basic/alloc-util.h:28:0,
from ../src/basic/xattr-util.c:30:
../src/basic/xattr-util.c: In function ‘fd_getcrtime_at’:
../src/basic/macro.h:207:60: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
UNIQ_T(A,aq) < UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
^
../src/basic/xattr-util.c:155:19: note: ‘b’ was declared here
usec_t a, b;
^
2018-02-25 23:25:33 +03:00
assert_se ( fgetxattrat_fake ( fd , " test " , " user.foo " , v , 3 , 0 , & size ) > = 0 ) ;
assert_se ( size = = 3 ) ;
2016-03-03 02:35:36 +03:00
assert_se ( memcmp ( v , " bar " , 3 ) = = 0 ) ;
safe_close ( fd ) ;
fd = open ( " / " , O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOCTTY ) ;
assert_se ( fd > = 0 ) ;
basic/xattr-util: do not cast ssize_t to int
gcc warns about unitialized memory access because it notices that ssize_t which
is < 0 could be cast to positive int value. We know that this can't really
happen because only -1 can be returned, but OTOH, in principle a large
*positive* value cannot be cast properly. This is unlikely too, since xattrs
cannot be too large, but it seems cleaner to just use a size_t to return the
value and avoid the cast altoghter. This makes the code simpler and gcc is
happy too.
The following warning goes away:
[113/1502] Compiling C object 'src/basic/basic@sta/xattr-util.c.o'.
In file included from ../src/basic/alloc-util.h:28:0,
from ../src/basic/xattr-util.c:30:
../src/basic/xattr-util.c: In function ‘fd_getcrtime_at’:
../src/basic/macro.h:207:60: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
UNIQ_T(A,aq) < UNIQ_T(B,bq) ? UNIQ_T(A,aq) : UNIQ_T(B,bq); \
^
../src/basic/xattr-util.c:155:19: note: ‘b’ was declared here
usec_t a, b;
^
2018-02-25 23:25:33 +03:00
assert_se ( fgetxattrat_fake ( fd , " usr " , " user.idontexist " , v , 3 , 0 , & size ) = = - ENODATA ) ;
2016-03-03 02:35:36 +03:00
cleanup :
assert_se ( unlink ( x ) > = 0 ) ;
assert_se ( rmdir ( t ) > = 0 ) ;
}
2018-02-20 14:48:33 +03:00
static void test_getcrtime ( void ) {
_cleanup_close_ int fd = - 1 ;
char ts [ FORMAT_TIMESTAMP_MAX ] ;
const char * vt ;
usec_t usec , k ;
int r ;
assert_se ( tmp_dir ( & vt ) > = 0 ) ;
fd = open_tmpfile_unlinkable ( vt , O_RDWR ) ;
assert_se ( fd > = 0 ) ;
r = fd_getcrtime ( fd , & usec ) ;
if ( r < 0 )
log_debug_errno ( r , " btime: %m " ) ;
else
log_debug ( " btime: %s " , format_timestamp ( ts , sizeof ( ts ) , usec ) ) ;
k = now ( CLOCK_REALTIME ) ;
r = fd_setcrtime ( fd , 1519126446UL * USEC_PER_SEC ) ;
if ( ! IN_SET ( r , - EOPNOTSUPP , - ENOTTY ) ) {
assert_se ( fd_getcrtime ( fd , & usec ) > = 0 ) ;
assert_se ( k < 1519126446UL * USEC_PER_SEC | |
usec = = 1519126446UL * USEC_PER_SEC ) ;
}
}
2016-03-03 02:35:36 +03:00
int main ( void ) {
2018-09-13 15:31:13 +03:00
test_setup_logging ( LOG_DEBUG ) ;
2018-02-20 14:48:33 +03:00
2016-03-03 02:35:36 +03:00
test_fgetxattrat_fake ( ) ;
2018-02-20 14:48:33 +03:00
test_getcrtime ( ) ;
2016-03-03 02:35:36 +03:00
return 0 ;
}