/** * Clears away any URNs for files that do not exist anymore. * @param shouldClearURNSetMap true if you want to clear TIME_TO_URNSET_MAP * too */ private void pruneTimes(boolean shouldClearURNSetMap) { // if i'm using FM, always grab that lock first and then me. be quick // about it though :) synchronized (RouterService.getFileManager()) { synchronized (this) { Iterator iter = URN_TO_TIME_MAP.entrySet().iterator(); while (iter.hasNext()) { Map.Entry currEntry = (Map.Entry) iter.next(); if(!(currEntry.getKey() instanceof URN) || !(currEntry.getValue() instanceof Long)) { iter.remove(); dirty = true; continue; } URN currURN = (URN) currEntry.getKey(); Long cTime = (Long) currEntry.getValue(); // check to see if file still exists // NOTE: technically a URN can map to multiple FDs, but I only want // to know about one. getFileDescForUrn prefers FDs over iFDs. FileDesc fd = RouterService.getFileManager().getFileDescForUrn(currURN); if ((fd == null) || (fd.getFile() == null) || !fd.getFile().exists()) { dirty = true; iter.remove(); if (shouldClearURNSetMap) removeURNFromURNSet(currURN, cTime); } } } } }
public static FileDesc loadFile(Path root, Path file, int blocSize) throws NoSuchAlgorithmException, FileNotFoundException, IOException { MessageDigest md = MessageDigest.getInstance("SHA-512"); MessageDigest fileMd = MessageDigest.getInstance("SHA-512"); FileDesc desc = new FileDesc(file.toString(), null, null); List<Bloc> list = new ArrayList<Bloc>(); try (FileInputStream fis = new FileInputStream(root.resolve(file).toString())) { byte[] buf = new byte[blocSize]; byte[] h; int s; while ((s = fis.read(buf)) != -1) { int c; while (s < buf.length && (c = fis.read()) != -1) buf[s++] = (byte) c; fileMd.update(buf, 0, s); // padding byte p = 0; while (s < buf.length) buf[s++] = ++p; h = md.digest(buf); Bloc bloc = new Bloc(RollingChecksum.compute(buf), new Hash(h)); list.add(bloc); } h = fileMd.digest(); desc.fileHash = new Hash(h); desc.blocs = list.toArray(new Bloc[0]); } return desc; }
@Override public long getNumBytes() { long bytes = 0; for (FileDesc fd : fileDescList) { bytes += fd.getFileSize(); } return bytes; }
/** * Returns an List of URNs, from 'youngest' to 'oldest'. * @param max the maximum number of URNs you want returned. if you * want all, give Integer.MAX_VALUE. * @param request in case the query has meta-flags, you can give it to * me. null is fine though. * @return a List ordered by younger URNs. */ public List getFiles(final QueryRequest request, final int max) throws IllegalArgumentException { // if i'm using FM, always grab that lock first and then me. be quick // about it though :) synchronized (RouterService.getFileManager()) { synchronized (this) { if (max < 1) throw new IllegalArgumentException("bad max = " + max); List urnList = new ArrayList(); Iterator iter = TIME_TO_URNSET_MAP.entrySet().iterator(); final MediaType.Aggregator filter = (request == null ? null : MediaType.getAggregator(request)); // may be non-null at loop end List toRemove = null; // we bank on the fact that the TIME_TO_URNSET_MAP iterator returns the // entries in descending order.... while (iter.hasNext() && (urnList.size() < max)) { Map.Entry currEntry = (Map.Entry) iter.next(); Set urns = (Set) currEntry.getValue(); // only put as many as desired, and possibly filter results based // on what the query desires Iterator innerIter = urns.iterator(); while ((urnList.size() < max) && innerIter.hasNext()) { URN currURN = (URN) innerIter.next(); FileDesc fd = RouterService.getFileManager().getFileDescForUrn(currURN); // unfortunately fds can turn into ifds so ignore if ((fd == null) || (fd instanceof IncompleteFileDesc)) { if (toRemove == null) toRemove = new ArrayList(); toRemove.add(currURN); continue; } if (filter == null) urnList.add(currURN); else if (filter.allow(fd.getFileName())) urnList.add(currURN); } } // clear any ifd's or unshared files that may have snuck into structures if (toRemove != null) { Iterator removees = toRemove.iterator(); while (removees.hasNext()) { URN currURN = (URN) removees.next(); removeTime(currURN); } } return urnList; } } }
/** * Constructs a new <tt>Response</tt> instance from the data in the specified <tt>FileDesc</tt>. * * @param fd the <tt>FileDesc</tt> containing the data to construct this <tt>Response</tt> -- must * not be <tt>null</tt> */ public Response(FileDesc fd) { this( fd.getIndex(), fd.getFileSize(), fd.getFileName(), fd.getUrns(), null, new GGEPContainer( getAsEndpoints(RouterService.getAltlocManager().getDirect(fd.getSHA1Urn())), CreationTimeCache.instance().getCreationTimeAsLong(fd.getSHA1Urn())), null); }