def remove_directory(self, dir_name): '''Remove a directory in the current path, if exists.''' ib = self.get_level_one_current_dir()[1] f = lambda x: x.is_directory() del_ref = self.search_name_in_ib(ib, dir_name, False, f) if del_ref is None: ib = self.get_level_two_current_dir()[1] if ib is None: raise OSException("Error: Directory not found.") for b in ib.get_blocks(): ib = self.get_llfm().get_disk_at(b) del_ref = self.search_name_in_ib(ib, dir_name, True, f) if del_ref is not None: break if del_ref is None: raise OSException("Error: Directory not found.") elif self.get_llfm().check_empty_dir(del_ref): ib.remove_index(del_ref) self.get_llfm().delete_indirections(del_ref) self.get_llfm().remove_block(del_ref) self.get_llfm().dump_new_data() Logger.get_instance().log_del(dir_name, 'directory') else: raise OSException( "Error: Attempt to delete a non-empty directory.")
def change_directory(self, dir_name): ''' Special cases (. , .. and /) are treated apart, because their position in the index block are always the same (0 and 1 respectively), or is the root directory (/). ''' if dir_name == '.': pass #nothing to do, it's the same directory elif dir_name == '..': new_ref = self.get_level_one_current_dir()[1].get_blocks()[1] self.set_cwd((self.remove_last_dir_from_path(), new_ref)) elif dir_name == '/': self.set_cwd(('/', 0)) #shortcut to go to the root else: f = lambda x: x.is_directory() lv_one = self.get_level_one_current_dir()[1] new_ref = self.search_name_in_ib(lv_one, dir_name, False, f) if new_ref is None: (snd_ib_ref, snd_ib) = self.get_level_two_current_dir() if snd_ib_ref is None: raise OSException("Error: Directory not found.") for b in snd_ib.get_blocks(): block = self.get_llfm().get_disk_at(b) new_ref = self.search_name_in_ib(block, dir_name, True, f) if new_ref is not None: break if new_ref is None: raise OSException("Error: Directory not found.") else: self.set_cwd(((self.get_cwd()[0] + '/' + dir_name), new_ref))
def do_sub_in_mem_cell(self, pcb, mem_cell, cpu_regs, mem): ''' Operation for data substract, impossible to implement by String. @raise OSException: can't store in a value !! ''' raise OSException("Error: can't sub a String !!")
def ren_file(self, old_name, new_name): '''Rename a file in the current directory. Fail if it's in memory.''' the_path = self.get_current_path() if self.search_file_in_memory(the_path + '/' + old_name) is not None: raise OSException("Error: Can't delete. The file is in memory.") self.rename(old_name, new_name, lambda x: x.is_file()) Logger.get_instance().log_rename(old_name, new_name, 'file')
def delete(self, file_name): ''' Delete a file from the file system and delete all its references, index blocks, and data blocks. ''' the_path = self.get_current_path() if self.search_file_in_memory(the_path + '/' + file_name) is not None: raise OSException("Error: Can't delete. The file is in memory.") (file_i, file_b) = self.search_file_name(file_name) fst_lev = file_b.get_level_one_blocks() self.get_llfm().remove_db_from_ib(self.get_llfm().get_disk_at(fst_lev)) self.get_llfm().remove_block(fst_lev) snd_i = file_b.get_level_two_blocks() if snd_i is not None: snd_b = self.get_llfm().get_disk_at(snd_i) for b in snd_b.get_blocks(): block = self.get_llfm().get_disk_at(b) self.get_llfm().remove_db_from_ib(block) self.get_llfm().remove_db_from_ib(snd_b) self.get_llfm().remove_block(snd_i) self.get_llfm().remove_block(file_i) try: self.get_level_one_current_dir()[1].remove_index(file_i) except: lt = self.get_level_two_current_dir()[1] if lt is not None: for b in lt.get_blocks(): try: self.get_llfm().get_disk_at(b).remove_index(file_i) except: continue self.get_llfm().dump_new_data() Logger.get_instance().log_del(file_name, 'file')
def write(self, file_name, data): '''Write to the end of a file given, a data also given.''' the_path = self.get_current_path() the_file = self.search_file_in_memory(the_path + '/' + file_name) if the_file is None: raise OSException("Error: The file isn't open or doesn't exists.") the_file.append_data(data)
def save(self, file_name): ''' Synchronize the contents of a file in memory and persist its data in the disk. ''' the_path = self.get_current_path() the_file = self.search_file_in_memory(the_path + '/' + file_name) if the_file is None: raise OSException("Error: File isn't in memory or doesn't exists.") data = the_file.get_data() file_inode = self.get_llfm().get_disk_at(the_file.get_disk_pos()) file_inode.set_size(the_file.get_size()) fst_ib = self.get_llfm().get_disk_at(file_inode.get_level_one_blocks()) data = self.set_data_to_blocks(fst_ib, data) if file_inode.get_level_two_blocks() is None and not data == '': ltb = self.get_llfm().create_ib_for_two_level() file_inode.set_level_two_blocks(ltb) snd_ib = self.get_llfm().get_disk_at(file_inode.get_level_two_blocks()) if not data == '': for iblock in snd_ib.get_blocks(): curr_ib = self.get_llfm().get_disk_at(iblock) data = self.set_data_to_blocks(curr_ib, data) if data == '': break self.get_llfm().dump_new_data() Logger.get_instance().log_datasaved(file_name)
def open(self, name, rw=True): ''' Loads a file into the memory. If it's already loaded, updates its ref_count. ''' complete_name = self.get_cwd()[0] + '/' + name if self.is_opened(complete_name): self.search_file_in_memory(complete_name).inc_ref_count() else: (file_i, file_b) = self.search_file_name(name) res = '' fst_ib = self.get_llfm().get_disk_at(file_b.get_level_one_blocks()) for index in fst_ib.get_blocks(): curr_db = self.get_llfm().get_disk_at(index) if curr_db is not None: res += curr_db.get_data() #appending each block (1st ind) snd_ib = self.get_llfm().get_disk_at(file_b.get_level_two_blocks()) if snd_ib is not None: #there are more data for ib in snd_ib.get_blocks(): iblock = self.get_llfm().get_disk_at(ib) for dbi in iblock.get_blocks(): curr_db = self.get_llfm().get_disk_at(dbi) if curr_db is not None: res += curr_db.get_data() if not self.check_available_space(): raise OSException('Error: Full memory.') else: date = file_b.get_date() size = file_b.get_size() open_file = File(complete_name, rw, date, res, file_i, size) pos = self.store_in_memory(open_file) Logger.get_instance().log_openfile(name, pos) return pos
def do_mov(self, pcb, another_parameter, cpu_regs, mem): ''' Operation for data moving, impossible to implement in String. @raise OSException: can't store in a value !! ''' raise OSException("Error: cant' mov (store) a value in a value !!")
def do_sub(self, pcb, another_parameter, cpu_regs, mem): ''' Operation for data substract, impossible to implement by String as a first argument. @raise OSException: can't store in a value !! ''' raise OSException("Error: can't sub and then store in a value !!")
def do_add(self, pcb, another_parameter, cpu_regs, mem): ''' Operation for data adding, impossible to implement by Number as a first argument. @raise OSException: can't store in a value !! ''' raise OSException("Error: can't add and then store in a value !!")
def __get_and_check_real_address(self, pcb, address): ''' Checks if an address is valid to the pcb given. If it's valid, returns it. ''' real_address = pcb.get_base_reg() + address if real_address > pcb.get_limit_reg(): raise OSException("Error: Process out of addressing space") else: return real_address
def search_file_name(self, name): '''Search for a file name and return its position and its block.''' f = lambda x: x.is_file() lev_one_dir = self.get_level_one_current_dir()[1] res_i = self.search_name_in_ib(lev_one_dir, name, True, f) if res_i is not None: return (res_i, self.get_llfm().get_disk_at(res_i)) else: (snd_ib_ref, snd_ib) = self.get_level_two_current_dir() if snd_ib_ref is None: raise OSException("Error: File not found.") for b in snd_ib.get_blocks(): curr_ib = self.get_llfm().get_disk_at(b) res_i = self.search_name_in_ib(curr_ib, name, True, f) if res_i is not None: break if res_i is not None: return (res_i, self.get_llfm().get_disk_at(res_i)) else: raise OSException("Error: File not found.")
def rename(self, old_name, new_name, f): ''' Generic method. Rename directories or files. @param f: a function that determines if the element is a file or directory. ''' lev_one = self.get_level_one_current_dir()[1] new_ref = self.search_name_in_ib(lev_one, old_name, False, f) if new_ref is None: (snd_ib_ref, snd_ib) = self.get_level_two_current_dir() if snd_ib_ref is None: raise OSException("Error: Directory or file not found.") for b in snd_ib.get_blocks(): block = self.get_llfm().get_disk_at(b) new_ref = self.search_name_in_ib(block, old_name, True, f) if new_ref is not None: break if new_ref is None: raise OSException("Error: Directory or file not found.") self.get_llfm().get_disk_at(new_ref).set_name(new_name) self.get_llfm().dump_new_data()
def __init__(self, regex_filter=None, interval=None, kind='Security'): if os.name != 'nt': raise OSException('This parser works only on Windows machine!') self.regex_filter = regex_filter self.interval = interval # ova dva bih mogao citati iz configa isto # ili mozda ipak ni ne treba self.server = 'localhost' # name of the target computer to get event logs self.logtype = kind self.hand = win32evtlog.OpenEventLog(self.server, self.logtype) self.flags = win32evtlog.EVENTLOG_BACKWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ self.total = win32evtlog.GetNumberOfEventLogRecords(self.hand) Thread.__init__(self) self.last_log_timestamp = datetime.now()
def load(self, file_name): ''' Executes a file storaged in the file system, in the folder "programs". First load the loader :-) , to leave in memory the program to run. ''' if self.__loader_is_running(): raise OSException('Error: The loader is running.') loader = self.__make_loader(file_name) self.__set_time_parameters(loader) banker = self.get_cpu().get_banker() banker.loader_come(banker.calculate_new_max(loader.get_pcb())) self.get_process_store().put_in_all_process(loader) self.get_process_store().put_in_ready_queue(loader.get_pcb()) while Memory.get_instance().get_value_at(1) is None: time.sleep(1) self.__load_program(Memory.get_instance().get_value_at(1)) Memory.get_instance().remove(1)
def add_program(self, file_name, new_name): ''' Take a file from the real disk and put in the file system, in the folder "programs", in the root directory. @param new_name: the name that the file will have in the file system. @precondition: This method only must be executed in console mode. ''' try: the_file = open(file_name) data = the_file.read() the_file.close() except: raise OSException('Error: Cannot open file name.') self.change_directory('/') self.change_directory('programs') self.create_file(new_name) self.open(new_name) self.write(new_name, data) self.save(new_name) self.close(new_name)
def do_intinput(self, pcb, message, mem, using_int): '''@raise OSException: because only can store in memory.''' raise OSException("Error: Input - Only can store in memory.")
def set_priority(self, prior): '''Setter of _priority.''' if prior not in range(1, 11): raise OSException('Error: Wrong process priority') else: self._priority = prior
def set_state(self, state): '''Setter of _state.''' if state not in ['New', 'Ready', 'Running', 'Waiting', 'Finished']: raise OSException('Error: Wrong process state') else: self._state = state
def set_pc(self, pc): '''Setter of _pc.''' if pc > len(self.get_process().get_instructions()): raise OSException('Error: Program Counter out of bounds') else: self._pc = pc
def get_write_data(self, pcb): raise OSException("Error: I/O operation with CPU registers.")
def do_show(self, pcb, memory): raise OSException("Error: I/O operation with CPU registers.")