/** * Checks if we have to recreate rootDirectory. * This is needed because old versions of this storage created too much different files * in the same dir, and Samsung's RFS has a bug that after the 13.000th creation fails. * So if cache is not already in expected version let's destroy everything * (if not in expected version... there's nothing to reuse here anyway). */ private void recreateDirectoryIfVersionChanges() { boolean recreateBase = false; if (!mRootDirectory.exists()) { recreateBase = true; } else if (!mVersionDirectory.exists()) { recreateBase = true; FileTree.deleteRecursively(mRootDirectory); } if (recreateBase) { try { FileUtils.mkdirs(mVersionDirectory); } catch (FileUtils.CreateDirectoryException e) { // not the end of the world, when saving files we will try to create missing parent dirs mCacheErrorLogger.logError( CacheErrorLogger.CacheErrorCategory.WRITE_CREATE_DIR, TAG, "version directory could not be created: " + mVersionDirectory, null); } } }
@VisibleForTesting void createRootDirectoryIfNecessary(File rootDirectory) throws IOException { try { FileUtils.mkdirs(rootDirectory); } catch (FileUtils.CreateDirectoryException cde) { mCacheErrorLogger.logError( CacheErrorLogger.CacheErrorCategory.WRITE_CREATE_DIR, TAG, "createRootDirectoryIfNecessary", cde); throw cde; } FLog.d(TAG, "Created cache directory %s", rootDirectory.getAbsolutePath()); }
/** * Creates the directory (and its parents, if necessary). * In case of an exception, log an error message with the relevant parameters * @param directory the directory to create * @param message message to use * @throws IOException */ private void mkdirs(File directory, String message) throws IOException { try { FileUtils.mkdirs(directory); } catch (FileUtils.CreateDirectoryException cde) { mCacheErrorLogger.logError( CacheErrorLogger.CacheErrorCategory.WRITE_CREATE_DIR, TAG, message, cde); throw cde; } }
@Override public BinaryResource commit(Object debugInfo) throws IOException { // the temp resource must be ours! File targetFile = getContentFileFor(mResourceId); try { FileUtils.rename(mTemporaryFile, targetFile); } catch (FileUtils.RenameException re) { CacheErrorLogger.CacheErrorCategory category; Throwable cause = re.getCause(); if (cause == null) { category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_OTHER; } else if (cause instanceof FileUtils.ParentDirNotFoundException) { category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_TEMPFILE_PARENT_NOT_FOUND; } else if (cause instanceof FileNotFoundException) { category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_TEMPFILE_NOT_FOUND; } else { category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_OTHER; } mCacheErrorLogger.logError( category, TAG, "commit", re); throw re; } if (targetFile.exists()) { targetFile.setLastModified(mClock.now()); } return FileBinaryResource.createOrNull(targetFile); }