Пример #1
0
    def process_IN_MOVED_TO(self, event):
        """
            Note about how the IN_MOVED_TO event works:
            when a dir/file is moved, if it's source location is being monitored
            by inotify, there will be an attribute in the event called src_pathname.
            if the dir/file was moved from somewhere outside of pyinotify's watch,
            the src_pathname attribute won't exist.
        """
        log.debug(event)
        self.check_last_moved(event)

        # event.src_pathname will only exist if file was moved from a
        # directory that is being watched
        try:
            src_full_path = event.src_pathname
        except AttributeError:
            src_full_path = None

        dest_full_path = event.pathname
        dest_filename = event.name
        if not is_ignored_file(dest_filename) and not is_ignored_directory(dest_full_path):
            if src_full_path:
                # append / so split_path knows it's input is a directory
                if event.dir:
                    src_full_path += os.sep
                    dest_full_path += os.sep

                src_split_path = split_path(args["watch_dirs"], src_full_path)
                dest_split_path = split_path(args["watch_dirs"], dest_full_path)
                log.debug("src_split_path    = {0}".format(src_split_path))
                log.debug("dest_split_path    = {0}".format(dest_split_path))

                if event.dir:
                    log.info("directory '{0}' has been moved to '{1}', updating.".format(src_full_path,
                                                                                         dest_full_path))
                    rows_changed = self.db.move_directory(src_split_path, dest_split_path)
                else:
                    log.info("{0} has been moved to {1}, updating.".format(src_full_path,
                                                                           dest_full_path))
                    rows_changed = self.db.move_file(src_split_path, dest_split_path)

                log.debug("rows_changed = {0}".format(rows_changed))
                if rows_changed == 0:
                    # since update was unsuccessful, it's presumed that the original
                    # file was not in the database, so we'll just insert it instead
                    log.debug("no rows were changed during UPDATE, inserting {0} into database.".format(dest_full_path))

                    if event.dir: scan_dir(self.db, dest_full_path)
                    else: add_file(self.db, dest_full_path)
            else:
                if event.dir: scan_dir(self.db, dest_full_path)
                else: add_file(self.db, dest_full_path)
        else:
            # since either the file or the path of the file was flagged as ignored,
            # it's not going to be updated in the database. therefor,
            # the src file should just be deleted from the database
            if src_full_path: self.db.delete_file(src_full_path)
Пример #2
0
    def delete_file(self, full_path):
        (watch_dir, path, filename) = split_path(config.args["watch_dirs"], full_path)

        # get id
        data = self.get_fields(watch_dir, path, filename, ["id"])

        if data:
            id = data[0][0]
            log.info("removing {0} from the database.".format(filename))
            self.delete_id(id)
        else:
            log.debug("file '{0}' not found in database.".format(full_path))
Пример #3
0
    def delete_directory(self, d):
        self._check_connection()
        d = d.rstrip(os.sep) + os.sep # append os.sep so split_path knows its a dir
        watch_dir, path = split_path(config.args["watch_dirs"], d)[0:2]

        ########################################################################
        # query = """DELETE FROM `archive` WHERE `watch_dir` = '{0}' AND
        #        `path` REGEXP '{1}(\/|$)'""".format(watch_dir, path)
        ########################################################################

        # the REGEXP will delete all sub directories as well
        query = """DELETE FROM `{0}` WHERE `watch_dir` = %s and
                `path` REGEXP %s""".format(self.table_name)

        args = (watch_dir,
                re.escape(path) + "(\/|$)")

        return(self._execute(query, args))
Пример #4
0
def add_file(db, full_path):
    # skip if symlink or not file
    if not os.path.isfile(full_path) or os.path.islink(full_path):
        return

    mtime = os.stat(full_path).st_mtime
    size = os.stat(full_path).st_size

    (watch_dir, path, filename) = split_path(args["watch_dirs"], full_path)

    data = db.get_fields(watch_dir, path, filename, ["mtime", "size"])

    if not data: # file is new
        log.info("generating checksum for {0} ...".format(filename))
        md5 = md5sum(full_path)
        # md5sum returns None if file was moved/deleted
        if md5:
            log.info("inserting {0} into the database.".format(filename))
            db.insert_file(watch_dir, path, filename, md5, mtime, size)
        else:
            log.warn("file '{0}' was moved/deleted during md5sum creation. not being added to database".format(full_path))
    else:
        old_mtime = data[0][0]
        old_size = data[0][1]
        # check if it has changed
        if int(old_mtime) != int(mtime) or int(old_size) != int(size):
            log.debug("old_mtime = {0}".format(old_mtime))
            log.debug("mtime = {0}".format(mtime))
            log.debug("old_size = {0}".format(old_size))
            log.debug("size = {0}".format(size))
            log.info("generating checksum for {0} ...".format(filename))

            md5 = md5sum(full_path)
            if md5:
                log.info("updating {0} in the database.".format(filename))
                rows_changed = db.update_file(watch_dir, path, filename, md5, mtime, size)
                log.debug("rows_changed = {0}".format(rows_changed))
            else:
                log.warn("file '{0}' was moved/deleted during md5sum creation. not being added to database".format(full_path))