def fs_read(self, path, size, offset): ''' Reads a directory. Returns list of files. Example: axon.fs_read('/mydir', 100, 0) ''' tufo = self.core.getTufoByProp('axon:path', path) if not tufo: raise s_common.NoSuchEntity() bval = tufo[1].get('axon:path:blob') blob = None if bval: blob = self.core.getTufoByProp('axon:blob', bval) if not blob: return b'' boff = blob[1].get('axon:blob:off') blob[1]['axon:blob:off'] = boff + offset # the offset of the blob in the axon + the offset within the file blob[1]['axon:blob:size'] = size # number of bytes that the OS asks for return b''.join(self.iterblob(blob))
def fs_utimens(self, path, times=None): ''' Change file timestamps (st_atime, st_mtime). Args: path (str): Path to file to change. times (tuple): Tuple containing two integers - st_atime and st_mtime. Examples: Set the timestamps to epoch 0:: axon.fs_utimens('/myfile', (0, 0)) Returns: None ''' if not (type(times) is tuple and len(times) == 2): return st_atime = int(times[0]) st_mtime = int(times[1]) tufo = self.core.getTufoByProp('axon:path', path) if tufo: self.core.setTufoProps(tufo, st_atime=st_atime, st_mtime=st_mtime) return raise s_common.NoSuchEntity(mesg='Path does not exist.', path=path)
def fs_truncate(self, path): ''' Truncates a file by setting its st_size to zero. Args: path (str): Path to truncate. Examples: Truncate a file:: axon.fs_truncate('/myfile') Returns: None Raises: NoSuchEntity: If the path does not exist. ''' tufo = self.core.getTufoByProp('axon:path', path) if tufo: self.core.delTufoProp(tufo, 'blob') self.core.setTufoProps(tufo, st_size=0) return raise s_common.NoSuchEntity(mesg='File does not exist to truncate.', path=path)
def fs_rmdir(self, path): ''' Removes a directory Args: path (str): Path to remove. Examples: Remove a directory:: axon.fs_rmdir('/mydir') Returns: None Raises: NoSuchEntity: If the path does not exist. NotEmpty: If the path is not empty. ''' tufo = self.core.getTufoByProp('axon:path', path) if not tufo: raise s_common.NoSuchEntity() nlinks = tufo[1].get('axon:path:st_nlink') if nlinks != 2: raise s_common.NotEmpty() parent = tufo[1].get('axon:path:dir') if parent: parentfo = self.core.getTufoByProp('axon:path', parent) self.core.incTufoProp(parentfo, 'st_nlink', -1) self.core.delTufo(tufo)
def fs_rename(self, src, dst): ''' Renames a file. Example: axon.fs_rename('/myfile', '/mycoolerfile') ''' _, srcprops = self.core.getPropNorm('axon:path', src) srcppath, srcfname = srcprops.get('dir'), srcprops.get('base') _, dstprops = self.core.getPropNorm('axon:path', dst) dstppath, dstfname = dstprops.get('dir'), dstprops.get('base') with self.flock: srcfo = self.core.getTufoByProp('axon:path', src) if not srcfo: raise s_common.NoSuchEntity() src_isdir = Axon._fs_isdir(srcfo[1].get('axon:path:st_mode')) psrcfo = self.core.getTufoByProp('axon:path', srcppath) if not (psrcfo and Axon._fs_isdir(psrcfo[1].get('axon:path:st_mode'))): raise s_common.NoSuchDir() pdstfo = self.core.getTufoByProp('axon:path', dstppath) if not (pdstfo and Axon._fs_isdir(pdstfo[1].get('axon:path:st_mode'))): raise s_common.NoSuchDir() # prepare to set dst props to what src props were dstprops = Axon._get_renameprops(srcfo) dstprops.update({'dir': dstppath}) # create or update the dstfo node dstfo = self.core.formTufoByProp('axon:path', dst, **dstprops) dst_isdir = Axon._fs_isdir(dstfo[1].get('axon:path:st_mode')) dst_isemptydir = dstfo[1].get('axon:path:st_nlink', -1) == 2 dstfo_isnew = dstfo[1].get('.new') if dst_isdir and not dst_isemptydir and not dstfo_isnew: raise s_common.NotEmpty() # all pre-checks complete if dstfo_isnew: # if a new file was created, increment its parents link count ?? self.core.incTufoProp(pdstfo, 'st_nlink', 1) else: # Now update dstfo props self.core.setTufoProps(dstfo, **dstprops) # if overwriting a regular file with a dir, remove its st_size if src_isdir: self.core.delRowsByIdProp(dstfo[0], 'axon:path:st_size') self._fs_reroot_kids(src, dst) # Remove src and decrement its parent's link count self.core.delTufo(srcfo) self.core.incTufoProp(psrcfo, 'st_nlink', -1)
def _fs_tufo2attr(tufo): if not tufo: raise s_common.NoSuchEntity() attrs = {} for attr in _fs_attrs: val = tufo[1].get('axon:path:%s' % attr) if val is not None: attrs[attr] = val return attrs
def fs_rmdir(self, path): ''' Removes a directory. Example: axon.fs_rmdir('/mydir') ''' tufo = self.core.getTufoByProp('axon:path', path) if not tufo: raise s_common.NoSuchEntity() nlinks = tufo[1].get('axon:path:st_nlink') if nlinks != 2: raise s_common.NotEmpty() parent = tufo[1].get('axon:path:dir') if parent: parentfo = self.core.getTufoByProp('axon:path', parent) self.core.incTufoProp(parentfo, 'st_nlink', -1) self.core.delTufo(tufo)
def fs_read(self, path, size, offset): ''' Reads a file. Args: path (str): Path to read size (int): Number of bytes to read. offset (int): File offset to retrieve. Examples: Get the bytes of a file:: byts = axon.fs_read('/dir/file1', 100, 0) Returns: bytes: Bytes read from the path. Raises: NoSuchEntity: If the path does not exist. ''' tufo = self.core.getTufoByProp('axon:path', path) if not tufo: raise s_common.NoSuchEntity() bval = tufo[1].get('axon:path:blob') blob = None if bval: blob = self.core.getTufoByProp('axon:blob', bval) if not blob: return b'' boff = blob[1].get('axon:blob:off') blob[1][ 'axon:blob:off'] = boff + offset # the offset of the blob in the axon + the offset within the file blob[1][ 'axon:blob:size'] = size # number of bytes that the OS asks for return b''.join(self.iterblob(blob))