merge with 0.30.213
[util-vserver.git] / src / showattr.c
index 1d7783e..5260026 100644 (file)
@@ -1,4 +1,4 @@
-// $Id: showattr.c,v 1.1.4.1 2003/11/18 22:31:10 ensc Exp $
+// $Id: showattr.c 1980 2005-03-24 12:44:17Z ensc $
 
 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 // based on showattr.cc by Jacques Gelinas
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
-#include "compat.h"
 
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
+#include "fstool.h"
+#include "util.h"
 
-#include "ext2fs.h"
+#include <lib/fmt.h>
+#include <lib/vserver.h>
+#include <lib/vserver-internal.h>
 
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <ctype.h>
 
-// Patch to help compile this utility on unpatched kernel source
-#ifndef EXT2_IUNLINK_FL
-/* Set both bits for backward compatibility */
-#define EXT2_IUNLINK_FL 0x08008000
+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 }
+};
 
-#ifndef EXT2_BARRIER_FL
-#define EXT2_BARRIER_FL 0x04000000
-#endif
+char const             CMDLINE_OPTIONS_SHORT[] = "Radx";
 
-/*
-       Get the extended attributes of a file
-*/
-static int getext2flags (const char *fname, long *flags)
+void
+showHelp(int fd, char const *cmd, int res)
 {
-       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(fd, "Usage:  ");
+  WRITE_STR(fd, cmd);
+  WRITE_MSG(fd,
+           " [-Radx] [--] <file>*\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"
+           "   -x  ...  do not cross filesystems\n\n"
+           "Please report bugs to " PACKAGE_BUGREPORT "\n");
+  exit(res);
 }
 
-/*
-       Set the extended attributes of a file
-*/
-static int setext2flags (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{
-               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;
+  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);
 }
 
+void
+fixupParams(struct Arguments UNUSED * args, int UNUSED argc)
+{
+}
 
-int main (int argc, char *argv[])
+static bool
+getFlags(char const *name, uint32_t *flags, uint32_t *mask)
 {
-       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; i<argc; i++){
-                       long flags;
-                       ret = getext2flags (argv[i],&flags);
-                       if (ret == -1){
-                               break;
-                       }else{
-                               printf ("%s\t%08lx\n",argv[i],flags);
-                       }
-               }
-       }else if (strstr(argv[0],"setattr")!=NULL){
-               long flags, add_flags = 0, remove_flags = 0;
-               int  i;
-               ret = 0;
-               for (i=1; i<argc; i++){
-                       const char *arg = argv[i];
-                       if (strncmp(arg,"--",2)==0){
-                               if (strcmp(arg,"--immutable")==0){
-                                       add_flags |= EXT2_IMMUTABLE_FL;
-                               }else if (strcmp(arg,"--!immutable")==0 || strcmp(arg,"--~immutable")==0){
-                                       remove_flags |= EXT2_IMMUTABLE_FL;
-                               }else if (strcmp(arg,"--immulink")==0){
-                                       add_flags |= EXT2_IUNLINK_FL;
-                               }else if (strcmp(arg,"--!immulink")==0 || strcmp(arg,"--~immulink")==0){
-                                       remove_flags |= EXT2_IUNLINK_FL;
-                               }else if (strcmp(arg,"--barrier")==0){
-                                       add_flags |= EXT2_BARRIER_FL;
-                               }else if (strcmp(arg,"--!barrier")==0 || strcmp(arg,"--~barrier")==0){
-                                       remove_flags |= EXT2_BARRIER_FL;
-                               }else{
-                                       fprintf (stderr,"Invalid option %s\n",arg);
-                                       ret = -1;
-                                       break;
-                               }
-                       }else{
-                               ret = getext2flags (arg,&flags);
-                               if (ret == -1){
-                                       break;
-                               }
-                               flags |= add_flags;
-                               flags &= ~remove_flags;
-                               ret = setext2flags (arg,flags);
-                               if (ret == -1){
-                                       break;
-                               }
-                       }
-               }
-       }
-       return ret;
+  xid_t                xid;
+  *mask = ~0;
+  
+  if (vc_get_iattr(name, &xid, flags, mask)==-1) {
+    perror("vc_get_iattr()");
+    return false;
+  }
+
+  return true;
 }
 
+bool
+handleFile(char const *name, char const *display_name)
+{
+  bool                 res = true;
+  char                 buf[40];
+  char                 *ptr = buf;
+  uint32_t             flags;
+  uint32_t             mask;
+
+  memset(buf, ' ', sizeof buf);
+
+  if (getFlags(name, &flags, &mask)) {
+      //                                     1       1       0       0
+      //                              fedcba9876543210fedcba9876543210
+    static char const  MARKER[33] = ".......x.....iub.............hwa";
+    int                i;
+    uint32_t           used_flags = (VC_IATTR_XID|VC_IATTR_ADMIN|
+                                     VC_IATTR_WATCH|VC_IATTR_HIDE|
+                                     VC_IATTR_BARRIER|VC_IATTR_IUNLINK|
+                                     VC_IATTR_IMMUTABLE);
+
+    for (i=0; i<32; ++i) {
+      if (used_flags & 1) {
+       if (!   (mask  & 1) ) *ptr++ = '-';
+       else if (flags & 1)   *ptr++ = toupper(MARKER[31-i]);
+       else                  *ptr++ = MARKER[31-i];
+      }
+
+      used_flags >>= 1;
+      flags      >>= 1;
+      mask       >>= 1;
+    }
+  }      
+  else {
+    memcpy(buf, "ERR   ", 7);
+    res = false;
+  }
+
+  Vwrite(1, buf, 8);
+  Vwrite(1, display_name, strlen(display_name));
+  Vwrite(1, "\n", 1);
+
+  return res;
+}