
/* usage: strip_locks srcdir 
 *        strip_locks --files-from file srcdir 
 * 
 * strips locks from files in srcdir
 * 
 * with the --files-from flag, the only files in srcdir that
 * are examined are those listed in file (and subdirectories
 * along the way).
 *
 * Returns -1 if errors detected, and 0 otherwise.
 *
 */

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



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


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, "strip_locks: 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, "strip_locks: 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 (has_locks(&itemstat)) {
         fprintf(stderr, "strip_locks: removing locks on %s\n", itemname);

         if (remove_locks(itemname, &itemstat)) {
            fprintf(stderr, "strip_locks: failed to remove lock on %s\n", itemname);
            return_value = -1;
         }
      }

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

   closedir(dirlist);
}

void usage()
{
   fprintf(stderr, "usage: strip_locks [ --files-from file ] srcdir\n");
}


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

   fname = 0;
   
   
   if (argc == 4) {
      if (strcmp(argv[1], "--files-from") != 0) {
         usage();
         return -1;
      }
      fname = argv[2];
      srcname = argv[3];
   }
   else if (argc == 2) {
      srcname = argv[1];
   }
   else {
      usage();
      return -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;
   }

   if (has_locks(&srcstat)) {
      fprintf(stderr, "strip_locks: removing locks on %s\n", srcname);

      if (remove_locks(srcname, &srcstat)) {
         fprintf(stderr, "strip_locks: failed to remove lock on %s\n", srcname);
         return_value = -1;
      }
   }


   source_name_len = srcname_len;

   dirwalk(srcname, &srcstat, walk_state);

   return return_value;

}
