All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xyz.cofe.fs.unix.UnixFileBasic Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License
 *
 * Copyright 2014 Kamnev Georgiy ([email protected]).
 *
 * Данная лицензия разрешает, безвозмездно, лицам, получившим копию данного программного
 * обеспечения и сопутствующей документации (в дальнейшем именуемыми "Программное Обеспечение"),
 * использовать Программное Обеспечение без ограничений, включая неограниченное право на
 * использование, копирование, изменение, объединение, публикацию, распространение, сублицензирование
 * и/или продажу копий Программного Обеспечения, также как и лицам, которым предоставляется
 * данное Программное Обеспечение, при соблюдении следующих условий:
 *
 * Вышеупомянутый копирайт и данные условия должны быть включены во все копии
 * или значимые части данного Программного Обеспечения.
 *
 * ДАННОЕ ПРОГРАММНОЕ ОБЕСПЕЧЕНИЕ ПРЕДОСТАВЛЯЕТСЯ «КАК ЕСТЬ», БЕЗ ЛЮБОГО ВИДА ГАРАНТИЙ,
 * ЯВНО ВЫРАЖЕННЫХ ИЛИ ПОДРАЗУМЕВАЕМЫХ, ВКЛЮЧАЯ, НО НЕ ОГРАНИЧИВАЯСЬ ГАРАНТИЯМИ ТОВАРНОЙ ПРИГОДНОСТИ,
 * СООТВЕТСТВИЯ ПО ЕГО КОНКРЕТНОМУ НАЗНАЧЕНИЮ И НЕНАРУШЕНИЯ ПРАВ. НИ В КАКОМ СЛУЧАЕ АВТОРЫ
 * ИЛИ ПРАВООБЛАДАТЕЛИ НЕ НЕСУТ ОТВЕТСТВЕННОСТИ ПО ИСКАМ О ВОЗМЕЩЕНИИ УЩЕРБА, УБЫТКОВ
 * ИЛИ ДРУГИХ ТРЕБОВАНИЙ ПО ДЕЙСТВУЮЩИМ КОНТРАКТАМ, ДЕЛИКТАМ ИЛИ ИНОМУ, ВОЗНИКШИМ ИЗ, ИМЕЮЩИМ
 * ПРИЧИНОЙ ИЛИ СВЯЗАННЫМ С ПРОГРАММНЫМ ОБЕСПЕЧЕНИЕМ ИЛИ ИСПОЛЬЗОВАНИЕМ ПРОГРАММНОГО ОБЕСПЕЧЕНИЯ
 * ИЛИ ИНЫМИ ДЕЙСТВИЯМИ С ПРОГРАММНЫМ ОБЕСПЕЧЕНИЕМ.
 */

package xyz.cofe.fs.unix;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import xyz.cofe.common.Reciver;
import xyz.cofe.fs.AbstractFile;
import xyz.cofe.fs.FileSystem;
import xyz.cofe.fs.FileVisitor;
import xyz.cofe.fs.OpenMode;
import xyz.cofe.fs.Path;
import xyz.cofe.fs.UnixFile;
import xyz.cofe.fs.UnixFileStat;
import xyz.cofe.fs.UnixPath;
import xyz.cofe.fs.VisitOptions;
import xyz.cofe.fs.unix.impl.FileStatImpl;
import xyz.cofe.fs.unix.impl.UnixPathImpl;
import xyz.cofe.unix.libc.FileStatMode;
import xyz.cofe.unix.libc.ResourcesInfo;
import xyz.cofe.unix.libc.UnixFileIO;

/**
 *
 * @author Kamnev Georgiy ([email protected])
 */
public class UnixFileBasic
extends AbstractFile
implements xyz.cofe.fs.UnixFile {
    //
    private static void logFine(String message,Object ... args){
        Logger.getLogger(UnixFileBasic.class.getName()).log(Level.FINE, message, args);
    }

    private static void logFiner(String message,Object ... args){
        Logger.getLogger(UnixFileBasic.class.getName()).log(Level.FINER, message, args);
    }

    private static void logFinest(String message,Object ... args){
        Logger.getLogger(UnixFileBasic.class.getName()).log(Level.FINEST, message, args);
    }

    private static void logInfo(String message,Object ... args){
        Logger.getLogger(UnixFileBasic.class.getName()).log(Level.INFO, message, args);
    }

    private static void logWarning(String message,Object ... args){
        Logger.getLogger(UnixFileBasic.class.getName()).log(Level.WARNING, message, args);
    }

    private static void logSevere(String message,Object ... args){
        Logger.getLogger(UnixFileBasic.class.getName()).log(Level.SEVERE, message, args);
    }

    private static void logException(Throwable ex){
        Logger.getLogger(UnixFileBasic.class.getName()).log(Level.SEVERE, null, ex);
    }
    //

    private UnixPath unixPath = null;
    private UnixFileConfig config = null;
    private final UnixFileSystem unixFileSystem;

    public UnixFileBasic(UnixFileSystem unixFileSystem, UnixFileConfig conf, String filePath){
        if( conf==null )throw new IllegalArgumentException( "conf==null" );
        if( filePath==null )throw new IllegalArgumentException( "filePath==null" );
        unixPath = createUnixPath(filePath);
        config = conf;
        this.unixFileSystem = unixFileSystem;
    }

    public UnixFileBasic(UnixFileSystem unixFileSystem, UnixFileConfig conf, UnixPath unixPath){
        if( conf==null )throw new IllegalArgumentException( "conf==null" );
        if( unixPath==null )throw new IllegalArgumentException( "unixPath==null" );
        this.unixPath = unixPath;
        config = conf;
        this.unixFileSystem = unixFileSystem;
    }
    
    public UnixFileBasic(UnixFileBasic src){
        if( src==null )throw new IllegalArgumentException( "src==null" );
        this.unixFileSystem = src.unixFileSystem;
        this.config = src.config;
        this.unixPath = new UnixPath(src.unixPath);
    }
    
    @Override
    public UnixFileBasic clone(){
        return new UnixFileBasic(this);
    }

    @Override
    public FileSystem getFileSystem() {
        return unixFileSystem;
    }

    protected UnixPath createUnixPath(String path){
        return new UnixPathImpl(path);
    }

    protected UnixPath createUnixPath(UnixPath parent, String child){
        return new UnixPathImpl(parent,child);
    }

    @Override
    public boolean isAbsolute() {
        return unixPath.isAbsolute();
    }

    @Override
    public boolean isRoot() {
        String[] cmpts = unixPath.getComponents();
        if( cmpts==null )return true;
        if( cmpts.length==0 )return true;
        if( cmpts[0]==null )return true;
        if( cmpts[0].length()==0 )return true;
        return false;
    }

    @Override
    public UnixFile getCanonical() {
        return new UnixFileBasic( unixFileSystem, config, unixPath.getCanonical());
    }

    @Override
    public UnixFile getAbsolute() {
        UnixPath abs = unixPath.getAbsolute(createUnixPath(io().getcwd()));
        return new UnixFileBasic( unixFileSystem, config, abs);
    }

    @Override
    public UnixFileStat getFileStat() {
        return filestat();
    }

    @Override
    public UnixPath getUnixPath() {
        return unixPath;
    }

    private transient WeakReference getParent = null;

    @Override
    public UnixFile getParent() {
        if( config.getParent_cache(this) ){
            if( getParent!=null ){
                UnixFile f = getParent.get();
                if( f!=null )return f;
            }
            UnixPath parentPath = unixPath.getParent();
            if( parentPath!=null ){
                UnixFile f = new UnixFileBasic( unixFileSystem, config,parentPath);
                getParent = new WeakReference(f);
                return f;
            }
        }else{
            UnixPath parentPath = unixPath.getParent();
            if( parentPath!=null ){
                UnixFile f = new UnixFileBasic( unixFileSystem, config,parentPath);
                getParent = new WeakReference(f);
                return f;
            }
        }
        return null;
    }

    private transient WeakHashMap child_cahce_map = null;
    private WeakHashMap child_cahce_map(){
        if( child_cahce_map!=null )return child_cahce_map;
        child_cahce_map = new WeakHashMap();
        return child_cahce_map;
    }

    @Override
    public UnixFile getChild(String name) {
        if( name==null )throw new IllegalArgumentException( "name==null" );

        if( config.getChild_cache(this) ){
            UnixFile ucfile = child_cahce_map().get(name);
            if( ucfile!=null )return ucfile;

            UnixPath cname = createUnixPath(unixPath, name);
            ucfile = new UnixFileBasic( unixFileSystem, config,cname);
            child_cahce_map().put(name, ucfile);
            return ucfile;
        }else{
            UnixPath cname = createUnixPath(unixPath, name);
            UnixFile ucfile = new UnixFileBasic( unixFileSystem, config,cname);
            child_cahce_map().put(name, ucfile);
            return ucfile;
        }
    }

    transient private Boolean isDirectory = null;

    @Override
    public boolean isDirectory() {
        if( config.isDirectory_cache(this) ){
            if( isDirectory!=null ){
                return isDirectory;
            }
            if( config.isDirectory_useFileStat(this) ){
                FileStatImpl fs = filestat();
                if( fs==null ){
                    isDirectory = new java.io.File(unixPath.toString()).isDirectory();
                    return isDirectory;
                }else{
                    isDirectory = FileStatMode.isDirectory( fs );
                    return isDirectory;
                }
            }else{
                isDirectory = new java.io.File(unixPath.toString()).isDirectory();
                return isDirectory;
            }
        }else{
            isDirectory = new java.io.File(unixPath.toString()).isDirectory();
            return isDirectory;
        }
    }

    transient private Boolean isFile = null;

    @Override
    public boolean isFile() {
        if( config.isFile_cache(this) ){
            if( isFile!=null )return isFile;
            if( config.isFile_useFileStat(this) ){
                FileStatImpl fs = filestat();
                if( fs==null ){
                    isFile = new java.io.File(unixPath.toString()).isFile();
                    return isFile;
                }else{
                    isFile = FileStatMode.isRegularFile( fs );
                    return isFile;
                }
            }else{
                isFile = new java.io.File(unixPath.toString()).isFile();
                return isFile;
            }
        }else{
            isFile = new java.io.File(unixPath.toString()).isFile();
            return isFile;
        }
    }

    transient private Boolean isExists = null;

    @Override
    public boolean isExists() {
        if( config.isFile_cache(this) ){
            if( isExists!=null ){
                return isExists;
            }
            isExists = new java.io.File(unixPath.toString()).exists();
            return isExists;
        }else{
            isExists = new java.io.File(unixPath.toString()).exists();
            return isExists;
        }
    }

    @Override
    public List listFiles() {
        ArrayList res = new ArrayList();
        String[] content = config.io(this).readdir(unixPath.toString());
        if( content==null ){
            return res;
//            throw new Error("error reading directory "+unixPath.toString());
        }

        for( String c : content ){
            if( c==null )continue;
            if( c.equals(".") )continue;
            if( c.equals("..") )continue;
            res.add( getChild(c) );
        }
        return res;
    }

    transient private Date getModifyDate = null;

    @Override
    public Date getModifyDate() {
        if( config.getModifyDate_cache(this) ){
            if( getModifyDate!=null )return getModifyDate;
            UnixFileStat fs = filestat();
            if( fs==null ){
                Date d = new Date(new java.io.File(getPath()).lastModified());
                getModifyDate = d;
                return d;
            }
            long sec = fs.getModifyTime();
            getModifyDate = new Date(sec * 1000);
            return getModifyDate;
        }else{
            UnixFileStat fs = filestat();
            if( fs==null ){
                Date d = new Date(new java.io.File(getPath()).lastModified());
                getModifyDate = d;
                return d;
            }
            long sec = fs.getModifyTime();
            getModifyDate = new Date(sec * 1000);
            return getModifyDate;
        }
    }

    @Override
    public Date getCreateDate() {
        UnixFileStat fs = filestat();
        if( fs==null ){
            Date d = new Date(new java.io.File(getPath()).lastModified());
            return d;
        }
        long sec = fs.getCreateTime();
        Date d = new Date(sec * 1000);
        return d;
    }

    @Override
    public Date getAccessDate() {
        UnixFileStat fs = filestat();
        if( fs==null ){
            Date d = new Date(new java.io.File(getPath()).lastModified());
            return d;
        }
        long sec = fs.getAccessTime();
        Date d = new Date(sec * 1000);
        return d;
    }

    @Override
    public String[] getComponents() {
        return unixPath.getComponents();
    }

    @Override
    public String getName() {
        return unixPath.getName();
    }

    @Override
    public String getPath() {
        return unixPath.toString();
    }

    @Override
    public int compareTo(Path o) {
        int r = unixPath.toString().compareTo(
            o.getPath()
        );
        return r;
    }

    private UnixFileIO io(){
        return config.io(this);
    }

    transient private FileStatImpl filestat = null;
    private FileStatImpl filestat(){
        if( config.filestat_cache(this) ){
            if( filestat!=null )return filestat;
            if( !isExists() )return null;
            filestat = new FileStatImpl( io().lstat(unixPath.toString()) );
            return filestat;
        }else{
            if( !isExists() )return null;
            filestat = new FileStatImpl( io().lstat(unixPath.toString()) );
            return filestat;
        }
    }

    transient private Boolean isSymbolicLink = null;

    @Override
    public boolean isSymbolicLink() {
        if( config.isSymbolicLink_cache(this) ){
            if( isSymbolicLink!=null )return isSymbolicLink;
            FileStatImpl fs = filestat();
            if( fs==null ){
                isSymbolicLink = false;
                return isSymbolicLink;
            }
            isSymbolicLink = FileStatMode.isSymbolicLink( fs );
            return isSymbolicLink;
        }else{
            FileStatImpl fs = filestat();
            if( fs==null ){
                isSymbolicLink = false;
                return isSymbolicLink;
            }
            isSymbolicLink = FileStatMode.isSymbolicLink( fs );
            return isSymbolicLink;
        }
    }

    transient private String readLink = null;

    @Override
    public String readLink() {
        if( config.readLink_cache(this) ){
            if( readLink!=null )return readLink;
            if( !isExists() )return null;
            readLink = io().readlink(unixPath.toString());
            return readLink;
        }else{
            if( !isExists() )return null;
            readLink = io().readlink(unixPath.toString());
            return readLink;
        }
    }

    transient private UnixFileBasic resolveLink = null;

    @Override
    public UnixFileBasic resolveLink() {
        if( config.resolveLink_cache(this) ){
            if( resolveLink!=null )return resolveLink;
            String target = readLink();
            if( target==null )return null;
            UnixPath resPath =
            unixPath.resolveLink(new UnixPath(target), new UnixPath(io().getcwd()));
            if( resPath!=null ){
                resolveLink = new UnixFileBasic( unixFileSystem, config,resPath);
                return resolveLink;
            }
        }else{
            String target = readLink();
            if( target==null )return null;
            UnixPath resPath =
            unixPath.resolveLink(new UnixPath(target), new UnixPath(io().getcwd()));
            if( resPath!=null ){
                resolveLink = new UnixFileBasic( unixFileSystem, config,resPath);
                return resolveLink;
            }
        }
        return null;
    }

    transient private Long getLength=null;

    @Override
    public long getLength() {
        if( config.getParent_cache(this) ){
            if( getLength!=null )return getLength;
            getLength = new java.io.File(unixPath.toString()).length();
            return getLength;
        }else{
            getLength = new java.io.File(unixPath.toString()).length();
            return getLength;
        }
    }

    @Override
    public boolean isFifo() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isFifo(st);
    }

    @Override
    public boolean isCharDevice() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isCharDevice(st);
    }

    @Override
    public boolean isBlockDevice() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isBlockDevice(st);
    }

    @Override
    public boolean isRegularFile() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isRegularFile(st);
    }

    @Override
    public boolean isSocket() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isSocket(st);
    }

    @Override
    public boolean isUserRead() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isUserRead(st);
    }

    @Override
    public boolean isUserWrite() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isUserWrite(st);
    }

    @Override
    public boolean isUserExec() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isUserExec(st);
    }

    @Override
    public boolean isGroupRead() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isGroupRead(st);
    }

    @Override
    public boolean isGroupWrite() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isGroupWrite(st);
    }

    @Override
    public boolean isGroupExec() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isGroupExec(st);
    }

    @Override
    public boolean isOtherRead() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isOtherRead(st);
    }

    @Override
    public boolean isOtherWrite() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isOtherWrite(st);
    }

    @Override
    public boolean isOtherExec() {
        FileStatImpl st = filestat();
        if( st==null )return false;
        return FileStatMode.isOtherExec(st);
    }

    @Override
    public int getMode() {
        UnixFileStat st = filestat();
        if( st==null )return 0700;
        return st.getMode();
    }

    @Override
    public void setMode(int mode) {
        io().chmod(unixPath.toString(), mode);
        filestat = null;
    }

    @Override
    public void setUserRead(boolean read) {
        int mode = getMode();
        if( read )
            mode |= FileStatMode.USER_READ;
        else
            mode = mode ^ FileStatMode.USER_READ;
       setMode( mode);
    }

    @Override
    public void setUserWrite(boolean write) {
        int mode = getMode();
        if( write )
            mode |= FileStatMode.USER_WRITE;
        else
            mode = mode ^ FileStatMode.USER_WRITE;
        setMode( mode);
    }

    @Override
    public void setUserExec(boolean exec) {
        int mode = getMode();
        if( exec )
            mode |= FileStatMode.USER_EXEC;
        else
            mode = mode ^ FileStatMode.USER_EXEC;
        setMode( mode );
    }

    @Override
    public void setGroupRead(boolean value) {
        int mode = getMode();
        if( value )
            mode |= FileStatMode.GROUP_READ;
        else
            mode = mode ^ FileStatMode.GROUP_READ;
        setMode( mode );
    }

    @Override
    public void setGroupWrite(boolean value) {
        int mode = getMode();
        if( value )
            mode |= FileStatMode.GROUP_WRITE;
        else
            mode = mode ^ FileStatMode.GROUP_WRITE;
        setMode( mode );
    }

    @Override
    public void setGroupExec(boolean value) {
        int mode = getMode();
        if( value )
            mode |= FileStatMode.GROUP_EXEC;
        else
            mode = mode ^ FileStatMode.GROUP_EXEC;
        setMode( mode );
    }

    @Override
    public void setOtherRead(boolean value) {
        int mode = getMode();
        if( value )
            mode |= FileStatMode.OTHER_READ;
        else
            mode = mode ^ FileStatMode.OTHER_READ;
        setMode( mode );
    }

    @Override
    public void setOtherWrite(boolean value) {
        int mode = getMode();
        if( value )
            mode |= FileStatMode.OTHER_WRITE;
        else
            mode = mode ^ FileStatMode.OTHER_WRITE;
        setMode( mode );
    }

    @Override
    public void setOtherExec(boolean value) {
        int mode = getMode();
        if( value )
            mode |= FileStatMode.OTHER_EXEC;
        else
            mode = mode ^ FileStatMode.OTHER_EXEC;
        setMode( mode );
    }

    @Override
    public int getUID() {
        UnixFileStat st = filestat();
        if( st==null )return 0;
        return st.getUID();
    }

    @Override
    public void setUID(int uid) {
        UnixFileStat st = filestat();
        if( st==null )throw new Error("can't fetch lstat("+unixPath.toString()+")\nfilestat()==null");
        int gid = st.getGID();
        io().chown(unixPath.toString(), uid, gid);
        filestat = null;
    }

    @Override
    public int getGID() {
        UnixFileStat st = filestat();
        if( st==null )return 0;
        return st.getGID();
    }

    @Override
    public void setGID(int gid) {
        UnixFileStat st = filestat();
        if( st==null )throw new Error("can't fetch lstat("+unixPath.toString()+")\nfilestat()==null");
        int uid = st.getUID();
        io().chown(unixPath.toString(), uid, gid);
        filestat = null;
    }

    @Override
    public String getType() {
        FileStatImpl st = filestat();
        if( st==null )throw new Error("can't fetch lstat("+unixPath.toString()+")\nfilestat()==null");
        return FileStatMode.getType(st).name();
    }

    @Override
    public void setOwner(int uid, int gid) {
        io().chown(unixPath.toString(), uid, gid);
        filestat = null;
    }

    @Override
    public BufferedInputStream openRead() {
        try{
            String path = getPath();
            FileInputStream fin = new FileInputStream(path);
            BufferedInputStream bin = new BufferedInputStream(fin,ResourcesInfo.getPageSize());
            return bin;
        } catch( IOException ex ) {
            throw new IOError(ex);
        }
    }

    @Override
    public BufferedOutputStream openWrite() {
        try{
            String path = getPath();
            FileOutputStream fout = new FileOutputStream(path);
            BufferedOutputStream bout = new BufferedOutputStream(fout,ResourcesInfo.getPageSize());
            return bout;
        } catch( IOException ex ) {
            throw new IOError(ex);
        }
    }

    @Override
    public BufferedOutputStream openAppend() {
        try{
            String path = getPath();
            FileOutputStream fout = new FileOutputStream(path,true);
            BufferedOutputStream bout = new BufferedOutputStream(fout,ResourcesInfo.getPageSize());
            return bout;
        } catch( IOException ex ) {
            throw new IOError(ex);
        }
    }

    @Override
    public RandomAccessFile openReadWrite(OpenMode mode) {
        if( mode==null )throw new IllegalArgumentException( "mode==null" );
        try{
            String m = "r";
            switch( mode ){
                case Read: m="r"; break;
                case ReadWrite: m="rw"; break;
                case ReadWriteSync: m="rws"; break;
                case ReadWriteSyncMeta: m="rwd"; break;
            }
            String path = getPath();
            return new RandomAccessFile(path, m);
        } catch( IOException ex ) {
            throw new IOError(ex);
        }
    }

    @Override
    public String toString() {
        return unixPath.toString();
    }

    @Override
    public Iterable walk() {
        FileVisitor visitor = new FileVisitor();
        VisitOptions vopt = new VisitOptions(true);
        apply(visitor, vopt);
        return visitor.walkIterable(this);
    }

    @Override
    public Iterable walk(VisitOptions opts) {
        FileVisitor visitor = new FileVisitor();
        VisitOptions vopt = opts==null ? new VisitOptions(true) : opts;
        apply(visitor, vopt);
        return visitor.walkIterable(this);
    }

    @Override
    public boolean isReadable() {
        return new java.io.File(getPath()).canRead();
    }

    @Override
    public void setReadable(boolean readable) {
        new java.io.File(getPath()).setReadable(readable);
    }

    @Override
    public void setReadable(boolean readable, boolean ownerOnly) {
        new java.io.File(getPath()).setReadable(readable,ownerOnly);
    }

    @Override
    public boolean isWritable() {
        return new java.io.File(getPath()).canWrite();
    }

    @Override
    public void setWritable(boolean writable) {
        new java.io.File(getPath()).setWritable(writable);
    }

    @Override
    public void setWritable(boolean writable, boolean ownerOnly) {
        new java.io.File(getPath()).setWritable(writable,ownerOnly);
    }

    @Override
    public boolean isExecutable() {
        return new java.io.File(getPath()).canExecute();
    }

    @Override
    public void setExecutable(boolean executable) {
        new java.io.File(getPath()).setExecutable(executable);
    }

    @Override
    public void setExecutable(boolean executable, boolean ownerOnly) {
        new java.io.File(getPath()).setExecutable(executable,ownerOnly);
    }

    @Override
    public void setModifyDate(Date modifyDate) {
        if( modifyDate==null )throw new IllegalArgumentException( "modifyDate==null" );
        new java.io.File(getPath()).setLastModified(modifyDate.getTime());
        filestat = null;
        getModifyDate = null;
    }

    private void apply( FileVisitor visitor, VisitOptions opts ){
        if( visitor==null || opts==null )return;
        Boolean v;
        v = opts.isFollowLink();
        if( v!=null )visitor.setFollowLink( v );
    }

    @Override
    public void delete(){
        delete( null );
    }

    @Override
    public void delete(final Reciver deleted) {
        VisitOptions vopt = new VisitOptions(false);
        delete( deleted, vopt );
    }

    @Override
    public void delete(final Reciver deleted,VisitOptions opts) {
        FileVisitor visitor = new FileVisitor(){
            @Override
            public synchronized void exit(Stack path) {
                try{
                    if( !path.empty() ){
                        xyz.cofe.fs.File file = path.peek();
                        if( file instanceof UnixFile ){
                            UnixFile ufile = (UnixFile)file;
                            String p = ufile.getPath();
                            UnixFileStat fs = ufile.getFileStat();
                            if( FileStatMode.isDirectory( new FileStatImpl(fs) ) ){
                                io().rmdir(p);
                                if( deleted!=null )deleted.recive(ufile);
                            }else{
                                io().unlink(p);
                                if( deleted!=null )deleted.recive(ufile);
                            }
                        }
                    }
                }
                catch(Throwable e){
                    logException(e);
                }
            }
        };
        apply(visitor, opts);
        visitor.walk(this);
    }

    @Override
    public void mkdir(int mode){
        String path = getPath();
        io().mkdir(path, mode);
    }

    @Override
    public void mkdirs(int mode){
        UnixFile parent = getParent();
        if( parent!=null && !parent.isExists() ){
            parent.mkdirs(mode);
        }

        String path = getPath();
        io().mkdir(path, mode);
    }

    private int default_mkdir_mode(){
        return 0775;
    }

    @Override
    public void mkdir() {
        mkdir(default_mkdir_mode());
    }

    @Override
    public void mkdirs() {
        mkdirs(default_mkdir_mode());
    }

    @Override
    public void renameTo( xyz.cofe.fs.File file ){
        if( file==null )throw new IllegalArgumentException( "file==null" );

        String targetPath = file.getPath();
        String srcPath = this.getPath();
        io().rename(srcPath, targetPath);
        this.unixPath = createUnixPath(targetPath);
        this.child_cahce_map().clear();
        this.filestat = null;
        this.getLength = null;
        this.getModifyDate = null;
        this.getParent = null;
        this.isDirectory = null;
        this.isExists = null;
        this.isFile = null;
        this.isSymbolicLink = null;
        this.readLink = null;
        this.resolveLink = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy