From 4dabc1da766b70b24d24fd576b5a45363600f424 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Tue, 13 Jan 2004 13:49:12 +0000 Subject: [PATCH] rewrote completely git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@560 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/src/showattr.c | 228 +++++++++++++++++++++++++------------------- 1 file changed, 131 insertions(+), 97 deletions(-) diff --git a/util-vserver/src/showattr.c b/util-vserver/src/showattr.c index daaf7a6..01a9f52 100644 --- a/util-vserver/src/showattr.c +++ b/util-vserver/src/showattr.c @@ -20,117 +20,151 @@ #ifdef HAVE_CONFIG_H # include #endif -#include "compat.h" + +#include "fstool.h" +#include "util.h" + +#include +#include +#include #include -#include -#include -#include +#include +#include #include -#include -#include "ext2fs.h" +struct option const +CMDLINE_OPTIONS[] = { + { "help", no_argument, 0, CMD_HELP }, + { "version", no_argument, 0, CMD_VERSION }, +#ifdef VC_ENABLE_API_LEGACY + { "legacy", no_argument, 0, CMD_LEGACY }, +#endif + { 0,0,0,0 } +}; +char const CMDLINE_OPTIONS_SHORT[] = "Rad"; -// Patch to help compile this utility on unpatched kernel source -#ifndef EXT2_IMMUTABLE_FILE_FL - #define EXT2_IMMUTABLE_FILE_FL 0x00000010 - #define EXT2_IMMUTABLE_LINK_FL 0x00008000 -#endif +void +showHelp(int fd, char const *cmd, int res) +{ + WRITE_MSG(fd, "Usage: "); + WRITE_STR(fd, cmd); + WRITE_MSG(fd, + " [-Rad] [--] *\n\n" + " Options:\n" + " -R ... recurse through directories\n" + " -a ... display files starting with '.' also\n" + " -d ... list directories like other files instead of listing\n" + " their content\n" + "Please report bugs to " PACKAGE_BUGREPORT "\n"); + exit(res); +} -/* - Get the extended attributes of a file -*/ -static int getext2flags (const char *fname, long *flags) +void +showVersion() { - int ret = -1; - int fd = open (fname,O_RDONLY); - if (fd == -1){ - fprintf (stderr,"Can't open file %s (%s)\n",fname,strerror(errno)); - }else{ - *flags = 0; - ret = ioctl (fd,EXT2_IOC_GETFLAGS,flags); - close (fd); - if (ret == -1){ - fprintf (stderr,"Can't get ext2 flags on file %s (%s)\n" - ,fname,strerror(errno)); - } - } - return ret; + WRITE_MSG(1, + "showattr " VERSION " -- shows vserver specific file attributes\n" + "This program is part of " PACKAGE_STRING "\n\n" + "Copyright (C) 2004 Enrico Scholz\n" + VERSION_COPYRIGHT_DISCLAIMER); + exit(0); } -/* - Set the extended attributes of a file -*/ -static int setext2flags (const char *fname, long flags) +void +checkParams(struct Arguments const UNUSED * args, int UNUSED argc) { - int ret = -1; - int fd = open (fname,O_RDONLY); - if (fd == -1){ - fprintf (stderr,"Can't open file %s (%s)\n",fname,strerror(errno)); - }else{ - ret = ioctl (fd,EXT2_IOC_SETFLAGS,&flags); - close (fd); - if (ret == -1){ - fprintf (stderr,"Can't set ext2 flags on file %s (%s)\n" - ,fname,strerror(errno)); - } - } - return ret; } +static bool +getFlags(char const *name, struct stat const *exp_st, long *flags) +{ + int fd = open(name, O_RDONLY); + int rc; + + if (fd==-1) { + perror("open()"); + return false; + } + + if (exp_st) + checkForRace(fd, name, exp_st); + + rc = vc_X_get_ext2flags(fd, flags); + *flags &= VC_IMMUTABLE_ALL; -int main (int argc, char *argv[]) + if (rc==-1) + perror("vc_X_get_ext2flags()"); + + close(fd); + return rc!=-1; +} + +static void +writePadded(long num) { - int ret = -1; - if (argc <= 1){ - fprintf (stderr - ,"showattr file ...\n" - "\n" - "Presents extended file attribute.\n" - "\n" - "setattr --immutable --immulink file ...\n" - "\n" - "Sets the extended file attributes.\n" - "\n" - "These utilities exist as an interim until lsattr and\n" - "chattr are updated.\n" - ); - }else if (strstr(argv[0],"showattr")!=NULL){ - int i; - for (i=1; ist_mode)) { + write(1, display_name, strlen(display_name)); + write(1, " -\n", 2); + return true; + } + + if (!getFlags(name, exp_st, &flags)) { + perror(display_name); + return false; + } + + write(1, display_name, strlen(display_name)); + write(1, "\t", 1); + writePadded(flags); + write(1, "\n", 1); + + return true; +} +#endif + +bool +handleFile(char const *name, char const *display_name, + struct stat const *exp_st) +{ + bool res = true; +#ifdef VC_ENABLE_API_LEGACY + if (global_args->is_legacy) + return handleFileLegacy(name, display_name, exp_st); +#endif + + if (S_ISLNK(exp_st->st_mode)) { + write(1, "--------", 8); + } + else { + long flags; + + if (getFlags(name, exp_st, &flags)) + writePadded(flags); + else { + write(1, "ERR ", 8); + res = false; + } + } + + write(1, " ", 2); + write(1, display_name, strlen(display_name)); + write(1, "\n", 1); + + return res; +} -- 1.8.1.5