diff options
author | Volker Lendecke <vl@samba.org> | 2009-05-14 15:34:42 +0200 |
---|---|---|
committer | Volker Lendecke <vl@samba.org> | 2009-05-26 17:48:23 +0200 |
commit | 49ca690b4b22ee6e597179059c9442e94c5bd423 (patch) | |
tree | ad233a9bcfee2d467824290448ca366219dbd301 /source3/lib | |
parent | 52f2f9449f8d53aa9181d656a4b54a007c80fa81 (diff) | |
download | samba-49ca690b4b22ee6e597179059c9442e94c5bd423.tar.gz samba-49ca690b4b22ee6e597179059c9442e94c5bd423.tar.bz2 samba-49ca690b4b22ee6e597179059c9442e94c5bd423.zip |
Introduce "struct stat_ex" as a replacement for SMB_STRUCT_STAT
This patch introduces
struct stat_ex {
dev_t st_ex_dev;
ino_t st_ex_ino;
mode_t st_ex_mode;
nlink_t st_ex_nlink;
uid_t st_ex_uid;
gid_t st_ex_gid;
dev_t st_ex_rdev;
off_t st_ex_size;
struct timespec st_ex_atime;
struct timespec st_ex_mtime;
struct timespec st_ex_ctime;
struct timespec st_ex_btime; /* birthtime */
blksize_t st_ex_blksize;
blkcnt_t st_ex_blocks;
};
typedef struct stat_ex SMB_STRUCT_STAT;
It is really large because due to the friendly libc headers playing macro
tricks with fields like st_ino, so I renamed them to st_ex_xxx.
Why this change? To support birthtime, we already have quite a few #ifdef's at
places where it does not really belong. With a stat struct that we control, we
can consolidate the nanosecond timestamps and the birthtime deep in the VFS
stat calls.
At this moment it is triggered by a request to support the birthtime field for
GPFS. GPFS does not extend the system level struct stat, but instead has a
separate call that gets us the additional information beyond posix. Without
being able to do that within the VFS stat calls, that support would have to be
scattered around the main smbd code.
It will very likely break all the onefs modules, but I think the changes will
be reasonably easy to do.
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/debug.c | 2 | ||||
-rw-r--r-- | source3/lib/system.c | 225 | ||||
-rw-r--r-- | source3/lib/time.c | 245 | ||||
-rw-r--r-- | source3/lib/util.c | 10 |
4 files changed, 222 insertions, 260 deletions
diff --git a/source3/lib/debug.c b/source3/lib/debug.c index 14aca3adad..419af61ef3 100644 --- a/source3/lib/debug.c +++ b/source3/lib/debug.c @@ -739,7 +739,7 @@ void check_log_size( void ) maxlog = lp_max_log_size() * 1024; - if( sys_fstat( x_fileno( dbf ), &st ) == 0 && st.st_size > maxlog ) { + if( sys_fstat( x_fileno( dbf ), &st ) == 0 && st.st_ex_size > maxlog ) { (void)reopen_logs(); if( dbf && get_file_size( debugf ) > maxlog ) { char *name = NULL; diff --git a/source3/lib/system.c b/source3/lib/system.c index 517e347c0f..5158750f98 100644 --- a/source3/lib/system.c +++ b/source3/lib/system.c @@ -290,6 +290,195 @@ int sys_fcntl_long(int fd, int cmd, long arg) return ret; } +/**************************************************************************** + Return the best approximation to a 'create time' under UNIX from a stat + structure. +****************************************************************************/ + +static time_t calc_create_time(const struct stat *st) +{ + time_t ret, ret1; + + ret = MIN(st->st_ctime, st->st_mtime); + ret1 = MIN(ret, st->st_atime); + + if(ret1 != (time_t)0) { + return ret1; + } + + /* + * One of ctime, mtime or atime was zero (probably atime). + * Just return MIN(ctime, mtime). + */ + return ret; +} + +/**************************************************************************** + Return the 'create time' from a stat struct if it exists (birthtime) or else + use the best approximation. +****************************************************************************/ + +static struct timespec get_create_timespec(const struct stat *pst) +{ + struct timespec ret; + + if (S_ISDIR(pst->st_mode) && lp_fake_dir_create_times()) { + ret.tv_sec = 315493200L; /* 1/1/1980 */ + ret.tv_nsec = 0; + return ret; + } + +#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC) + ret = pst->st_birthtimespec; +#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC) + ret.tv_sec = pst->st_birthtime; + ret.tv_nsec = pst->st_birthtimenspec; +#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME) + ret.tv_sec = pst->st_birthtime; + ret.tv_nsec = 0; +#else + ret.tv_sec = calc_create_time(pst); + ret.tv_nsec = 0; +#endif + + /* Deal with systems that don't initialize birthtime correctly. + * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>. + */ + if (null_timespec(ret)) { + ret.tv_sec = calc_create_time(pst); + ret.tv_nsec = 0; + } + return ret; +} + +/**************************************************************************** + Get/Set all the possible time fields from a stat struct as a timespec. +****************************************************************************/ + +static struct timespec get_atimespec(const struct stat *pst) +{ +#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) + struct timespec ret; + + /* Old system - no ns timestamp. */ + ret.tv_sec = pst->st_atime; + ret.tv_nsec = 0; + return ret; +#else +#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) + return pst->st_atim; +#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) + struct timespec ret; + ret.tv_sec = pst->st_atime; + ret.tv_nsec = pst->st_atimensec; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) + struct timespec ret; + ret.tv_sec = pst->st_atime; + ret.tv_nsec = pst->st_atime_n; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) + struct timespec ret; + ret.tv_sec = pst->st_atime; + ret.tv_nsec = pst->st_uatime * 1000; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) + return pst->st_atimespec; +#else +#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT +#endif +#endif +} + +static struct timespec get_mtimespec(const struct stat *pst) +{ +#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) + struct timespec ret; + + /* Old system - no ns timestamp. */ + ret.tv_sec = pst->st_mtime; + ret.tv_nsec = 0; + return ret; +#else +#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) + return pst->st_mtim; +#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) + struct timespec ret; + ret.tv_sec = pst->st_mtime; + ret.tv_nsec = pst->st_mtimensec; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) + struct timespec ret; + ret.tv_sec = pst->st_mtime; + ret.tv_nsec = pst->st_mtime_n; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) + struct timespec ret; + ret.tv_sec = pst->st_mtime; + ret.tv_nsec = pst->st_umtime * 1000; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) + return pst->st_mtimespec; +#else +#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT +#endif +#endif +} + +static struct timespec get_ctimespec(const struct stat *pst) +{ +#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) + struct timespec ret; + + /* Old system - no ns timestamp. */ + ret.tv_sec = pst->st_ctime; + ret.tv_nsec = 0; + return ret; +#else +#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) + return pst->st_ctim; +#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) + struct timespec ret; + ret.tv_sec = pst->st_ctime; + ret.tv_nsec = pst->st_ctimensec; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) + struct timespec ret; + ret.tv_sec = pst->st_ctime; + ret.tv_nsec = pst->st_ctime_n; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) + struct timespec ret; + ret.tv_sec = pst->st_ctime; + ret.tv_nsec = pst->st_uctime * 1000; + return ret; +#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) + return pst->st_ctimespec; +#else +#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT +#endif +#endif +} + +static void init_stat_ex_from_stat (struct stat_ex *dst, + const struct stat *src) +{ + dst->st_ex_dev = src->st_dev; + dst->st_ex_ino = src->st_ino; + dst->st_ex_mode = src->st_mode; + dst->st_ex_nlink = src->st_nlink; + dst->st_ex_uid = src->st_uid; + dst->st_ex_gid = src->st_gid; + dst->st_ex_rdev = src->st_rdev; + dst->st_ex_size = src->st_size; + dst->st_ex_atime = get_atimespec(src); + dst->st_ex_mtime = get_mtimespec(src); + dst->st_ex_ctime = get_ctimespec(src); + dst->st_ex_btime = get_create_timespec(src); + dst->st_ex_blksize = src->st_blksize; + dst->st_ex_blocks = src->st_blocks; +} + /******************************************************************* A stat() wrapper that will deal with 64 bit filesizes. ********************************************************************/ @@ -300,10 +489,16 @@ int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf) #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64) ret = stat64(fname, sbuf); #else - ret = stat(fname, sbuf); + struct stat statbuf; + ret = stat(fname, &statbuf); #endif - /* we always want directories to appear zero size */ - if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0; + if (ret == 0) { + /* we always want directories to appear zero size */ + if (S_ISDIR(statbuf.st_mode)) { + statbuf.st_size = 0; + } + init_stat_ex_from_stat(sbuf, &statbuf); + } return ret; } @@ -317,10 +512,16 @@ int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf) #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64) ret = fstat64(fd, sbuf); #else - ret = fstat(fd, sbuf); + struct stat statbuf; + ret = fstat(fd, &statbuf); #endif - /* we always want directories to appear zero size */ - if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0; + if (ret == 0) { + /* we always want directories to appear zero size */ + if (S_ISDIR(statbuf.st_mode)) { + statbuf.st_size = 0; + } + init_stat_ex_from_stat(sbuf, &statbuf); + } return ret; } @@ -334,10 +535,16 @@ int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf) #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64) ret = lstat64(fname, sbuf); #else - ret = lstat(fname, sbuf); + struct stat statbuf; + ret = lstat(fname, &statbuf); #endif - /* we always want directories to appear zero size */ - if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0; + if (ret == 0) { + /* we always want directories to appear zero size */ + if (S_ISDIR(statbuf.st_mode)) { + statbuf.st_size = 0; + } + init_stat_ex_from_stat(sbuf, &statbuf); + } return ret; } diff --git a/source3/lib/time.c b/source3/lib/time.c index 611debe366..a2e615acc5 100644 --- a/source3/lib/time.c +++ b/source3/lib/time.c @@ -322,251 +322,6 @@ void put_long_date(char *p, time_t t) put_long_date_timespec(p, ts); } -/**************************************************************************** - Return the best approximation to a 'create time' under UNIX from a stat - structure. -****************************************************************************/ - -static time_t calc_create_time(const SMB_STRUCT_STAT *st) -{ - time_t ret, ret1; - - ret = MIN(st->st_ctime, st->st_mtime); - ret1 = MIN(ret, st->st_atime); - - if(ret1 != (time_t)0) { - return ret1; - } - - /* - * One of ctime, mtime or atime was zero (probably atime). - * Just return MIN(ctime, mtime). - */ - return ret; -} - -/**************************************************************************** - Return the 'create time' from a stat struct if it exists (birthtime) or else - use the best approximation. -****************************************************************************/ - -struct timespec get_create_timespec(const SMB_STRUCT_STAT *pst,bool fake_dirs) -{ - struct timespec ret; - - if(S_ISDIR(pst->st_mode) && fake_dirs) { - ret.tv_sec = 315493200L; /* 1/1/1980 */ - ret.tv_nsec = 0; - return ret; - } - -#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC) - ret = pst->st_birthtimespec; -#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC) - ret.tv_sec = pst->st_birthtime; - ret.tv_nsec = pst->st_birthtimenspec; -#elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME) - ret.tv_sec = pst->st_birthtime; - ret.tv_nsec = 0; -#else - ret.tv_sec = calc_create_time(pst); - ret.tv_nsec = 0; -#endif - - /* Deal with systems that don't initialize birthtime correctly. - * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>. - */ - if (null_timespec(ret)) { - ret.tv_sec = calc_create_time(pst); - ret.tv_nsec = 0; - } - return ret; -} - -/**************************************************************************** - Get/Set all the possible time fields from a stat struct as a timespec. -****************************************************************************/ - -struct timespec get_atimespec(const SMB_STRUCT_STAT *pst) -{ -#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) - struct timespec ret; - - /* Old system - no ns timestamp. */ - ret.tv_sec = pst->st_atime; - ret.tv_nsec = 0; - return ret; -#else -#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) - return pst->st_atim; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) - struct timespec ret; - ret.tv_sec = pst->st_atime; - ret.tv_nsec = pst->st_atimensec; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) - struct timespec ret; - ret.tv_sec = pst->st_atime; - ret.tv_nsec = pst->st_atime_n; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) - struct timespec ret; - ret.tv_sec = pst->st_atime; - ret.tv_nsec = pst->st_uatime * 1000; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) - return pst->st_atimespec; -#else -#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT -#endif -#endif -} - -void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts) -{ -#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) - /* Old system - no ns timestamp. */ - pst->st_atime = ts.tv_sec; -#else -#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) - pst->st_atim = ts; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) - pst->st_atime = ts.tv_sec; - pst->st_atimensec = ts.tv_nsec; -#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) - pst->st_atime = ts.tv_sec; - pst->st_atime_n = ts.tv_nsec; -#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) - pst->st_atime = ts.tv_sec; - pst->st_uatime = ts.tv_nsec / 1000; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) - pst->st_atimespec = ts; -#else -#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT -#endif -#endif -} - -struct timespec get_mtimespec(const SMB_STRUCT_STAT *pst) -{ -#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) - struct timespec ret; - - /* Old system - no ns timestamp. */ - ret.tv_sec = pst->st_mtime; - ret.tv_nsec = 0; - return ret; -#else -#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) - return pst->st_mtim; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) - struct timespec ret; - ret.tv_sec = pst->st_mtime; - ret.tv_nsec = pst->st_mtimensec; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) - struct timespec ret; - ret.tv_sec = pst->st_mtime; - ret.tv_nsec = pst->st_mtime_n; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) - struct timespec ret; - ret.tv_sec = pst->st_mtime; - ret.tv_nsec = pst->st_umtime * 1000; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) - return pst->st_mtimespec; -#else -#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT -#endif -#endif -} - -void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts) -{ -#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) - /* Old system - no ns timestamp. */ - pst->st_mtime = ts.tv_sec; -#else -#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) - pst->st_mtim = ts; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) - pst->st_mtime = ts.tv_sec; - pst->st_mtimensec = ts.tv_nsec; -#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) - pst->st_mtime = ts.tv_sec; - pst->st_mtime_n = ts.tv_nsec; -#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) - pst->st_mtime = ts.tv_sec; - pst->st_umtime = ts.tv_nsec / 1000; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) - pst->st_mtimespec = ts; -#else -#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT -#endif -#endif -} - -struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst) -{ -#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) - struct timespec ret; - - /* Old system - no ns timestamp. */ - ret.tv_sec = pst->st_ctime; - ret.tv_nsec = 0; - return ret; -#else -#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) - return pst->st_ctim; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) - struct timespec ret; - ret.tv_sec = pst->st_ctime; - ret.tv_nsec = pst->st_ctimensec; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) - struct timespec ret; - ret.tv_sec = pst->st_ctime; - ret.tv_nsec = pst->st_ctime_n; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) - struct timespec ret; - ret.tv_sec = pst->st_ctime; - ret.tv_nsec = pst->st_uctime * 1000; - return ret; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) - return pst->st_ctimespec; -#else -#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT -#endif -#endif -} - -void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts) -{ -#if !defined(HAVE_STAT_HIRES_TIMESTAMPS) - /* Old system - no ns timestamp. */ - pst->st_ctime = ts.tv_sec; -#else -#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) - pst->st_ctim = ts; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC) - pst->st_ctime = ts.tv_sec; - pst->st_ctimensec = ts.tv_nsec; -#elif defined(HAVE_STRUCT_STAT_ST_MTIME_N) - pst->st_ctime = ts.tv_sec; - pst->st_ctime_n = ts.tv_nsec; -#elif defined(HAVE_STRUCT_STAT_ST_UMTIME) - pst->st_ctime = ts.tv_sec; - pst->st_uctime = ts.tv_nsec / 1000; -#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) - pst->st_ctimespec = ts; -#else -#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT -#endif -#endif -} - void dos_filetime_timespec(struct timespec *tsp) { tsp->tv_sec &= ~1; diff --git a/source3/lib/util.c b/source3/lib/util.c index c86f259ce3..13f7e3c9ee 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -541,7 +541,7 @@ bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf) if (sys_stat(fname,sbuf) != 0) return(False); - return((S_ISREG(sbuf->st_mode)) || (S_ISFIFO(sbuf->st_mode))); + return((S_ISREG(sbuf->st_ex_mode)) || (S_ISFIFO(sbuf->st_ex_mode))); } /******************************************************************* @@ -554,7 +554,7 @@ bool socket_exist(const char *fname) if (sys_stat(fname,&st) != 0) return(False); - return S_ISSOCK(st.st_mode); + return S_ISSOCK(st.st_ex_mode); } /******************************************************************* @@ -572,7 +572,7 @@ bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st) if (sys_stat(dname,st) != 0) return(False); - ret = S_ISDIR(st->st_mode); + ret = S_ISDIR(st->st_ex_mode); if(!ret) errno = ENOTDIR; return ret; @@ -584,7 +584,7 @@ bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st) uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf) { - return sbuf->st_size; + return sbuf->st_ex_size; } /******************************************************************* @@ -594,7 +594,7 @@ uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf) SMB_OFF_T get_file_size(char *file_name) { SMB_STRUCT_STAT buf; - buf.st_size = 0; + buf.st_ex_size = 0; if(sys_stat(file_name,&buf) != 0) return (SMB_OFF_T)-1; return get_file_size_stat(&buf); |