
/* usage: splitf_xattr options srcdir 
 *    options:  --files-from file
 *              --crtime
 * 
 * Works like split_xattr, but writes all xattr information to
 * stdout, rather than creating a directory structure.
 * 
 * with the --files-from file option, the contructed repository is 
 * pruned to only include those files and directories listed in file.
 *
 * the --crtime flag causes the creation times of all files to
 * be preserved 
 *
 * Returns -1 if errors detected, and 0 otherwise.
 *
 */


/* output file format:
 *   - header (8 "magic" bytes)
 *   - for each entry:
 *       - a null terminated relative path name (starting with "/", if non-empty)
 *       - an xattr container
 */


#include "util.h"
#include "xattr_util.h"



static int crtime = 0;
static int return_value = 0;
static int source_name_len = 0;


static char magic[8] = { 0xb7, 0x0e, 0xbf, 0xb2, 0xc2, 0x91, 0xf2, 0x92 };


void process_xattrs(const char *itemname, const struct stat *itemstat)
{
   const char *ext;
   long extlen;


#if 0
   if (crtime || has_locks(itemstat) || has_xattr(itemname))
#endif
   {
   

      ext = itemname + source_name_len;
      extlen = strlen(ext);

      if (fwrite(ext, 1, extlen+1, stdout) != extlen+1 ||
          split_xattr(itemname, itemstat, "", crtime)) {
            fprintf(stderr,"splitf_xattr: error processing %s --- aborting\n", itemname);
            exit(-1);

      }

   }
}


void dirwalk(const char *dirname, const struct stat *dirstat, int walk_state)
{
   char itemname[MAXLEN];
   DIR *dirlist;
   struct dirent *diritem; 
   struct stat itemstat;
   int walk_state1;


   dirlist = opendir(dirname);

   if (!dirlist) {
      fprintf(stderr, "splitf_xattr: internal error -- opendir failed\n");
      return_value = -1;
      return;
   }

   while ( (diritem = readdir(dirlist)) ) {

      if (strcmp(diritem->d_name, ".") == 0 
          || strcmp(diritem->d_name, "..") == 0)
         continue;

      if (snprintf(itemname, MAXLEN, "%s/%s", 
          dirname, diritem->d_name) >= MAXLEN) overflow();
       

      walk_state1 = walk_state;

      if (walk_state1 == 0) {
	    walk_state1 = lookup_name(itemname + source_name_len + 1);
	    if (walk_state1 == -1) continue; /* pruning */
      }

      if (lstat(itemname, &itemstat)) {
         fprintf(stderr, "splitf_xattr: lstat failed on %s\n", itemname);
         return_value = -1;
         continue;
      }


      if (!(S_ISDIR(itemstat.st_mode) || S_ISREG_OR_LNK(itemstat.st_mode)))
         continue;


      if (walk_state1 == 1 && S_ISREG_OR_LNK(itemstat.st_mode)) 
         process_xattrs(itemname, &itemstat);

      if (S_ISDIR(itemstat.st_mode)) {
	 dirwalk(itemname, &itemstat, walk_state1);
      }

   }

   closedir(dirlist);

   if (walk_state == 1)
      process_xattrs(dirname, dirstat);
}

void usage()
{
   fprintf(stderr, "usage: splitf_xattr options srcdir\n");
   fprintf(stderr, "  options:  --files-from file\n");
   fprintf(stderr, "            --crtime\n");

}


int main(int argc, char **argv)
{
   char *fname, *srcname;
   struct stat srcstat;
   int srcname_len;
   int walk_state;

   int i;

   fname = 0;
   
   i = 1;
   while (i < argc) {
      if (strcmp(argv[i], "--crtime") == 0) {
         i++;
         crtime = 1;
      }
      else if (strcmp(argv[i], "--files-from") == 0) {
         if (i == argc-1) {
            usage();
            return -1;
         }
         i++;
         fname = argv[i];
         i++;
      }
      else
         break;
   }

   if (i != argc-1) {
      usage();
      return -1;
   }

   srcname = argv[argc-1];

   if (fname) {
      collect_names(fname);
      walk_state = 0;
   }
   else {
      walk_state = 1;
   }

   srcname_len = strlen(srcname);
   if (srcname[srcname_len-1] == '/') {
      srcname[srcname_len-1] = '\0';
      srcname_len--;
   }

   if (lstat(srcname, &srcstat) || !S_ISDIR(srcstat.st_mode)) {
      usage();
      return -1;
   }

   source_name_len = srcname_len;

   if (fwrite(magic, 1, 8, stdout) != 8) {
      fprintf(stderr, "write error --- aborting\n");
      return -1;
   }

   dirwalk(srcname, &srcstat, walk_state);

   return return_value;

}
