def ListCustomerFiles1(customerNumber): idurl = contacts.getCustomerID(customerNumber) filename = nameurl.UrlFilename(idurl) customerDir = os.path.join(settings.getCustomersFilesDir(), filename) if os.path.exists(customerDir) and os.path.isdir(customerDir): backupFilesList = os.listdir(customerDir) if len(backupFilesList) > 0: return ListSummary(backupFilesList) return "No files stored for this customer"
def constructFilename(customerID, packetID): customerDirName = nameurl.UrlFilename(customerID) customersDir = settings.getCustomersFilesDir() if not os.path.exists(customersDir): dhnio._dir_make(customersDir) ownerDir = os.path.join(customersDir, customerDirName) if not os.path.exists(ownerDir): dhnio._dir_make(ownerDir) filename = os.path.join(ownerDir, packetID) return filename
def ListCustomerFiles(customer_idurl): filename = nameurl.UrlFilename(customer_idurl) customer_dir = os.path.join(settings.getCustomersFilesDir(), filename) result = cStringIO.StringIO() def cb(realpath, subpath, name): if os.path.isdir(realpath): result.write('D%s\n' % subpath) else: result.write('F%s\n' % subpath) return True dhnio.traverse_dir_recursive(cb, customer_dir) src = result.getvalue() result.close() return src
def Validate(): printlog("Validate " + str(time.strftime("%a, %d %b %Y %H:%M:%S +0000"))) contacts_init() commands_init() customers_dir = getCustomersFilesDir() if not os.path.exists(customers_dir): return for customer_filename in os.listdir(customers_dir): onecustdir = os.path.join(customers_dir, customer_filename) if not os.path.isdir(onecustdir): continue def cb(path, subpath, name): if not os.access(path, os.R_OK | os.W_OK): return False if not os.path.isfile(path): return True if name in [BackupIndexFileName()]: return False packetsrc = dhnio.ReadBinaryFile(path) if not packetsrc: try: os.remove(path) # if is is no good it is of no use to anyone printlog("Validate " + path + " removed (empty file)") except: printlog("Validate ERROR removing " + path) return False packet = Unserialize(packetsrc) if packet is None: try: os.remove(path) # if is is no good it is of no use to anyone printlog("Validate " + path + " removed (unserialize error)") except: printlog("Validate ERROR removing " + path) return False result = packet.Valid() packetsrc = "" del packet if not result: try: os.remove(path) # if is is no good it is of no use to anyone printlog("Validate " + path + " removed (invalid packet)") except: printlog("Validate ERROR removing " + path) return False time.sleep(0.1) return False dhnio.traverse_dir_recursive(cb, onecustdir)
def ListFiles(request): MyID = misc.getLocalID() RemoteID = request.OwnerID PacketID = request.PacketID Payload = request.Payload dhnio.Dprint(8, "p2p_service.ListFiles from [%s], format is %s" % (nameurl.GetName(request.OwnerID), Payload)) custdir = settings.getCustomersFilesDir() ownerdir = os.path.join(custdir, nameurl.UrlFilename(request.OwnerID)) if not os.path.isdir(ownerdir): dhnio.Dprint(8, "p2p_service.ListFiles did not find customer dir " + ownerdir) src = PackListFiles('', Payload) result = dhnpacket.dhnpacket(commands.Files(), MyID, MyID, PacketID, src, RemoteID) transport_control.outboxNoAck(result) return plaintext = TreeSummary(ownerdir) src = PackListFiles(plaintext, Payload) outpacket = dhnpacket.dhnpacket(commands.Files(), MyID, MyID, PacketID, src, RemoteID) transport_control.outboxNoAck(outpacket)
def UpdateCustomers(): printlog("UpdateCustomers " + str(time.strftime("%a, %d %b %Y %H:%M:%S +0000"))) space = dhnio._read_dict(CustomersSpaceFile()) if space is None: printlog("UpdateCustomers ERROR space file can not read") return customers_dir = getCustomersFilesDir() if not os.path.exists(customers_dir): printlog("UpdateCustomers ERROR customers folder not exist") return remove_list = {} for customer_filename in os.listdir(customers_dir): onecustdir = os.path.join(customers_dir, customer_filename) if not os.path.isdir(onecustdir): remove_list[onecustdir] = "is not a folder" continue idurl = FilenameUrl(customer_filename) if idurl is None: remove_list[onecustdir] = "wrong folder name" continue curspace = space.get(idurl, None) if curspace is None: remove_list[onecustdir] = "is not a customer" continue for path in remove_list.keys(): if not os.path.exists(path): continue if os.path.isdir(path): try: dhnio._dir_remove(path) printlog("UpdateCustomers " + path + " folder removed (%s)" % (remove_list[path])) except: printlog("UpdateCustomers ERROR removing " + path) continue if not os.access(path, os.W_OK): os.chmod(path, 0600) try: os.remove(path) printlog("UpdateCustomers " + path + " file removed (%s)" % (remove_list[path])) except: printlog("UpdateCustomers ERROR removing " + path)
def SpaceTime(): printlog("SpaceTime " + str(time.strftime("%a, %d %b %Y %H:%M:%S +0000"))) space = dhnio._read_dict(CustomersSpaceFile()) if space is None: printlog("SpaceTime ERROR can not read file " + CustomersSpaceFile()) return customers_dir = getCustomersFilesDir() if not os.path.exists(customers_dir): printlog("SpaceTime ERROR customers folder not exist") return remove_list = {} for customer_filename in os.listdir(customers_dir): onecustdir = os.path.join(customers_dir, customer_filename) if not os.path.isdir(onecustdir): remove_list[onecustdir] = "is not a folder" continue idurl = FilenameUrl(customer_filename) if idurl is None: remove_list[onecustdir] = "wrong folder name" continue curspace = space.get(idurl, None) if curspace is None: continue try: maxspaceV = int(float(curspace) * 1024 * 1024) # in bytes except: remove_list[onecustdir] = "wrong space value" continue timedict = {} sizedict = {} def cb(path, subpath, name): if not os.access(path, os.R_OK | os.W_OK): return False if not os.path.isfile(path): return True if name in [BackupIndexFileName()]: return False stats = os.stat(path) timedict[path] = stats.st_ctime sizedict[path] = stats.st_size dhnio.traverse_dir_recursive(cb, onecustdir) currentV = 0 for path in sorted(timedict.keys(), key=lambda x: timedict[x], reverse=True): currentV += sizedict.get(path, 0) if currentV < maxspaceV: continue try: os.remove(path) printlog("SpaceTime " + path + " file removed (cur:%s, max: %s)" % (str(currentV), str(maxspaceV))) except: printlog("SpaceTime ERROR removing " + path) time.sleep(0.1) timedict.clear() sizedict.clear() for path in remove_list.keys(): if not os.path.exists(path): continue if os.path.isdir(path): try: dhnio._dir_remove(path) printlog("SpaceTime " + path + " dir removed (%s)" % (remove_list[path])) except: printlog("SpaceTime ERROR removing " + path) continue if not os.access(path, os.W_OK): os.chmod(path, 0600) try: os.remove(path) printlog("SpaceTime " + path + " file removed (%s)" % (remove_list[path])) except: printlog("SpaceTime ERROR removing " + path) del remove_list