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_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)