def load(unix_file_name, pab): fh = None try: fh = open(unix_file_name, "rb") fdata = bytearray(fh.read()) # Check that request matches DISPLAY or INTERNAL of file ti_files.validateDataType(fdata, dataType(pab)) # Check that target file is valid if not ti_files.isValid(fdata): raise Exception("invalid TIFILE") # Check that we are a VARIABLE record file logger.debug("flags %d", ti_files.flags(fdata)) if not ti_files.isVariable(fdata): raise Exception("file is FIXED, must be VARIABLE") if fileType(pab) == RELATIVE: raise Exception( "variable length records are restricted to SEQUENTIAL access" ) return VariableRecordFile(fdata, pab) except Exception as e: logger.exception("not a valid Variable Record TIFILE %s", unix_file_name) return None finally: if fh != None: fh.close()
def basicContents(filename): logger.debug("fetching BASIC PROGRAM as ascii in %s", filename) # We are assuming the test for FIAD isTiFile has already passed. prg_tmp_file = '/tmp/' + str(uuid.uuid4()) + '.tmp' bas_tmp_file = '/tmp/' + str(uuid.uuid4()) + '.tmp' try: # strip the FIAD header off to get the raw file xbas99 needs. with open(filename, "rb") as tifile: with open(prg_tmp_file, "wb") as program: bytes = bytearray(tifile.read()) if ti_files.isProgram(bytes): program.write(bytes[128:]) elif ti_files.isVariable(bytes) and ti_files.isInternal( bytes) and ti_files.recordLength(bytes) == 254: i = 128 limit = len(bytes) while (i < limit): rlen = bytes[i] next = i + rlen + 1 program.write(bytes[i:next]) i = next if bytes[i] == 0xff: # skip to next 256 byte boundary i = (256 * (((i - 128) / 256) + 1)) + 128 else: return False call([ '/home/tipi/xdt99/xbas99.py', '-d', prg_tmp_file, '-o', bas_tmp_file ]) if ti_files.isTiBasicAscii(bas_tmp_file): with open(bas_tmp_file, 'rb') as content_file: return content_file.read().decode("latin_1") finally: if os.path.exists(prg_tmp_file): os.unlink(prg_tmp_file) if os.path.exists(bas_tmp_file): os.unlink(bas_tmp_file) return False
def load(unix_file_name, pab): fh = None try: fh = open(unix_file_name, "rb") fdata = bytearray(fh.read()) # Check that request matches DISPLAY or INTERNAL of file ti_files.validateDataType(fdata, dataType(pab)) # Check that target file is valid if not ti_files.isValid(fdata): raise Exception("invaid TIFILE") # Check that we are a FIXED record file if ti_files.isVariable(fdata): raise Exception("file is variable") return FixedRecordFile(fdata, pab) except Exception as e: logger.error("File does not match PAB type: %s", unix_file_name) return None finally: if fh != None: fh.close()
def handleStatus(self, pab, devname): logger.info("Opcode 9 Status - %s", devname) logPab(pab) statbyte = 0 unix_name = tinames.devnameToLocal(devname) if unix_name is None: self.sendErrorCode(EDVNAME) return if not os.path.exists(unix_name): statbyte |= STNOFILE else: if not os.path.isdir(unix_name): open_file = None try: open_file = self.openFiles[unix_name] except: pass if open_file is not None: statbyte = open_file.getStatusByte() else: if ti_files.isTiFile(unix_name): fh = open(unix_name, "rb") header = bytearray(fh.read())[:128] if ti_files.isVariable(header): statbyte |= STVARIABLE if ti_files.isProgram(header): statbyte |= STPROGRAM if ti_files.isInternal(header): statbyte |= STINTERNAL else: statbyte = NativeFile.status(unix_name) self.tipi_io.send([SUCCESS]) self.tipi_io.send([statbyte])