Java Code Examples for com.facebook.common.file.FileUtils

The following examples show how to use com.facebook.common.file.FileUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 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);
    }
  }
}
 
Example 2
Source Project: fresco   Source File: DefaultDiskStorage.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
    }
  }
}
 
Example 3
/**
 * 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;
  }
}
 
Example 4
@Override
public FileBinaryResource commit(String resourceId, BinaryResource temporary, Object debugInfo)
    throws IOException {
  // will cause a class-cast exception
  FileBinaryResource tempFileResource = (FileBinaryResource) temporary;

  File tempFile = tempFileResource.getFile();
  File targetFile = getContentFileFor(resourceId);

  try {
    FileUtils.rename(tempFile, 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);
}
 
Example 5
@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());
}
 
Example 6
Source Project: fresco   Source File: DynamicDefaultDiskStorage.java    License: MIT License 5 votes vote down vote up
@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());
}
 
Example 7
Source Project: fresco   Source File: DefaultDiskStorage.java    License: MIT License 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example 8
Source Project: fresco   Source File: DefaultDiskStorage.java    License: MIT License 5 votes vote down vote up
@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);
}