Example #1
0
    def handle_delete_event(self, event):
        #make sure that all queued uploads are finished before we send a delete event
        #this ensures that all events stay in-order
        if len(self.to_upload) > 0:
            self.upload()

        #if the file doesn't exist and we're a delete event, just drop it
        res = self.database.execute(
            """SELECT * FROM events WHERE localpath=? 
                                    LIMIT 1""", (event.path, ))
        exists = next(res, None)
        if exists is None:
            return

        res = self.database.execute(
            """SELECT * FROM events WHERE localpath=?
                                    AND rev != 0 ORDER BY rev DESC LIMIT 1""",
            (event.path, ))
        latest = next(res, None)
        if latest is not None:
            e = Event(0)
            e.fromseq(latest)
            if e.type & EventType.DELETE:
                #returning because it was already deleted
                return

        #add event to the database
        self.database.execute("INSERT INTO events VALUES (0,?,?,?,?,?,?,?)",
                              event.totuple()[1:])

        self.sender_queue.put(event)
Example #2
0
    def handle_delete_event(self, event):
        #make sure that all queued uploads are finished before we send a delete event
        #this ensures that all events stay in-order
        if len(self.to_upload) > 0:
            self.upload()

        #if the file doesn't exist and we're a delete event, just drop it
        res = self.database.execute("""SELECT * FROM events WHERE localpath=? 
                                    LIMIT 1""", (event.path,))
        exists = next(res, None)
        if exists is None:
            return

        res = self.database.execute("""SELECT * FROM events WHERE localpath=?
                                    AND rev != 0 ORDER BY rev DESC LIMIT 1""", (event.path,))
        latest = next(res, None)
        if latest is not None:
            e = Event(0)
            e.fromseq(latest)
            if e.type & EventType.DELETE:
                #returning because it was already deleted
                return

        #add event to the database
        self.database.execute("INSERT INTO events VALUES (0,?,?,?,?,?,?,?)",
                              event.totuple()[1:])

        self.sender_queue.put(event)
Example #3
0
    def handle_update_event(self, event):
        filepath = os.path.join(Config().get("core", "syncdir"), event.path)

        #first, copy the file over to a temporary directory, get its hash,
        #upload it, and then move it to the filename with that hash value
        handle, tmppath = mkstemp(dir=Config().get("core", "cachedir"))
        os.close(handle) #we don't really want it open, we just want a good name
        try:
            copy2(filepath, tmppath)
        except IOError:
            logging.warning("Dropping update event because file was deleted before we could upload it: %s" % (str(event)))
            return

        #get the mode of the file
        stats = os.stat(filepath)
        event.permissions = str(stat.S_IMODE(stats.st_mode))

        #hash the temporary file
        event.hash = hash(tmppath)[0]
        logging.debug("HASHED  "+str(event))

        #make sure the most recent version of this file doesn't match this one
        #otherwise it's pointless to re-upload it
        res = self.database.execute("""SELECT * FROM events WHERE localpath=?
                                    AND rev != 0 ORDER BY rev DESC LIMIT 1""", (event.path,))
        latest = next(res, None)
        if latest is not None:
            e = Event(0)
            e.fromseq(latest)
            if e.hash == event.hash:
                #returning because hashes are equal
                #but first, remove the temporary file in the cache
                os.remove(tmppath)
                return

        res = self.database.execute("SELECT * FROM events WHERE hash=? AND rev!=0",
                                   (event.hash,))
        sameHash = next(res, None)
        #if this file isn't already uploaded, add it to the list to upload, and
        #upload them in a batch when we have a chance
        if sameHash is None:
            self.to_upload.append((event, tmppath))
            return

        e = Event(0)
        e.fromseq(sameHash)
        event.storagekey = e.storagekey

        #add event to the database
        self.database.execute("INSERT INTO events VALUES (0,?,?,?,?,?,?,?)",
                              event.totuple()[1:])

        #move tmp file to hash-named file in cache directory
        cachepath = os.path.join(Config().get("core", "cachedir"), event.hash)
        move(tmppath, cachepath)

        self.sender_queue.put(event)
Example #4
0
    def handle_update_event(self, event):
        filepath = os.path.join(Config().get("core", "syncdir"), event.path)

        #first, copy the file over to a temporary directory, get its hash,
        #upload it, and then move it to the filename with that hash value
        handle, tmppath = mkstemp(dir=Config().get("core", "cachedir"))
        os.close(
            handle)  #we don't really want it open, we just want a good name
        try:
            copy2(filepath, tmppath)
        except IOError:
            logging.warning(
                "Dropping update event because file was deleted before we could upload it: %s"
                % (str(event)))
            return

        #get the mode of the file
        stats = os.stat(filepath)
        event.permissions = str(stat.S_IMODE(stats.st_mode))

        #hash the temporary file
        event.hash = hash(tmppath)[0]
        logging.debug("HASHED  " + str(event))

        #make sure the most recent version of this file doesn't match this one
        #otherwise it's pointless to re-upload it
        res = self.database.execute(
            """SELECT * FROM events WHERE localpath=?
                                    AND rev != 0 ORDER BY rev DESC LIMIT 1""",
            (event.path, ))
        latest = next(res, None)
        if latest is not None:
            e = Event(0)
            e.fromseq(latest)
            if e.hash == event.hash:
                #returning because hashes are equal
                #but first, remove the temporary file in the cache
                os.remove(tmppath)
                return

        res = self.database.execute(
            "SELECT * FROM events WHERE hash=? AND rev!=0", (event.hash, ))
        sameHash = next(res, None)
        #if this file isn't already uploaded, add it to the list to upload, and
        #upload them in a batch when we have a chance
        if sameHash is None:
            self.to_upload.append((event, tmppath))
            return

        e = Event(0)
        e.fromseq(sameHash)
        event.storagekey = e.storagekey

        #add event to the database
        self.database.execute("INSERT INTO events VALUES (0,?,?,?,?,?,?,?)",
                              event.totuple()[1:])

        #move tmp file to hash-named file in cache directory
        cachepath = os.path.join(Config().get("core", "cachedir"), event.hash)
        move(tmppath, cachepath)

        self.sender_queue.put(event)