Beispiel #1
0
def deleteEntries(pLfnList, pKeepLfn, pRemoveLinks, pVerbose):
    """
    For each LFN in pLfnList, the contents of LFC_HOME env var are prepended
    for the relative paths (absoulte paths are used as they are), and
    deleteOneEntry is invoked.

    Please check deleteOneEntry for the meaning of the other arguments.
   """

    lfnList = pLfnList
    keepLfn = pKeepLfn
    removeLinks = pRemoveLinks
    verbose = pVerbose

    rc = 0

    for lfn in lfnList:
        lfn = lfn.strip()
        [lfn, dir] = checkHomeDir(lfn)
        err = lfc.lfc_chdir(dir)
        if (err < 0):
            sys.stderr.write("Error changing dir to: " + dir + " : " +
                             lfc.sstrerror(lfc.cvar.serrno) + "\n")
            return err
        err = deleteOneEntry(lfn, dir, keepLfn, removeLinks, verbose)

        if (err): rc = err

# Return the error code
    return rc
Beispiel #2
0
def deleteEntries(pLfnList, pKeepLfn, pRemoveLinks, pVerbose):
   """
    For each LFN in pLfnList, the contents of LFC_HOME env var are prepended
    for the relative paths (absoulte paths are used as they are), and
    deleteOneEntry is invoked.

    Please check deleteOneEntry for the meaning of the other arguments.
   """

   lfnList = pLfnList
   keepLfn = pKeepLfn
   removeLinks = pRemoveLinks
   verbose = pVerbose

   rc = 0

   for lfn in lfnList:
     lfn = lfn.strip()
     [lfn, dir] = checkHomeDir(lfn)
     err = lfc.lfc_chdir(dir)
     if(err < 0):
        sys.stderr.write("Error changing dir to: "+dir+" : "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
        return err
     err = deleteOneEntry(lfn, dir, keepLfn, removeLinks, verbose)

     if(err): rc=err
     
 # Return the error code
   return rc
Beispiel #3
0
def deleteOneDir(pDir, pForce, pExtLinks, pExtLfns, pVerbose):
    """
    Tries to delete the specified directory. If "pForce" is true, then the
    directory will be removed even if it is not empty and all the entries in
    the directory (including subdirectories) will be removed as well, in a
    recursive way. Otherwise, the deletion will be performed only if the
    directory is empty.

    If pExtLinks == pExtLfns == False, the function will remove only the
    specified directory contents and will not to  touch external sym links
    pointing to LFNs in the directory, nor entries pointed to by links located
    in the directory.

    If pExtLinks == True, then links located in other directories that point
    to LFNs in the specified directory are also removed.

    If pExtLfns == True, then LFNs (and their replicas and all their sym links)
    located in other directories and pointed to by links in the specified
    directory are also removed.
   """

    dir = pDir
    force = pForce
    extLinks = pExtLinks
    extLfns = pExtLfns
    verbose = pVerbose

    rc = 0
    err = 0

    subdirlist = [
    ]  # Subdirectories to remove when we are done with current one

    [dir, parentdir] = lfc_del_lfn.checkHomeDir(dir)
    fulldir = parentdir + dir

    if (verbose):
        print "--Deleting Dir: " + dir

    err = lfc.lfc_chdir(fulldir)
    if (err < 0):
        sys.stderr.write("Error changing dir to: " + fulldir + " : " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")
        return err

    dir_p = lfc.lfc_DIR()
    dir_entry = lfc.lfc_direnstatg()
    dir_p = lfc.lfc_opendirg(".", "")
    if (dir_p < 0):
        sys.stderr.write("Error opening specified dir: " + dir + ": " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")
        return -1

    # Read first entry
    lfc.lfc_rewinddir(dir_p)
    dir_entry = lfc.lfc_readdirg(dir_p)

    if (force):
        # Remove all LFNs in a loop
        S_IFDIR = 0x4000
        S_IFLNK = 0xA000

        while (dir_entry):
            lfn = dir_entry.d_name
            if (dir_entry.filemode & S_IFDIR):
                # This entry is a directory
                subdirlist.append(lfn)
            else:
                if ((dir_entry.filemode & S_IFLNK) == S_IFLNK):
                    # This entry is a sym link
                    if (extLfns):
                        # Remove the replicas and all the links (including main LFN)
                        err = lfc_del_lfn.deleteOneEntry(
                            lfn, fulldir, False, True, verbose)
                    else:
                        # Remove only the sym link (no replicas)
                        err = lfc_del_lfn.deleteOneLFN(lfn, verbose)
                else:
                    # This entry is a main LFN
                    #              # First check if the file has been alredy deleted (links, etc...)
                    #              fstat = lfc.lfc_filestatg()
                    #              if(lfc.lfc_statg(lfn, "", fstat)<0):
                    #                if(verbose):
                    #                   print "--Warning. Skipping deletion of non-accessible file:",lfn,":",\
                    #                          lfc.sstrerror(lfc.cvar.serrno)
                    #              else:
                    if (extLinks):
                        # Remove the replicas and all the links that point to this LFN
                        err = lfc_del_lfn.deleteOneEntry(
                            lfn, fulldir, False, True, verbose)
                    else:
                        # Remove only this LFN and replicas (but no external sym links)
                        err = lfc_del_lfn.deleteOneEntry(
                            lfn, fulldir, False, False, verbose)

            if (err): rc = err
            dir_entry = lfc.lfc_readdirg(dir_p)

    else:
        if (dir_entry):
            sys.stderr.write("Error: Directory " + dir +
                             " not empty! Consider using -r.\n")
            return -1

    # Close the directory
    if (lfc.lfc_closedir(dir_p) < 0):
        sys.stderr.write("Error closing dir: " + dir + " : " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")

    # Remove all subdirectories in the list
    for subdir in subdirlist:
        err = deleteOneDir(fulldir + '/' + subdir, force, extLinks, extLfns,
                           verbose)
        if (err): rc = err

    # Finally, remove also the top directory itself
    err = lfc.lfc_chdir("..")
    if (err < 0):
        sys.stderr.write("Error changing dir to \"..\" : " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")
        return err

    if (verbose):
        print "--lfc.lfc_unlink(\"" + dir + "\")"
    err = lfc.lfc_rmdir(dir)
    if (err < 0):
        sys.stderr.write("Error removing dir: " + dir + ": " +
                         lfc.sstrerror(lfc.cvar.serrno) + "\n")
        return err

# Return the error code


#   return rc
    return err
Beispiel #4
0
 def test(self):
     path = ""
     for i in xrange(0,1023+1):
         path=path + "a"
     ret=lfc.lfc_chdir(path)
     return ("", lfc.cvar.serrno, ret)
Beispiel #5
0
 def test(self):
     ret = lfc.lfc_chdir("/for_sure_non_existing_directory")
     return ((None,lfc.cvar.serrno), ret)
Beispiel #6
0
 def test(self):
     ret = lfc.lfc_chdir(self.path)
     path = "                     "
     lfc.lfc_getcwd(path,len(path))
     return (path.strip(),ret)
Beispiel #7
0
 def clean(self):
     lfc.lfc_chdir("/")
     lfc.lfc_rmdir(self.name)
Beispiel #8
0
 def prepare(self):
     self.name = "/grid/dteam/python_rmdir_test"
     lfc.lfc_mkdir(self.name,0755)
     lfc.lfc_chdir(self.name)
Beispiel #9
0
def deleteOneDir(pDir, pForce, pExtLinks, pExtLfns, pVerbose):
   """
    Tries to delete the specified directory. If "pForce" is true, then the
    directory will be removed even if it is not empty and all the entries in
    the directory (including subdirectories) will be removed as well, in a
    recursive way. Otherwise, the deletion will be performed only if the
    directory is empty.

    If pExtLinks == pExtLfns == False, the function will remove only the
    specified directory contents and will not to  touch external sym links
    pointing to LFNs in the directory, nor entries pointed to by links located
    in the directory.

    If pExtLinks == True, then links located in other directories that point
    to LFNs in the specified directory are also removed.

    If pExtLfns == True, then LFNs (and their replicas and all their sym links)
    located in other directories and pointed to by links in the specified
    directory are also removed.
   """

   dir = pDir
   force = pForce
   extLinks = pExtLinks 
   extLfns = pExtLfns
   verbose = pVerbose

   rc = 0
   err = 0

   subdirlist = [] # Subdirectories to remove when we are done with current one

   [dir, parentdir] = lfc_del_lfn.checkHomeDir(dir)
   fulldir = parentdir+dir

   if(verbose):
      print "--Deleting Dir: "+dir
      
   err = lfc.lfc_chdir(fulldir)
   if(err < 0):
      sys.stderr.write("Error changing dir to: "+fulldir+" : "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
      return err

   dir_p=lfc.lfc_DIR()
   dir_entry=lfc.lfc_direnstatg()
   dir_p=lfc.lfc_opendirg(".", "")
   if(dir_p < 0):
      sys.stderr.write("Error opening specified dir: "+dir+": "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
      return -1

   # Read first entry
   lfc.lfc_rewinddir(dir_p)
   dir_entry=lfc.lfc_readdirg(dir_p)

   if(force):
      # Remove all LFNs in a loop
      S_IFDIR = 0x4000   
      S_IFLNK = 0xA000  

      while(dir_entry):
         lfn = dir_entry.d_name
         if(dir_entry.filemode & S_IFDIR):
         # This entry is a directory
            subdirlist.append(lfn)
         else:
           if((dir_entry.filemode & S_IFLNK) == S_IFLNK):
           # This entry is a sym link
              if(extLfns):
                 # Remove the replicas and all the links (including main LFN)
                 err = lfc_del_lfn.deleteOneEntry(lfn, fulldir, False, True, verbose)
              else:
                 # Remove only the sym link (no replicas)
                 err = lfc_del_lfn.deleteOneLFN(lfn, verbose)
           else:
           # This entry is a main LFN
#              # First check if the file has been alredy deleted (links, etc...)
#              fstat = lfc.lfc_filestatg()
#              if(lfc.lfc_statg(lfn, "", fstat)<0):
#                if(verbose):
#                   print "--Warning. Skipping deletion of non-accessible file:",lfn,":",\
#                          lfc.sstrerror(lfc.cvar.serrno)
#              else:
                 if(extLinks):
                    # Remove the replicas and all the links that point to this LFN
                    err = lfc_del_lfn.deleteOneEntry(lfn, fulldir, False, True, verbose)
                 else:
                    # Remove only this LFN and replicas (but no external sym links)
                    err = lfc_del_lfn.deleteOneEntry(lfn, fulldir, False, False, verbose)
            
         if(err): rc = err
         dir_entry=lfc.lfc_readdirg(dir_p)
      
   else:
      if(dir_entry):
         sys.stderr.write("Error: Directory "+dir+" not empty! Consider using -r.\n")
         return -1

   # Close the directory
   if(lfc.lfc_closedir(dir_p) < 0):
      sys.stderr.write("Error closing dir: "+dir+" : "+lfc.sstrerror(lfc.cvar.serrno)+"\n")

   # Remove all subdirectories in the list 
   for subdir in subdirlist:
      err = deleteOneDir(fulldir+'/'+subdir, force, extLinks, extLfns, verbose)
      if(err): rc=err
 
   # Finally, remove also the top directory itself 
   err = lfc.lfc_chdir("..")
   if(err < 0):
      sys.stderr.write("Error changing dir to \"..\" : "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
      return err

   if(verbose):
      print "--lfc.lfc_unlink(\""+dir+"\")"
   err = lfc.lfc_rmdir(dir)
   if(err<0):
      sys.stderr.write("Error removing dir: "+dir+": "+lfc.sstrerror(lfc.cvar.serrno)+"\n")
      return err

 # Return the error code
#   return rc
   return err