Example #1
0
def is_file_valid(myfile="", message_level="normal"):
    """
    Check if the file exists
    Check if it is a file
    Check if it is non-zero
    
    @author: Roy Nielsen
    """
    is_valid = True

    if re.match("^\s*$", str(myfile)):
        log_message("Cannot process file: \"" + str(myfile) + "\"", "normal",
                    message_level)
        is_valid = False
    else:
        if not os.path.exists(str(myfile)):
            log_message("File: \"" + str(myfile) + "\" does not exist...",
                        "normal", message_level)
            is_valid = False
        else:
            if not os.path.isfile(str(myfile)):
                log_message("File: \"" + str(file) + "\" is not a file...",
                            "normal", message_level)
                is_valid = False
            else:
                if not os.stat(myfile).st_size > 0:
                    log_message(
                        "I think \"" + str(file) + "\" is an empty file...",
                        "normal", message_level)
                    is_valid = False
                else:
                    is_valid = True

    return is_valid
Example #2
0
def main():
    """
    Main program
    
    @author: Roy Nielsen
    """
    program_options = ProgramOptions()
    message_level = program_options.get_message_level()

    log_message("Successfully acquired program options.", "verbose",
                message_level)

    flsfile = do_fls(program_options.get_fs_type(), \
              program_options.get_source(), \
              program_options.get_fls(), \
              program_options.get_message_level())

    log_message("Completed fls command.", "verbose", message_level)

    do_icat(flsfile, \
            program_options.get_fs_type(), \
            program_options.get_source(), \
            program_options.get_target(), \
            program_options.get_root_dir(), \
            program_options.get_icat(), \
            program_options.get_message_level())

    log_message("Completed icat command.", "verbose", message_level)

    log_message(sys.argv[0] + " done...", "verbose", message_level)
Example #3
0
def is_file_valid(myfile="", message_level="normal") :
    """
    Check if the file exists
    Check if it is a file
    Check if it is non-zero
    
    @author: Roy Nielsen
    """
    is_valid = True
    
    if re.match("^\s*$", str(myfile)) :
        log_message("Cannot process file: \"" + str(myfile) + "\"", "normal", message_level)
        is_valid = False
    else :
        if not os.path.exists(str(myfile)) :
            log_message("File: \"" + str(myfile) + "\" does not exist...", "normal", message_level)
            is_valid = False
        else :
            if not os.path.isfile(str(myfile)) :
                log_message("File: \"" + str(file) + "\" is not a file...", "normal", message_level)
                is_valid = False
            else :
                if not os.stat(myfile).st_size > 0 :
                    log_message("I think \"" + str(file) + "\" is an empty file...", "normal", message_level)
                    is_valid = False
                else :
                    is_valid = True
                
    return is_valid
Example #4
0
def main() :
    """
    Main program
    
    @author: Roy Nielsen
    """
    program_options = ProgramOptions()
    message_level = program_options.get_message_level()
    
    log_message("Successfully acquired program options.", "verbose", message_level)
    
    flsfile = do_fls(program_options.get_fs_type(), \
              program_options.get_source(), \
              program_options.get_fls(), \
              program_options.get_message_level())
    
    log_message("Completed fls command.", "verbose", message_level)
    
    do_icat(flsfile, \
            program_options.get_fs_type(), \
            program_options.get_source(), \
            program_options.get_target(), \
            program_options.get_root_dir(), \
            program_options.get_icat(), \
            program_options.get_message_level())
    
    log_message("Completed icat command.", "verbose", message_level)
    
    log_message(sys.argv[0] + " done...", "verbose", message_level)
Example #5
0
def create_directory(name="", target="", message_level="normal"):
    """
    Create a directory in the target based on the name passed in.
    
    @author: Roy Nielsen
    """
    #####
    # Check for valid parameters passed in...
    if re.match("^\s*$", name) or re.match("^\s*$", target):
        log_message("Sorry, you need a valid name and target...", "normal",
                    message_level)
        log_message("name passed in: \"" + str(name) + "\"", "normal",
                    message_level)
        log_message("target passed in: \"" + str(target) + "\"", "normal",
                    message_level)
    else:
        #####
        # create list of dirs in dirname (if directory name passed in is something
        # like one/two/three
        dirs = name.split("/")

        if len(dirs) > 0:
            path = ""
            #####
            # Loop through dirs
            for dir in dirs:
                path = os.path.join(path, dir)
                full_path = os.path.join(target, path)

                #####
                # Check if name exists on target
                if os.path.exists(full_path):
                    if not os.path.isdir(full_path):
                        #####
                        # Log error and return...
                        log_message("ERROR: name: \"" + str(full_path) + "\" " + \
                        "exists, and is not a directory... Skipping...", \
                        "normal", message_level)
                        break
                    else:
                        #####
                        # go to the next level and try again...
                        continue
                else:
                    try:
                        #####
                        # path does not exist, so let's create it...
                        os.makedirs(full_path, 0755)
                    except Exception, err:
                        log_message("Exception attempting to create directory:" + \
                                    " \"" + str(full_path) + "\"", "normal", message_level)
                        log_message("Associated exception: \"" + str(err) + "\"", \
                                    message_level)
Example #6
0
def is_root_dir_found(type1="",
                      type2="",
                      filename="",
                      root_dir="",
                      message_level="normal"):
    """
    Look for the root_dir in the filename full path...
    
    Returns True if dir found in filename full path
            False if dir not found in filename full path
            
    @author: Roy Nielsen
    """
    found = False
    if re.match("^\s*$", type1) or \
       re.match("^\s*$", type2) or \
       re.match("^\s*$", filename) or \
       re.match("^\s*$", root_dir) :
        log_message("Cannot process looking for the \"root dir\" with empty " + \
                    "parameters...", "normal", message_level)
        log_message("type1:\t " + str(type1), "normal", message_level)
        log_message("type2:\t " + str(type2), "normal", message_level)
        log_message("filename:\t " + str(filename), "normal", message_level)
        log_message("root_dir:\t " + str(root_dir), "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(250)
    else:
        dirs = filename.split("/")
        if len(dirs) > 1:
            if not re.match("^d$", type1) and not re.match("^d$", type2):
                dirs = dirs[:-1]
            for dir in dirs:
                if re.match("%s" % dir, root_dir):
                    found = True
                    break

    return found
Example #7
0
    # -pr = p is report full path, r is recursive, d is only deleted files
    command = [fls, "-prd", "-f", filesystem, source]
    retval = ""
    reterr = ""

    log_message("command: " + str(command), "debug", message_level)

    try:
        (sfile_descriptor, sfile_name) = tempfile.mkstemp()
    except Exception, err:
        log_message("Exception making temporary directory: " + str(err), \
                    "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(254)
    else:
        log_message("tmpfile: \"" + str(sfile_name) + "\"", "debug",
                    message_level)
        try:
            sfile_handle = open(sfile_name, "wb")
        except Exception, err:
            log_message("Problem opening file: \"" + str(sfile_name) + "\"",
                        "normal", message_level)
            log_message("Associated exception: \"" + str(err) + "\"", "normal",
                        message_level)
            log_message("Exiting...", "normal", message_level)
            sys.exit(254)
        else:
            try:
                #####
                # Run the FLS command sending stdout to the "sfile_handle" filehandle,
                # opened above.
                reterr = Popen(command, stdout=sfile_handle,
Example #8
0
def do_fls(filesystem="", source="", fls="", message_level="normal") :
    """
    Perform fls operations on the bitstream image, writing a temporary file
    with the fls output.
    
    Parameters: program_options = ProgramOptions class, instanciated in calling
                                  function.
                                  
    Returns: temp_file = full path to temp file where fls information has been
                         collected
    
    @author: Roy Nielsen
    """    
    if re.match("^\s*$", str(filesystem)) or \
       re.match("^\s*$", str(source)) or \
       re.match("^\s*$", str(fls)) :
        log_message("Cannot process flsfile with any empty parameters:", \
                    "normal", message_level)
        log_message("filesystem:\t\"" + str(filesystem) + "\"", "normal", message_level)
        log_message("source:\t\"" + str(source) + "\"", "normal", message_level)
        log_message("fls:\t\"" + str(fls) + "\"", "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(253)
        
    #####
    # -pr = p is report full path, r is recursive, d is only deleted files
    command = [fls, "-prd", "-f", filesystem, source]
    retval = ""
    reterr = ""    
    
    log_message("command: " + str(command), "debug", message_level)
    
    try :
        (sfile_descriptor, sfile_name) = tempfile.mkstemp() 
    except Exception, err :
        log_message("Exception making temporary directory: " + str(err), \
                    "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(254)
Example #9
0
    def get_icat(self) :
        """
        Get the location of the icat command, if not in the default location.
        
        FUTURE MODIFICAITON - better input validation
        
        @author: Roy Nielsen
        """
        if re.match("^\s*$", self.opts.icat) :
            icats = ["/usr/local/bin/icat"]
            for icat_path in icats :
                if os.path.exists(icat_path):
                    icat = icat_path
                    break;

            if re.match("^\s$", self.opts.icat):
                log_message("No valid path for icat...", "normal", self.message_level)
                log_message("Exiting...", "normal", self.message_level)
                sys.exit(98)
            
        else :
            if os.path.exists(self.opts.icat) :
                #####
                # Make sure the last item in the path is "icat"
                icat_path = self.opts.icat.split("/")[-1]
                if re.match("^icat$", self.opts.icat) :
                    icat = self.opts.icat
                else :
                    log_message("Looking for icat, not: \"" + str(icat_path) + \
                                "\"", "normal", self.message_level)
                    log_message("Exiting...", "normal", self.message_level)
                    sys.exit(97)
            else :
                log_message("No valid path for icat...", "normal", self.message_level)
                log_message("Exiting...", "normal", self.message_level)
                sys.exit(96)

        return icat
Example #10
0
def recover_file(node="", 
                 filename="",
                 flsfile="",
                 filesystem="", 
                 source="", 
                 target="", 
                 rootdir="", 
                 icat="",
                 message_level="normal") :
    """
    Recover the filename passed in to the target.
    
    @author: Roy Nielsen
    """
    if re.match("^\s*$", str(node)) or \
       re.match("^\s*$", str(filename)) or \
       re.match("^\s*$", str(flsfile)) or \
       re.match("^\s*$", str(filesystem)) or \
       re.match("^\s*$", str(source)) or \
       re.match("^\s*$", str(target)) or \
       re.match("^\s*$", str(rootdir)) or \
       re.match("^\s*$", str(icat)) :
        log_message("Cannot recover filename: \"" + str(filename) + \
                    "\" with any empty parameters:", \
                    "normal", message_level)
        log_message("node:\t\"" + str(node) + "\"", "normal", message_level)
        log_message("filename:\t\"" + str(filename) + "\"", "normal", message_level)
        log_message("flsfile:\t\"" + str(flsfile) + "\"", "normal", message_level)
        log_message("filesystem:\t\"" + str(filesystem) + "\"", "normal", message_level)
        log_message("source:\t\"" + str(source) + "\"", "normal", message_level)
        log_message("target:\t\"" + str(target) + "\"", "normal", message_level)
        log_message("rootdir:\t\"" + str(rootdir) + "\"", "normal", message_level)
        log_message("icat:\t\"" + str(icat) + "\"", "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(252)
        
    else :
        if os.path.exists(str(icat)) and os.path.isfile(str(icat)) :
            
            #####
            # Create directories on the target if they don't exist...
            dirs = filename.split("/")[:-1]
            if dirs :
                dir = "/".join(dirs)
                log_message("Directory of file to restore is: \"" + str(dir) + \
                            "\"...", "verbose", message_level)

                create_directory(dir, target, message_level)
            
            #####
            # open a filehandle to the file that we want subprocess to write to...
            try :
                full_file_path = os.path.join(str(target), str(filename))
                fh = open(str(full_file_path), "wb")
            except Exception, err :
                log_message(" Cannot open file...", "normal", message_level)
                log_message("file: \"" + full_file_path + "\"", "normal", message_level)
                log_message("Associated exception: \"" + str(err) + "\"", \
                            "normal", message_level)
                if re.search("[N|n][O|o] [S|s]pace [L|l]eft", str(err)) :
                    log_message("It appears there is no space left on " + \
                                "target: \"" + str(target) + "\"", \
                                "normal", message_level)
                    log_message("Exiting...", "normal", message_level)
                    sys.exit(251)
            else :
                #####
                # Set up the icat recovery command 
                # THIS MAY BE DANGEROUS WITH VERY LARGE FILES....
                command = [icat, "-f", str(filesystem), source, node]
                log_message("command: \"" + " ".join(command) + "\"", "verbose", message_level)
                try :
                    reterr = Popen(command, stdout=fh, stderr=PIPE).communicate()[1]
                    if reterr :
                        log_message("icat spawned with subprocess returned " + \
                                    "this: \"" + str(reterr) + "\"", \
                                    "normal", message_level)
                except Exception, err :
                    log_message("Exception attemting to restore file: \"" + \
                                str(filename) + "\" with exception: \"" + \
                                str(err), "normal", message_level)
                else :
Example #11
0
def create_directory(name="", target="", message_level="normal") :
    """
    Create a directory in the target based on the name passed in.
    
    @author: Roy Nielsen
    """
    #####
    # Check for valid parameters passed in...
    if re.match("^\s*$", name) or re.match("^\s*$", target) :
        log_message("Sorry, you need a valid name and target...", "normal", message_level)
        log_message("name passed in: \"" + str(name) + "\"", "normal", message_level)
        log_message("target passed in: \"" + str(target) + "\"", "normal", message_level)
    else :
        #####
        # create list of dirs in dirname (if directory name passed in is something 
        # like one/two/three
        dirs = name.split("/")
        
        if len(dirs) > 0 :
            path = ""
            #####
            # Loop through dirs
            for dir in dirs :
                path = os.path.join(path, dir)
                full_path = os.path.join(target, path)
                
                #####
                # Check if name exists on target
                if os.path.exists(full_path) :
                    if not os.path.isdir(full_path) :
                        #####
                        # Log error and return...
                        log_message("ERROR: name: \"" + str(full_path) + "\" " + \
                        "exists, and is not a directory... Skipping...", \
                        "normal", message_level)
                        break
                    else :
                        #####
                        # go to the next level and try again...
                        continue
                else :
                    try :
                        #####
                        # path does not exist, so let's create it...
                        os.makedirs(full_path, 0755)
                    except Exception, err :
                        log_message("Exception attempting to create directory:" + \
                                    " \"" + str(full_path) + "\"", "normal", message_level)
                        log_message("Associated exception: \"" + str(err) + "\"", \
                                    message_level)
Example #12
0
def is_root_dir_found(type1="", type2="", filename="", root_dir="", message_level="normal") :
    """
    Look for the root_dir in the filename full path...
    
    Returns True if dir found in filename full path
            False if dir not found in filename full path
            
    @author: Roy Nielsen
    """
    found = False
    if re.match("^\s*$", type1) or \
       re.match("^\s*$", type2) or \
       re.match("^\s*$", filename) or \
       re.match("^\s*$", root_dir) :
        log_message("Cannot process looking for the \"root dir\" with empty " + \
                    "parameters...", "normal", message_level)
        log_message("type1:\t " + str(type1), "normal", message_level)
        log_message("type2:\t " + str(type2), "normal", message_level)
        log_message("filename:\t " + str(filename), "normal", message_level)
        log_message("root_dir:\t " + str(root_dir), "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(250)
    else :
        dirs = filename.split("/")
        if len(dirs) > 1 :
            if not re.match("^d$", type1) and not re.match("^d$", type2) :
                dirs = dirs[:-1]
            for dir in dirs :
                if re.match("%s"%dir, root_dir) :
                    found = True
                    break

    return found
Example #13
0
 # -pr = p is report full path, r is recursive, d is only deleted files
 command = [fls, "-prd", "-f", filesystem, source]
 retval = ""
 reterr = ""    
 
 log_message("command: " + str(command), "debug", message_level)
 
 try :
     (sfile_descriptor, sfile_name) = tempfile.mkstemp() 
 except Exception, err :
     log_message("Exception making temporary directory: " + str(err), \
                 "normal", message_level)
     log_message("Exiting...", "normal", message_level)
     sys.exit(254)
 else:
     log_message("tmpfile: \"" + str(sfile_name) + "\"", "debug", message_level)
     try :
         sfile_handle = open(sfile_name, "wb")
     except Exception, err :
         log_message("Problem opening file: \"" + str(sfile_name) + "\"", "normal", message_level)
         log_message("Associated exception: \"" + str(err) + "\"", "normal", message_level)
         log_message("Exiting...", "normal", message_level)
         sys.exit(254)
     else :
         try :
             #####
             # Run the FLS command sending stdout to the "sfile_handle" filehandle,
             # opened above.
             reterr = Popen(command, stdout=sfile_handle, stderr=PIPE).communicate()[1]
         except Exception, err :
             log_message("Error attempting to run fls...", "normal", message_level)
Example #14
0
    def get_icat(self):
        """
        Get the location of the icat command, if not in the default location.
        
        FUTURE MODIFICAITON - better input validation
        
        @author: Roy Nielsen
        """
        if re.match("^\s*$", self.opts.icat):
            icats = ["/usr/local/bin/icat"]
            for icat_path in icats:
                if os.path.exists(icat_path):
                    icat = icat_path
                    break

            if re.match("^\s$", self.opts.icat):
                log_message("No valid path for icat...", "normal",
                            self.message_level)
                log_message("Exiting...", "normal", self.message_level)
                sys.exit(98)

        else:
            if os.path.exists(self.opts.icat):
                #####
                # Make sure the last item in the path is "icat"
                icat_path = self.opts.icat.split("/")[-1]
                if re.match("^icat$", self.opts.icat):
                    icat = self.opts.icat
                else:
                    log_message("Looking for icat, not: \"" + str(icat_path) + \
                                "\"", "normal", self.message_level)
                    log_message("Exiting...", "normal", self.message_level)
                    sys.exit(97)
            else:
                log_message("No valid path for icat...", "normal",
                            self.message_level)
                log_message("Exiting...", "normal", self.message_level)
                sys.exit(96)

        return icat
Example #15
0
    def get_fls(self):
        """
        Get the location of the fls command, if not in the default location.
        
        FUTURE MODIFICAITON - better input validation
        
        @author: Roy Nielsen
        """
        if re.match("^\s*$", self.opts.fls):
            flss = ["/usr/local/bin/fls"]
            for fls_path in flss:
                if os.path.exists(fls_path):
                    fls = fls_path
                    break

            if re.match("^\s$", self.opts.fls):
                log_message("No valid path for fls...", "normal",
                            self.message_level)
                log_message("Exiting...", "normal", self.message_level)
                sys.exit(100)

        else:
            if os.path.exists(self.opts.fls):
                #####
                # Make sure the last item in the path is "fls"
                fls_path = self.opts.fls.split("/")[-1]
                if re.match("^fls$", self.opts.fls):
                    fls = self.opts.fls
                else:
                    log_message("Looking for fls, not: \"" + str(fls_path) + \
                                "\"", "normal", self.message_level)
                    log_message("Exiting...", "normal", self.message_level)
                    sys.exit(95)
            else:
                log_message("No valid path for fls...", "normal",
                            self.message_level)
                log_message("Exiting...", "normal", self.message_level)
                sys.exit(99)

        return fls
Example #16
0
def recover_file(node="",
                 filename="",
                 flsfile="",
                 filesystem="",
                 source="",
                 target="",
                 rootdir="",
                 icat="",
                 message_level="normal"):
    """
    Recover the filename passed in to the target.
    
    @author: Roy Nielsen
    """
    if re.match("^\s*$", str(node)) or \
       re.match("^\s*$", str(filename)) or \
       re.match("^\s*$", str(flsfile)) or \
       re.match("^\s*$", str(filesystem)) or \
       re.match("^\s*$", str(source)) or \
       re.match("^\s*$", str(target)) or \
       re.match("^\s*$", str(rootdir)) or \
       re.match("^\s*$", str(icat)) :
        log_message("Cannot recover filename: \"" + str(filename) + \
                    "\" with any empty parameters:", \
                    "normal", message_level)
        log_message("node:\t\"" + str(node) + "\"", "normal", message_level)
        log_message("filename:\t\"" + str(filename) + "\"", "normal",
                    message_level)
        log_message("flsfile:\t\"" + str(flsfile) + "\"", "normal",
                    message_level)
        log_message("filesystem:\t\"" + str(filesystem) + "\"", "normal",
                    message_level)
        log_message("source:\t\"" + str(source) + "\"", "normal",
                    message_level)
        log_message("target:\t\"" + str(target) + "\"", "normal",
                    message_level)
        log_message("rootdir:\t\"" + str(rootdir) + "\"", "normal",
                    message_level)
        log_message("icat:\t\"" + str(icat) + "\"", "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(252)

    else:
        if os.path.exists(str(icat)) and os.path.isfile(str(icat)):

            #####
            # Create directories on the target if they don't exist...
            dirs = filename.split("/")[:-1]
            if dirs:
                dir = "/".join(dirs)
                log_message("Directory of file to restore is: \"" + str(dir) + \
                            "\"...", "verbose", message_level)

                create_directory(dir, target, message_level)

            #####
            # open a filehandle to the file that we want subprocess to write to...
            try:
                full_file_path = os.path.join(str(target), str(filename))
                fh = open(str(full_file_path), "wb")
            except Exception, err:
                log_message(" Cannot open file...", "normal", message_level)
                log_message("file: \"" + full_file_path + "\"", "normal",
                            message_level)
                log_message("Associated exception: \"" + str(err) + "\"", \
                            "normal", message_level)
                if re.search("[N|n][O|o] [S|s]pace [L|l]eft", str(err)):
                    log_message("It appears there is no space left on " + \
                                "target: \"" + str(target) + "\"", \
                                "normal", message_level)
                    log_message("Exiting...", "normal", message_level)
                    sys.exit(251)
            else:
                #####
                # Set up the icat recovery command
                # THIS MAY BE DANGEROUS WITH VERY LARGE FILES....
                command = [icat, "-f", str(filesystem), source, node]
                log_message("command: \"" + " ".join(command) + "\"",
                            "verbose", message_level)
                try:
                    reterr = Popen(command, stdout=fh,
                                   stderr=PIPE).communicate()[1]
                    if reterr:
                        log_message("icat spawned with subprocess returned " + \
                                    "this: \"" + str(reterr) + "\"", \
                                    "normal", message_level)
                except Exception, err:
                    log_message("Exception attemting to restore file: \"" + \
                                str(filename) + "\" with exception: \"" + \
                                str(err), "normal", message_level)
                else:
Example #17
0
def do_icat(flsfile="",
            filesystem="",
            source="",
            target="",
            rootdir="",
            icat="",
            message_level="normal"):
    """
    Perform icat operations on the bitstream image.

    @author: Roy Nielsen
    """
    if re.match("^\s*$", str(flsfile)) or \
       re.match("^\s*$", str(filesystem)) or \
       re.match("^\s*$", str(source)) or \
       re.match("^\s*$", str(target)) or \
       re.match("^\s*$", str(rootdir)) or \
       re.match("^\s*$", str(icat)) :
        log_message("Cannot process flsfile with any empty parameters:", \
                    "normal", message_level)
        log_message("flsfile:\t\"" + str() + "\"", "normal", message_level)
        log_message("filesystem:\t\"" + str() + "\"", "normal", message_level)
        log_message("source:\t\"" + str() + "\"", "normal", message_level)
        log_message("target:\t\"" + str() + "\"", "normal", message_level)
        log_message("rootdir:\t\"" + str() + "\"", "normal", message_level)
        log_message("icat:\t\"" + str() + "\"", "normal", message_level)
        log_message(": \"" + str() + "\"", "normal", message_level)
        log_message(": \"" + str() + "\"", "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(253)

    if is_file_valid(str(flsfile), message_level):
        #####
        # Set up regular expression
        if re.match("^fat$", str(filesystem)):
            reline = re.compile("(\w)/(\w)\s+\S+\s+(\d+):(\s+)(\S.*)")
        elif re.match("^hfs$", str(filesystem)):
            reline = re.compile("(\w)/(\w)\s+(\d+):(\s+)(\S.*)")

        #####
        # Processing FLS file...
        # Read the file line by line - with fileinput - reads one line, so
        # you can process it, then throws it away so you can read the next line,
        # and so on.  Use this so you don't have to load the whole file into
        # memory
        for line in fileinput.input([flsfile]):
            file_info = reline.match(line)
            if file_info:
                col1 = file_info.group(1)
                col2 = file_info.group(2)
                node = file_info.group(3)
                spaces = file_info.group(4)
                filename = file_info.group(5)

                #log_message(str(col1) + "/" + str(col2) + " " + str(node) + ":" + \
                #            str(spaces) + str(filename), "debug", message_level)

                if is_root_dir_found(col1, col2, filename, rootdir,
                                     message_level):
                    if re.match("^d$", col1) and re.match("^d$", col2):
                        #####
                        # If the fls entry is a directory create it.
                        log_message("Creating directory: \"" + filename + "\"",
                                    "verbose", message_level)
                        create_directory(filename, target, message_level)

                    #####
                    # if re.match("^?????$", col1 and re.match("^???$", col2) :
                    #####
                    # If the fls entry is a link??? Hard vs Soft??

                    if re.match("^r$", col1) and re.match("^r$", col2):
                        #####
                        # If the fls entry is a file, recover it.  Call function to do so.
                        recover_file(node, filename, flsfile, filesystem,
                                     source, target, rootdir, icat,
                                     message_level)
            else:
                #log_message("Line does not match...", "verbose", message_level)
                #log_message("line: \"" + str(line) + "\"", "debug", message_level)
                pass
        log_message(
            "Done with recovery of files with rootdir of: " + str(rootdir),
            "normal", message_level)
    else:
        log_message("Cannot find the flsfile: \"" + str(flsfile) + "\"",
                    "normal", message_level)
Example #18
0
def do_icat(flsfile="", 
            filesystem="", 
            source="", 
            target="", 
            rootdir="", 
            icat ="", 
            message_level="normal") :
    """
    Perform icat operations on the bitstream image.

    @author: Roy Nielsen
    """
    if re.match("^\s*$", str(flsfile)) or \
       re.match("^\s*$", str(filesystem)) or \
       re.match("^\s*$", str(source)) or \
       re.match("^\s*$", str(target)) or \
       re.match("^\s*$", str(rootdir)) or \
       re.match("^\s*$", str(icat)) :
        log_message("Cannot process flsfile with any empty parameters:", \
                    "normal", message_level)
        log_message("flsfile:\t\"" + str() + "\"", "normal", message_level)
        log_message("filesystem:\t\"" + str() + "\"", "normal", message_level)
        log_message("source:\t\"" + str() + "\"", "normal", message_level)
        log_message("target:\t\"" + str() + "\"", "normal", message_level)
        log_message("rootdir:\t\"" + str() + "\"", "normal", message_level)
        log_message("icat:\t\"" + str() + "\"", "normal", message_level)
        log_message(": \"" + str() + "\"", "normal", message_level)
        log_message(": \"" + str() + "\"", "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(253)
        
    if is_file_valid(str(flsfile), message_level) :
        #####
        # Set up regular expression
        if re.match("^fat$", str(filesystem)) :
            reline = re.compile("(\w)/(\w)\s+\S+\s+(\d+):(\s+)(\S.*)")
        elif re.match("^hfs$", str(filesystem)) :
            reline = re.compile("(\w)/(\w)\s+(\d+):(\s+)(\S.*)")

        #####
        # Processing FLS file...
        # Read the file line by line - with fileinput - reads one line, so
        # you can process it, then throws it away so you can read the next line,
        # and so on.  Use this so you don't have to load the whole file into
        # memory
        for line in fileinput.input([flsfile]) :
            file_info = reline.match(line)
            if file_info :
                col1 = file_info.group(1)
                col2 = file_info.group(2)
                node = file_info.group(3)
                spaces = file_info.group(4)
                filename = file_info.group(5)
    
                #log_message(str(col1) + "/" + str(col2) + " " + str(node) + ":" + \
                #            str(spaces) + str(filename), "debug", message_level)
    
                if is_root_dir_found(col1, col2, filename, rootdir, message_level) :
                    if re.match("^d$", col1) and re.match("^d$", col2) :
                        #####
                        # If the fls entry is a directory create it.
                        log_message("Creating directory: \"" + filename + "\"", "verbose", message_level)
                        create_directory(filename, target, message_level)
    
                    #####
                    # if re.match("^?????$", col1 and re.match("^???$", col2) :
                        #####
                        # If the fls entry is a link??? Hard vs Soft??
                    
                    if re.match("^r$", col1) and re.match("^r$", col2) :
                        #####
                        # If the fls entry is a file, recover it.  Call function to do so.
                        recover_file(node, filename, flsfile, filesystem, source, target, rootdir, icat, message_level)
            else :
                #log_message("Line does not match...", "verbose", message_level)
                #log_message("line: \"" + str(line) + "\"", "debug", message_level)
                pass       
        log_message("Done with recovery of files with rootdir of: " + str(rootdir), "normal", message_level)
    else :
        log_message("Cannot find the flsfile: \"" + str(flsfile) + "\"", "normal", message_level)
Example #19
0
def do_fls(filesystem="", source="", fls="", message_level="normal"):
    """
    Perform fls operations on the bitstream image, writing a temporary file
    with the fls output.
    
    Parameters: program_options = ProgramOptions class, instanciated in calling
                                  function.
                                  
    Returns: temp_file = full path to temp file where fls information has been
                         collected
    
    @author: Roy Nielsen
    """
    if re.match("^\s*$", str(filesystem)) or \
       re.match("^\s*$", str(source)) or \
       re.match("^\s*$", str(fls)) :
        log_message("Cannot process flsfile with any empty parameters:", \
                    "normal", message_level)
        log_message("filesystem:\t\"" + str(filesystem) + "\"", "normal",
                    message_level)
        log_message("source:\t\"" + str(source) + "\"", "normal",
                    message_level)
        log_message("fls:\t\"" + str(fls) + "\"", "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(253)

    #####
    # -pr = p is report full path, r is recursive, d is only deleted files
    command = [fls, "-prd", "-f", filesystem, source]
    retval = ""
    reterr = ""

    log_message("command: " + str(command), "debug", message_level)

    try:
        (sfile_descriptor, sfile_name) = tempfile.mkstemp()
    except Exception, err:
        log_message("Exception making temporary directory: " + str(err), \
                    "normal", message_level)
        log_message("Exiting...", "normal", message_level)
        sys.exit(254)
Example #20
0
    def get_fls(self) :
        """
        Get the location of the fls command, if not in the default location.
        
        FUTURE MODIFICAITON - better input validation
        
        @author: Roy Nielsen
        """
        if re.match("^\s*$", self.opts.fls) :
            flss = ["/usr/local/bin/fls"]
            for fls_path in flss :
                if os.path.exists(fls_path):
                    fls = fls_path
                    break;

            if re.match("^\s$", self.opts.fls):
                log_message("No valid path for fls...", "normal", self.message_level)
                log_message("Exiting...", "normal", self.message_level)
                sys.exit(100)
            
        else :
            if os.path.exists(self.opts.fls) :
                #####
                # Make sure the last item in the path is "fls"
                fls_path = self.opts.fls.split("/")[-1]
                if re.match("^fls$", self.opts.fls) :
                    fls = self.opts.fls
                else :
                    log_message("Looking for fls, not: \"" + str(fls_path) + \
                                "\"", "normal", self.message_level)
                    log_message("Exiting...", "normal", self.message_level)
                    sys.exit(95)
            else :
                log_message("No valid path for fls...", "normal", self.message_level)
                log_message("Exiting...", "normal", self.message_level)
                sys.exit(99)

        return fls