def request_file_deletion(self, file_name):
     data = Data(self.db_path)
     modifications=data.get_file_modifications(file_name)
     for modification in modifications:
         file_id=modification[1]
         message="DELETE "+str(file_id)+CRLF+CRLF
         self.mc.sendto(message, (self.mc_address, self.mc_port))
 def request_file_deletion2(self, file_name):
     data = Data(self.db_path)
     modifications=data.get_file_modifications(file_name)
     for modification in modifications:
         file_id=modification[1]
         message="DELETE2 "+str(file_id)+CRLF+CRLF
         self.mc.sendto(message, (self.mc_address, self.mc_port))
         chunks=modification[3]
         hosts_set=set([])
         for chunk in range(chunks):
             hosts=data.get_chunk_hosts(file_id, chunk)
             hosts=filter(lambda a:a!="localhost",hosts)
             for host in hosts:
                 host=host[0]
                 hosts_set.add(host)
         self.pending_deletes[str(file_id)]=[MAX_DELETE_WAITING_TIME,hosts_set]
 def handle_shell_request(self, message,addr):
     args=message.split(" ")
     operation=args[0]
     data = Data(self.db_path)
     if(operation=="backup"):
         file_path=args[1]
         replication_degree=args[2]
         if(self.send_chunks(file_path,int(replication_degree))):
             self.shell.sendto("ok\n", addr)
         else:
             self.shell.sendto("fail\n", addr)
     elif(operation=="restore"):
         file_path=args[1]
         modifications=data.get_file_modifications(file_path)
         message="found "+ str(len(modifications))
         for modification in modifications:
             modification_date=modification[2]
             modification_date=modification_date[:10]+"T"+modification_date[11:19]
             message+= " " + modification_date
         self.shell.sendto(message,addr)
     
     elif(operation=="restoremodification"):
         file_name = args[1]
         option = int(args[2])
         modification = data.get_file_modifications(file_name)[option-1]
         sha256 = modification[1]
         chunks = int(modification[3])
         self.restore_file_modification(sha256, chunks)
         self.get_file(sha256, file_name,chunks)
         
     elif(operation=="delete"):
         file_name=args[1]
         self.request_file_deletion(file_name)
         
     elif(operation=="delete2"):
         file_name=args[1]
         self.request_file_deletion2(file_name)
         
     elif(operation=="REMOVED"):
         if(self.can_send_removed):
             file_id=message.split(" ")[2]
             chunk_number=message.split(" ")[3]
             self.reject_putchunks[file_id+chunk_number]=datetime.now() + timedelta(0,60)
             message+=CRLF+CRLF
             self.mc.sendto(message,(self.mc_address,self.mc_port))
             
     elif(operation=="deletechunk"):
         data= Data(self.db_path)
         file_id=message.split(" ")[1]
         chunk_number=message.split(" ")[2]
         filepath=self.backup_dir+file_id+"_"+chunk_number+".chunk"
         if (os.path.exists(filepath)):
             os.remove(filepath)
             data.delete_chunk_removed(chunk_number, file_id)
             
     elif(operation=="deleted"):
         file_id=message.split(" ")[1]
         if (file_id in self.pending_deletes):
             host=addr[0]
             try:
                 self.pending_deletes[file_id][1].remove(host)
                 if(len(self.pending_deletes[file_id][1])==0):
                     del self.pending_deletes[file_id]
             except:
                 pass