/* * Depth first search for a parent that corresponds a local file */ private File pathToLocal(String remoteFileId, List<ParentReference> path) throws IOException { List<ParentReference> pRefs = drive.parents().list(remoteFileId).execute().getItems(); for (ParentReference pRef : pRefs) { if (pRef.getIsRoot()) { return Configuration.getLocalRoot(); } if (stub.storage().remoteToLocal().containsKey(pRef.getId())) { return DriveUtils.absolutePath(stub.storage().remoteToLocal().get(pRef.getId())); } else { path.add(pRef); File lookInside = pathToLocal(pRef.getId(), path); if (null != lookInside) { return lookInside; } else { path.remove(pRef); } } } return null; }
@Override public void init() throws IOException { // Detect and store change number long latestChange = changes(null); if (latestChange != -1) stub.storage().put(StorageService.REMOTE_CHANGE, latestChange); // Download files stub.storage().put(StorageService.REMOTE_TO_LOCAL, new HashMap<String, String>()); stub.storage().put(StorageService.LOCAL_TO_REMOTE, new HashMap<String, String>()); String remoteRoot = Constants.FOLDER_ROOT; java.io.File localRoot = Configuration.getLocalRoot(); if (!localRoot.exists()) { localRoot.mkdirs(); } String localRelative = DriveUtils.relativePath(localRoot); stub.storage().remoteToLocal().put(remoteRoot, localRelative); stub.storage().localToRemote().put(localRelative, remoteRoot); for (ChildReference childref : drive.children().list(remoteRoot).execute().getItems()) { stub.transmit().download(childref.getId(), localRoot); } stub.storage().put(StorageService.SNAPSHOT, stub.snapshot().make()); }