Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.pfabulist.lindwurm.stellvertreter;
  
  
  import java.nio.file.*;
  import java.util.Deque;
 import java.util.Set;
 import java.util.UUID;
 
 import static de.pfabulist.elsewhere.Result.NONE;
 import static de.pfabulist.elsewhere.Result.ONE;
 import static de.pfabulist.elsewhere.Result.ELSEWHEREGONE;
 import static de.pfabulist.kleinod.errors.Unchecked.runtime;

** BEGIN LICENSE BLOCK ***** BSD License (2 clause) Copyright (c) 2006 - 2014, Stephan Pfab All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Stephan Pfab BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **** END LICENSE BLOCK ****
 
 public class EncryptedElsewhereController implements ElsewhereController {
 
     private static final Logger LOG = Logger.getLoggerEncryptedElsewhereController.class.getName() );
 
     private final DeEnc deenc;
 
     private Deque<LocalFile>   changedPaths = new ConcurrentLinkedDeque<>();
     private Set<LocalFile>     all = new HashSet<>();
     private Deque<Path>     deleted = new ConcurrentLinkedDeque<>();
     private final Elsewhere elsewhere;
 
     public EncryptedElsewhereController(Path elseRootDeEnc deenc) {
         thisnew FSElsewhereelseRoot ), deenc );
     }
 
     public EncryptedElsewhereController(Elsewhere elsewDeEnc deenc) {
         this. = deenc;
         this. = elsew;
     }
 
 
     @Override
     public void add(LocalFile localFile) {
 
         if ( .contains(localFile) ) {
             return;
         }
 
         .add(localFile);
         .add(localFile);
     }
 
     @Override
     public void delete(Path id) {
         .add(id);
     }
 
     @Override
     public boolean needsSync() {
         return !.isEmpty() || !.isEmpty();
     }
 
     @Override
     public boolean elsewhereIsUptodate(LocalFile localFile) { // TODO
 
         if ( !exists(localFile)) {
             return false;
         }
 
         FileTime elseChange = .lastModifiedTime(localFile.getUUID().toString());
         return elseChange.compareTolocalFile.getLastSyncTime()) >= 0;
     }
 
    @Override
    public boolean existsLocalFile localFile) {
        return .existslocalFile.getUUID().toString());
    }
    private Result syncOneNormal() {
        LocalFile localFile = .pollFirst();
        // TODO
        if ( localFile == null || .contains(localFile)) {
            return ;
        }
        try {
            if ( !Files.existslocalFile.getPath() )) {
                if ( Files.existslocalFile.getFileMetaPath() )) {
                    // information loss
//                    System.out.println("!!!!woo");
//                    LOG.severe( "wooot" );
                    // TODO
                    // this is either
                    // a) the file was before: synced, purged, not further modified -> remove from que ?
                    // b) deleted outside stellvertreterfs -> problem
                    return // TODO or error ?
                }
                // already deleted
                return ;
            } else {
                BasicFileAttributes bfaHost = Files.readAttributeslocalFile.getPath(), BasicFileAttributes.class );
                localFile.setAttributes(bfaHost);
                if (  == null ) {
                    boolean immediate = .putlocalFile.getUUID().toString(),
                                                       Files.newInputStream(localFile.getPath()),
                                                       Files.sizelocalFile.getPath()));
//                    Path elsewherPath = getElsewherePath(bridge);
//                    Files.createDirectories( elsewherPath.getParent() );
//                    Files.copy(bridge.getPath(), elsewherPath, StandardCopyOption.REPLACE_EXISTING);
                } else {
//                    new CopyEncrypted(deenc).copyEncrypt(bridge.getPath(), getElsewherePath(bridge));
                }
                localFile.setSyncTimeNow();
//                LOG.info( "synced " + bridge.getPath() + " to " + getElsewherePath(bridge) );
                return ;
            }
        } catch (Throwable e) {
            try {
                if ( !.connected()) {
                    .info("elsewhere is gone");
                    .addFirst(localFile);
                    return ;
                }
                .warning("some sync problem " + e);
                .addFirst(localFile);
            } catch ( Throwable t ) {
                .info("elsewhere is gone");
                .addFirst(localFile);
                return ;
            }
            return // TODO or error ?
        }
    }
    private Result syncOneDeleted() {
        Path deletedFileMethPath = .pollFirst();
        if ( deletedFileMethPath == null ) {
            return ;
        }
        UUID id = UUID.fromString(deletedFileMethPath.getFileName().toString());
        //Result res =
                .remove(id.toString());
//        if ( res.equals(Result.ELSEWHEREGONE) ) {
//            deleted.addFirst(deletedFileMethPath);
//            return ELSEWHEREGONE;
//        }
        // TODO if it only called after ... might be neccessary threads
//        if ( !elsewhere.connected()) {
//            deleted.addFirst(deletedFileMethPath);
//            return ELSEWHEREGONE;
//        }
//        Optional<FileMeta> meta = id.getFileMetaDeleted();
//
//        if ( !meta.isPresent() ) {
//            // file already gone
//            return NONE;
//        }
//        Path elsewhereLocation = elsewherePath.resolve( getElsewherePath( id ));
//        if ( Files.exists(elsewhereLocation)) {
//            try {
//                Files.delete(elsewhereLocation );
//            } catch (IOException e) {
//                LOG.warning( "can not delete " + elsewhereLocation );
//                return NONE; // TODO elsewhere prop ?
//            }
//        }
        try {
            Files.delete(deletedFileMethPath);
        } catch (IOException e) {
            .warning("can not delete deletedFileMeta" + deletedFileMethPath );
        }
        return ;
    }
    public Result syncOne() {
        Result res = syncOneNormal();
        if ( .equals(res)) {
//            if ( elsewherePath.startsWith( "/Volumes" )) {
//                int pos = elsewherePath.toString().indexOf( "/", "/Volumes/".length() );
//                Path plus1 = Paths.get( elsewherePath.toString().substring(0, 17) + " 1" + elsewherePath.toString().substring( 17));
//                System.out.println( "---------------" + plus1 );
//
//                if ( Files.exists( plus1 )) {
//                    elsewherePath = plus1;
//                }
//            }
        }
        if ( !.equalsres )) {
            return res;
        }
        return syncOneDeleted();
    }
    @Override
    public void sync() {
        while ( syncOne().equals ) ) {
        }
    }
    @Override
    public void ensureLocalLocalFile localFile){
        if ( localFile.isRoot() || !localFile.exists() ) {
            return;
        }
        if ( Files.existslocalFile.getPath())) {
            return// already there
        }
        if ( localFile.getSize() == 0 ) {
            return;
        }
        if ( !.connected()) {
            throw new NoElsewhereException"elsewhere root is not connected" );
        }
        try {
            .getlocalFile.getUUID().toString(), Files.newOutputStreamlocalFile.getPath() ));
        } catch (IOException e) {
            throw runtime(e);
        }
//        boolean elsewhereFilesExists = true;
//        try {
//            elsewhereFilesExists = Files.exists( getElsewherePath(bridge));
//        } catch ( Throwable exp ) {
//            try {
//                if (!elsewhere.connected()) {
//                    throw new NoElsewhereException("elsewhere root is not connected " + elsewherePath);
//                }
//            } catch ( Throwable e ) {
//                throw new NoElsewhereException("elsewhere root is not connected " + elsewherePath);
//            }
//        }
//
//        if ( !elsewhereFilesExists ) {
//            if ( !!elsewhere.connected()) {
//                throw new NoElsewhereException("elsewhere root is not connected " + elsewherePath);
//            }
//
//            if ( bridge.getSize() == 0 ) {
//                return;
//            }
//
//            LOG.warning( "no elsewhere file " + getElsewherePath(bridge) );
//            throw new IllegalStateException( "no elsewhere file, possible information loss" );
//        }
//
//        // todo copy atties in one class
//        if ( deenc == null ) {
//            Files.copy( getElsewherePath(bridge), bridge.getPath());
//        } else {
//        //    new CopyEncrypted(deenc).copyDecrypt(getElsewherePath(bridge), bridge.getPath());
//        }
        localFile.lastModifiedTimeToReal();
    }
    @Override
    public ElsewhereInfo getElsewhereInfo() {
        return ;
    }
New to GrepCode? Check out our FAQ X