Beispiel #1
0
def init():
    global _LocalStorage
    if _LocalStorage is not None:
        lg.warn('local storage already initialized')
        return
    contract_chain_dir = os.path.join(settings.ContractChainDir(), 'current')
    _LocalStorage = Database(contract_chain_dir)
    _LocalStorage.custom_header = coins_index.make_custom_header()
    if _Debug:
        lg.out(_DebugLevel, 'coins_db.init in %s' % contract_chain_dir)
    if db().exists():
        try:
            db().open()
        except:
            temp_dir = os.path.join(settings.ContractChainDir(), 'tmp')
            if os.path.isdir(temp_dir):
                bpio._dir_remove(temp_dir)
            tmpdb = regenerate_indexes(temp_dir)
            rewrite_indexes(db(), tmpdb)
            bpio._dir_remove(temp_dir)
            db().open()
            db().reindex()
    else:
        db().create()
    refresh_indexes(db())
Beispiel #2
0
def on_delete_file(newpacket):
    # TODO: call verify_packet_ownership()
    if not newpacket.Payload:
        ids = [
            newpacket.PacketID,
        ]
    else:
        ids = strng.to_text(newpacket.Payload).split('\n')
    filescount = 0
    dirscount = 0
    lg.warn('going to erase files: %s' % ids)
    customer_id = global_id.UrlToGlobalID(newpacket.OwnerID)
    for pcktID in ids:
        glob_path = global_id.ParseGlobalID(pcktID)
        if not glob_path['customer']:
            glob_path = global_id.ParseGlobalID(customer_id + ':' + pcktID)
        if not glob_path['path']:
            lg.err("got incorrect PacketID")
            p2p_service.SendFail(newpacket, 'incorrect path')
            return False
        if customer_id != glob_path['customer']:
            lg.warn('trying to delete file stored for another cusomer')
            continue
        # TODO: add validation of customerGlobID
        # TODO: process requests from another customer
        filename = make_valid_filename(newpacket.OwnerID, glob_path)
        if not filename:
            lg.warn("got empty filename, bad customer or wrong packetID?")
            p2p_service.SendFail(newpacket,
                                 'not a customer, or file not found')
            return False
        if os.path.isfile(filename):
            try:
                os.remove(filename)
                filescount += 1
            except:
                lg.exc()
        elif os.path.isdir(filename):
            try:
                bpio._dir_remove(filename)
                dirscount += 1
            except:
                lg.exc()
        else:
            lg.warn("path not found %s" % filename)


#         if self.publish_event_supplier_file_modified:
#             events.send('supplier-file-modified', data=dict(
#                 action='delete',
#                 glob_path=glob_path['path'],
#                 owner_id=newpacket.OwnerID,
#             ))
    if _Debug:
        lg.dbg(
            _DebugLevel,
            "from [%s] with %d IDs, %d files and %d folders were removed" %
            (newpacket.OwnerID, len(ids), filescount, dirscount))
    p2p_service.SendAck(newpacket)
    return True
Beispiel #3
0
def recreate_db(chat_history_dir):
    """
    """
    global _LocalStorage
    temp_dir = os.path.join(settings.ChatHistoryDir(), 'tmp')
    if os.path.isdir(temp_dir):
        bpio._dir_remove(temp_dir)
    tmpdb = regenerate_indexes(temp_dir)
    try:
        db().close()
    except:
        pass
    rewrite_indexes(db(), tmpdb)
    bpio._dir_remove(temp_dir)
    try:
        db().open()
        db().reindex()
    except:
        # really bad... we will lose whole data
        _LocalStorage = Database(chat_history_dir)
        _LocalStorage.custom_header = message_index.make_custom_header()
        try:
            _LocalStorage.destroy()
        except:
            pass
        try:
            _LocalStorage.create()
        except Exception as exc:
            lg.warn('failed to create local storage: %r' % exc)
Beispiel #4
0
def UpdateCustomers():
    """
    Test packets after list of customers was changed.
    """
    space, _ = accounting.read_customers_quotas()
    if space is None:
        printlog('UpdateCustomers ERROR space file can not be read')
        return False
    customers_dir = settings.getCustomersFilesDir()
    if not os.path.exists(customers_dir):
        printlog('UpdateCustomers ERROR customers folder not exist')
        return False

    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 = nameurl.FilenameUrl(customer_filename)
        idurl = global_id.GlobalUserToIDURL(customer_filename)
        if idurl is None:
            remove_list[onecustdir] = 'wrong folder name'
            continue
        curspace = space.get(idurl.to_bin(), 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:
                bpio._dir_remove(path)
                printlog('UpdateCustomers %r folder removed (%s)' % (
                    path,
                    remove_list[path],
                ))
            except:
                printlog('UpdateCustomers ERROR removing %r' % path)
            continue
        try:
            if not os.access(path, os.W_OK):
                os.chmod(path, 0o600)
        except:
            pass
        try:
            os.remove(path)
            printlog('UpdateCustomers %r file removed (%s)' % (
                path,
                remove_list[path],
            ))
        except:
            printlog('UpdateCustomers ERROR removing %r' % path)
    printlog('UpdateCustomers %r' %
             time.strftime("%a, %d %b %Y %H:%M:%S +0000"))

    return True
Beispiel #5
0
 def _on_delete_file(self, newpacket):
     import os
     from logs import lg
     from system import bpio
     from lib import strng
     from userid import global_id
     from p2p import p2p_service
     from main import events
     if not newpacket.Payload:
         ids = [newpacket.PacketID, ]
     else:
         ids = strng.to_text(newpacket.Payload).split('\n')
     filescount = 0
     dirscount = 0
     lg.warn('going to erase files: %s' % ids)
     customer_id = global_id.UrlToGlobalID(newpacket.OwnerID)
     for pcktID in ids:
         glob_path = global_id.ParseGlobalID(pcktID)
         if not glob_path['customer']:
             glob_path = global_id.ParseGlobalID(customer_id + ':' + pcktID)
         if not glob_path['path']:
             lg.err("got incorrect PacketID")
             p2p_service.SendFail(newpacket, 'incorrect path')
             return False
         if customer_id != glob_path['customer']:
             lg.warn('trying to delete file stored for another cusomer')
             continue
         # TODO: add validation of customerGlobID
         # TODO: process requests from another customer
         filename = self._do_make_valid_filename(newpacket.OwnerID, glob_path)
         if not filename:
             lg.warn("got empty filename, bad customer or wrong packetID?")
             p2p_service.SendFail(newpacket, 'not a customer, or file not found')
             return False
         if os.path.isfile(filename):
             try:
                 os.remove(filename)
                 filescount += 1
             except:
                 lg.exc()
         elif os.path.isdir(filename):
             try:
                 bpio._dir_remove(filename)
                 dirscount += 1
             except:
                 lg.exc()
         else:
             lg.warn("path not found %s" % filename)
         if self.publish_event_supplier_file_modified:
             events.send('supplier-file-modified', data=dict(
                 action='delete',
                 glob_path=glob_path['path'],
                 owner_id=newpacket.OwnerID,
             ))
     lg.out(self.debug_level, "service_supplier._on_delete_file from [%s] with %d IDs, %d files and %d folders were removed" % (
         newpacket.OwnerID, len(ids), filescount, dirscount))
     p2p_service.SendAck(newpacket)
     return True
Beispiel #6
0
def DeleteFile(request):
    """
    Delete one ore multiple files (that belongs to another user) or folders on my machine.
    """
    if _Debug:
        lg.out(
            _DebugLevel, 'p2p_service.DeleteFile [%s] by %s | %s' %
            (request.PacketID, request.OwnerID, request.CreatorID))
    if not driver.is_on('service_supplier'):
        return SendFail(request, 'supplier service is off')
    if request.Payload == '':
        ids = [request.PacketID]
    else:
        ids = request.Payload.split('\n')
    filescount = 0
    dirscount = 0
    for pcktID in ids:
        glob_path = global_id.ParseGlobalID(pcktID)
        if not glob_path['path']:
            # backward compatible check
            glob_path = global_id.ParseGlobalID(my_id.getGlobalID() + ':' +
                                                request.PacketID)
        if not glob_path['path']:
            lg.warn("got incorrect PacketID")
            SendFail(request, 'incorrect PacketID')
            return
        # TODO: add validation of customerGlobID
        # TODO: process requests from another customer
        filename = makeFilename(request.OwnerID, glob_path['path'])
        if filename == "":
            filename = constructFilename(request.OwnerID, glob_path['path'])
            if not os.path.exists(filename):
                lg.warn(
                    "had unknown customer: %s or pathID is not correct or not exist: %s"
                    % (nameurl.GetName(request.OwnerID), glob_path['path']))
                return SendFail(request, 'not a customer, or file not found')
        if os.path.isfile(filename):
            try:
                os.remove(filename)
                filescount += 1
            except:
                lg.exc()
        elif os.path.isdir(filename):
            try:
                bpio._dir_remove(filename)
                dirscount += 1
            except:
                lg.exc()
        else:
            lg.warn("path not found %s" % filename)
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.DeleteFile from [%s] with %d IDs, %d files and %d folders were removed"
            % (nameurl.GetName(
                request.OwnerID), len(ids), filescount, dirscount))
    SendAck(request)
Beispiel #7
0
def load_suppliers(path=None, customer_idurl=None, all_customers=False):
    """
    Load suppliers list from disk.
    """
    if all_customers:
        list_local_customers = list(os.listdir(settings.SuppliersDir()))
        if _Debug:
            lg.out(_DebugLevel, 'contactsdb.load_suppliers %d known customers' % len(list_local_customers))
        for customer_id in list_local_customers:
            if not global_id.IsValidGlobalUser(customer_id):
                lg.warn('invalid customer record %s found in %s' % (customer_id, settings.SuppliersDir()))
                continue
            path = os.path.join(settings.SuppliersDir(), customer_id, 'supplierids')
            lst = bpio._read_list(path)
            if lst is None:
                lg.warn('did not found suppliers ids at %s' % path)
                continue
            one_customer_idurl = global_id.GlobalUserToIDURL(customer_id)
            if not id_url.is_cached(one_customer_idurl):
                lg.warn('customer identity %r not cached yet' % one_customer_idurl)
                continue
            if not one_customer_idurl.is_latest():
                latest_customer_path = os.path.join(settings.SuppliersDir(), one_customer_idurl.to_id())
                old_customer_path = os.path.join(settings.SuppliersDir(), customer_id)
                if not os.path.exists(latest_customer_path):
                    os.rename(old_customer_path, latest_customer_path)
                    lg.info('detected and processed idurl rotate when loading suppliers for customer : %r -> %r' % (customer_id, one_customer_idurl.to_id()))
                else:
                    bpio._dir_remove(old_customer_path)
                    lg.warn('found old customer dir %r and removed' % old_customer_path)
                    continue
            lst = list(map(lambda i: i if id_url.is_cached(i) else b'', lst))
            set_suppliers(lst, customer_idurl=one_customer_idurl)
            if _Debug:
                lg.out(_DebugLevel, '    loaded %d known suppliers for customer %r' % (len(lst), one_customer_idurl))
        return True
    if not customer_idurl:
        customer_idurl = my_id.getLocalID()
    customer_idurl = id_url.field(customer_idurl)
    if path is None:
        path = os.path.join(settings.SuppliersDir(), global_id.UrlToGlobalID(customer_idurl), 'supplierids')
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    lst = list(map(lambda i: i if id_url.is_cached(i) else b'', lst))
    set_suppliers(lst, customer_idurl=customer_idurl)
    if _Debug:
        lg.out(_DebugLevel, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path))
    return True
Beispiel #8
0
def UpdateCustomers():
    """
    Test packets after list of customers was changed.
    """
    space = bpio._read_dict(settings.CustomersSpaceFile())
    if space is None:
        printlog('UpdateCustomers ERROR space file can not be read')
        return
    customers_dir = settings.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 = nameurl.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:
                bpio._dir_remove(path)
                printlog('UpdateCustomers ' + path + ' folder removed (%s)' % (remove_list[path]))
            except:
                printlog('UpdateCustomers ERROR removing ' + path)
            continue
        try:
            if not os.access(path, os.W_OK):
                os.chmod(path, 0o600)
        except:
            pass
        try:
            os.remove(path)
            printlog('UpdateCustomers ' + path + ' file removed (%s)' % (remove_list[path]))
        except:
            printlog('UpdateCustomers ERROR removing ' + path)
    printlog('UpdateCustomers ' + str(time.strftime("%a, %d %b %Y %H:%M:%S +0000")))
Beispiel #9
0
def DeleteBackup(request):
    """
    Delete one or multiple backups on my machine.
    """
    if not driver.is_on('service_supplier'):
        return SendFail(request, 'supplier service is off')
    if request.Payload == '':
        ids = [request.PacketID]
    else:
        ids = request.Payload.split('\n')
    count = 0
    for bkpID in ids:
        glob_path = global_id.ParseGlobalID(bkpID)
        if not glob_path['path']:
            lg.warn("got incorrect backupID")
            SendFail(request, 'incorrect backupID')
            return
        # TODO: add validation of customerGlobID
        # TODO: process requests from another customer
        filename = makeFilename(request.OwnerID, glob_path['path'])
        if filename == "":
            filename = constructFilename(request.OwnerID, glob_path['path'])
            if not os.path.exists(filename):
                lg.warn("had unknown customer: %s or backupID: %s" (
                    bkpID, request.OwnerID))
                return SendFail(request, 'not a customer, or file not found')
        if os.path.isdir(filename):
            try:
                bpio._dir_remove(filename)
                count += 1
            except:
                lg.exc()
        elif os.path.isfile(filename):
            try:
                os.remove(filename)
                count += 1
            except:
                lg.exc()
        else:
            lg.warn("path not found %s" % filename)
    SendAck(request)
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.DeleteBackup from [%s] with %d IDs, %d were removed" %
            (nameurl.GetName(request.OwnerID), len(ids), count))
Beispiel #10
0
def DeleteFile(request):
    """
    Delete one ore multiple files or folders on my machine.
    """
    if not driver.is_started("service_supplier"):
        return SendFail(request, "supplier service is off")
    if request.Payload == "":
        ids = [request.PacketID]
    else:
        ids = request.Payload.split("\n")
    filescount = 0
    dirscount = 0
    for pathID in ids:
        filename = makeFilename(request.OwnerID, pathID)
        if filename == "":
            filename = constructFilename(request.OwnerID, pathID)
            if not os.path.exists(filename):
                lg.warn(
                    "had unknown customer: %s or pathID is not correct or not exist: %s"
                    % (nameurl.GetName(request.OwnerID), pathID)
                )
                return SendFail(request, "not a customer, or file not found")
        if os.path.isfile(filename):
            try:
                os.remove(filename)
                filescount += 1
            except:
                lg.exc()
        elif os.path.isdir(filename):
            try:
                bpio._dir_remove(filename)
                dirscount += 1
            except:
                lg.exc()
        else:
            lg.warn("path not found %s" % filename)
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.DeleteFile from [%s] with %d IDs, %d files and %d folders were removed"
            % (nameurl.GetName(request.OwnerID), len(ids), filescount, dirscount),
        )
    SendAck(request)
Beispiel #11
0
def DeleteBackup(request):
    """
    Delete one or multiple backups on my machine.
    """
    if not driver.is_started("service_supplier"):
        return SendFail(request, "supplier service is off")
    if request.Payload == "":
        ids = [request.PacketID]
    else:
        ids = request.Payload.split("\n")
    count = 0
    for backupID in ids:
        filename = makeFilename(request.OwnerID, backupID)
        if filename == "":
            filename = constructFilename(request.OwnerID, backupID)
            if not os.path.exists(filename):
                lg.warn("had unknown customer " + request.OwnerID + " or backupID " + backupID)
                return SendFail(request, "not a customer, or file not found")
        if os.path.isdir(filename):
            try:
                bpio._dir_remove(filename)
                count += 1
            except:
                lg.exc()
        elif os.path.isfile(filename):
            try:
                os.remove(filename)
                count += 1
            except:
                lg.exc()
        else:
            lg.warn("path not found %s" % filename)
    SendAck(request)
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.DeleteBackup from [%s] with %d IDs, %d were removed"
            % (nameurl.GetName(request.OwnerID), len(ids), count),
        )
Beispiel #12
0
def init():
    global _LocalStorage
    if _LocalStorage is not None:
        lg.warn('local storage already initialized')
        return
    chat_history_dir = os.path.join(settings.ChatHistoryDir(), 'current')
    _LocalStorage = Database(chat_history_dir)
    _LocalStorage.custom_header = message_index.make_custom_header()
    if _Debug:
        lg.out(_DebugLevel, 'message_db.init in %s' % chat_history_dir)
    if db().exists():
        try:
            db().open()
        except:
            temp_dir = os.path.join(settings.ChatHistoryDir(), 'tmp')
            if os.path.isdir(temp_dir):
                bpio._dir_remove(temp_dir)
            tmpdb = regenerate_indexes(temp_dir)
            rewrite_indexes(db(), tmpdb)
            bpio._dir_remove(temp_dir)
            try:
                db().open()
                db().reindex()
            except:
                # really bad... we will lose whole data
                _LocalStorage = Database(chat_history_dir)
                _LocalStorage.custom_header = message_index.make_custom_header(
                )
                try:
                    _LocalStorage.destroy()
                except DatabaseConflict:
                    pass
                _LocalStorage.create()
    else:
        db().create()
    refresh_indexes(db())
Beispiel #13
0
def recreate_db(chat_history_dir):
    """
    """
    global _LocalStorage
    try:
        _LocalStorage.close()
    except Exception as exc:
        lg.warn('failed closing local storage : %r' % exc)
    _LocalStorage = None
    dbs = Database(chat_history_dir)
    dbs.custom_header = message_index.make_custom_header()
    temp_dir = os.path.join(settings.ChatHistoryDir(), 'tmp')
    if os.path.isdir(temp_dir):
        bpio._dir_remove(temp_dir)
    orig_dir = os.path.join(settings.ChatHistoryDir(), 'orig')
    if os.path.isdir(orig_dir):
        bpio._dir_remove(orig_dir)
    dbt = Database(temp_dir)
    dbt.custom_header = message_index.make_custom_header()
    source_opened = False
    try:
        dbs.open()
        source_opened = True
    except Exception as exc:
        lg.warn('failed open local storage : %r' % exc)
    # patch_flush_fsync(dbs)
    dbt.create()
    dbt.close()
    refresh_indexes(dbt, reindex=False)
    dbt.open()
    # patch_flush_fsync(dbt)
    if source_opened:
        for c in dbs.all('id'):
            del c['_rev']
            dbt.insert(c)
    dbt.close()
    if source_opened:
        dbs.close()
    os.rename(dbs.path, orig_dir)
    os.rename(dbt.path, dbs.path)
    _LocalStorage = Database(chat_history_dir)
    _LocalStorage.custom_header = message_index.make_custom_header()
    db().open()
    # patch_flush_fsync(db())
    if refresh_indexes(db(), rewrite=False, reindex=False):
        bpio._dir_remove(orig_dir)
        lg.info('local DB re-created in %r' % chat_history_dir)
    else:
        lg.err('local DB is broken !!!')
Beispiel #14
0
def on_identity_url_changed(evt):
    old_idurl = id_url.field(evt.data['old_idurl'])
    # update customer idurl in "space" file
    contacts_changed = False
    for customer_idurl in contactsdb.customers():
        if old_idurl == customer_idurl:
            customer_idurl.refresh()
            contacts_changed = True
            lg.info('found customer idurl rotated : %r -> %r' % (
                evt.data['old_idurl'],
                evt.data['new_idurl'],
            ))
    if contacts_changed:
        contactsdb.save_customers()
    # update meta info for that customer
    meta_info_changed = False
    all_meta_info = contactsdb.read_customers_meta_info_all()
    for customer_idurl_bin in list(all_meta_info.keys()):
        if id_url.is_cached(old_idurl) and id_url.is_cached(
                customer_idurl_bin):
            if old_idurl == id_url.field(customer_idurl_bin):
                latest_customer_idurl_bin = id_url.field(
                    customer_idurl_bin).to_bin()
                if latest_customer_idurl_bin != customer_idurl_bin:
                    all_meta_info[
                        latest_customer_idurl_bin] = all_meta_info.pop(
                            customer_idurl_bin)
                    meta_info_changed = True
                    lg.info(
                        'found customer idurl rotated in customers meta info : %r -> %r'
                        % (
                            latest_customer_idurl_bin,
                            customer_idurl_bin,
                        ))
    if meta_info_changed:
        contactsdb.write_customers_meta_info_all(all_meta_info)
    # update customer idurl in "space" file
    space_dict, free_space = accounting.read_customers_quotas()
    space_changed = False
    for customer_idurl_bin in list(space_dict.keys()):
        if id_url.is_cached(old_idurl) and id_url.is_cached(
                customer_idurl_bin):
            if id_url.field(customer_idurl_bin) == old_idurl:
                latest_customer_idurl_bin = id_url.field(
                    customer_idurl_bin).to_bin()
                if latest_customer_idurl_bin != customer_idurl_bin:
                    space_dict[latest_customer_idurl_bin] = space_dict.pop(
                        customer_idurl_bin)
                    space_changed = True
                    lg.info(
                        'found customer idurl rotated in customer quotas dictionary : %r -> %r'
                        % (
                            latest_customer_idurl_bin,
                            customer_idurl_bin,
                        ))
    if space_changed:
        accounting.write_customers_quotas(space_dict, free_space)
    # rename customer folder where I store all his files
    old_customer_dirname = str(global_id.UrlToGlobalID(evt.data['old_idurl']))
    new_customer_dirname = str(global_id.UrlToGlobalID(evt.data['new_idurl']))
    customers_dir = settings.getCustomersFilesDir()
    old_owner_dir = os.path.join(customers_dir, old_customer_dirname)
    new_owner_dir = os.path.join(customers_dir, new_customer_dirname)
    if os.path.isdir(old_owner_dir):
        try:
            bpio.move_dir_recursive(old_owner_dir, new_owner_dir)
            lg.info('copied %r into %r' % (
                old_owner_dir,
                new_owner_dir,
            ))
            if os.path.exists(old_owner_dir):
                bpio._dir_remove(old_owner_dir)
                lg.warn('removed %r' % old_owner_dir)
        except:
            lg.exc()
    # update customer idurl in "spaceused" file
    local_tester.TestSpaceTime()
    return True
Beispiel #15
0
def SpaceTime():
    """
    Test all packets for each customer.

    Check if he use more space than we gave him and if packets is too
    old.
    """
    printlog('SpaceTime ' + str(time.strftime("%a, %d %b %Y %H:%M:%S +0000")))
    space = accounting.read_customers_quotas()
    if space is None:
        printlog(
            'SpaceTime ERROR customers quotas file can not be read or it is empty, skip'
        )
        return
    customers_dir = settings.getCustomersFilesDir()
    if not os.path.exists(customers_dir):
        printlog('SpaceTime ERROR customers folder not exist')
        return
    remove_list = {}
    used_space = accounting.read_customers_usage()
    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 = nameurl.FilenameUrl(customer_filename)
        idurl = global_id.GlobalUserToIDURL(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] = 'not found in space file'
            continue
        try:
            maxspaceV = int(curspace)
        except:
            remove_list[onecustdir] = 'wrong space value'
            continue
        timedict = {}
        sizedict = {}

        def cb(path, subpath, name):
            if not os.path.isfile(path):
                return True
            stats = os.stat(path)
            timedict[path] = stats.st_ctime
            sizedict[path] = stats.st_size

        for key_alias in os.listdir(onecustdir):
            if not misc.ValidKeyAlias(key_alias):
                remove_list[onecustdir] = 'invalid key alias'
                continue
            okekeydir = os.path.join(onecustdir, key_alias)
            bpio.traverse_dir_recursive(cb, okekeydir)
            currentV = 0
            for path in sorted(list(timedict.keys()),
                               key=lambda x: timedict[x],
                               reverse=True):
                filesize = sizedict.get(path, 0)
                currentV += filesize
                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.01)

        used_space[idurl] = str(currentV)
        timedict.clear()
        sizedict.clear()

    for path in remove_list.keys():
        if not os.path.exists(path):
            continue
        if os.path.isdir(path):
            try:
                bpio._dir_remove(path)
                printlog('SpaceTime ' + path + ' dir removed (%s)' %
                         (remove_list[path]))
            except:
                printlog('SpaceTime ERROR removing ' + path)
            continue
        try:
            if not os.access(path, os.W_OK):
                os.chmod(path, 0o600)
        except:
            pass
        try:
            os.remove(path)
            printlog('SpaceTime ' + path + ' file removed (%s)' %
                     (remove_list[path]))
        except:
            printlog('SpaceTime ERROR removing ' + path)
    del remove_list
    accounting.update_customers_usage(used_space)
Beispiel #16
0
def SpaceTime():
    """
    Test all packets for each customer.

    Check if he use more space than we gave him and if packets is too
    old.
    """
    printlog('SpaceTime ' + str(time.strftime("%a, %d %b %Y %H:%M:%S +0000")))
    space = bpio._read_dict(settings.CustomersSpaceFile())
    if space is None:
        printlog('SpaceTime ERROR can not read file ' + settings.CustomersSpaceFile())
        return
    customers_dir = settings.getCustomersFilesDir()
    if not os.path.exists(customers_dir):
        printlog('SpaceTime ERROR customers folder not exist')
        return
    remove_list = {}
    used_space = {}
    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 = nameurl.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] = 'not found in space file'
            continue
        try:
            maxspaceV = int(curspace)
        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 [settings.BackupIndexFileName(),]:
#                 return False
            stats = os.stat(path)
            timedict[path] = stats.st_ctime
            sizedict[path] = stats.st_size
        bpio.traverse_dir_recursive(cb, onecustdir)
        currentV = 0
        for path in sorted(timedict.keys(), key=lambda x: timedict[x], reverse=True):
            filesize = sizedict.get(path, 0)
            currentV += filesize
            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.01)
        used_space[idurl] = str(currentV)
        timedict.clear()
        sizedict.clear()
    for path in remove_list.keys():
        if not os.path.exists(path):
            continue
        if os.path.isdir(path):
            try:
                bpio._dir_remove(path)
                printlog('SpaceTime ' + path + ' dir removed (%s)' % (remove_list[path]))
            except:
                printlog('SpaceTime ERROR removing ' + path)
            continue
        try:
            if not os.access(path, os.W_OK):
                os.chmod(path, 0o600)
        except:
            pass
        try:
            os.remove(path)
            printlog('SpaceTime ' + path + ' file removed (%s)' % (remove_list[path]))
        except:
            printlog('SpaceTime ERROR removing ' + path)
    del remove_list
    bpio._write_dict(settings.CustomersUsedSpaceFile(), used_space)