Beispiel #1
0
  /**
   * 删除目录(包括:目录里的所有文件)
   *
   * @param fileName
   * @return
   */
  public static boolean deleteDirectory(String fileName) {
    boolean status;
    SecurityManager checker = new SecurityManager();

    if (!fileName.equals("")) {

      // File path = Environment.getExternalStorageDirectory();
      File newPath = new File(fileName);
      checker.checkDelete(newPath.toString());
      if (newPath.isDirectory()) {
        String[] listfile = newPath.list();
        // delete all files within the specified directory and then
        // delete the directory
        try {
          for (int i = 0; i < listfile.length; i++) {
            File deletedFile = new File(newPath.toString() + "/" + listfile[i].toString());
            deletedFile.delete();
          }
          newPath.delete();
          Log.i(TAG, "DirectoryManager deleteDirectory:" + fileName);
          status = true;
        } catch (Exception e) {
          e.printStackTrace();
          status = false;
        }

      } else status = false;
    } else status = false;
    return status;
  }
Beispiel #2
0
 /**
  * 删除文件
  *
  * @param filePath
  */
 public static boolean deleteFileWithPath(String filePath) {
   SecurityManager checker = new SecurityManager();
   File f = new File(filePath);
   checker.checkDelete(filePath);
   if (f.isFile()) {
     Log.d(TAG, "DirectoryManager deleteFile " + filePath);
     f.delete();
     return true;
   }
   return false;
 }
Beispiel #3
0
 /**
  * 删除文件
  *
  * @param fileName
  * @return
  */
 public static boolean deleteFile(String fileName) {
   boolean status;
   SecurityManager checker = new SecurityManager();
   if (!fileName.equals("")) {
     // File path = Environment.getExternalStorageDirectory();
     File newPath = new File(fileName);
     checker.checkDelete(newPath.toString());
     if (newPath.isFile()) {
       try {
         Log.i(TAG, "DirectoryManager deleteDirectory:" + fileName);
         newPath.delete();
         status = true;
       } catch (SecurityException se) {
         se.printStackTrace();
         status = false;
       }
     } else status = false;
   } else status = false;
   return status;
 }
Beispiel #4
0
  /**
   * 删除文件
   *
   * @param fileName 全路径
   * @return
   */
  public static boolean deleteFileWithPath(String fileName) {
    boolean status;
    SecurityManager checker = new SecurityManager();

    if (!TextUtils.isEmpty(fileName)) {

      File newPath = new File(fileName);
      checker.checkDelete(newPath.toString());
      if (newPath.isFile()) {
        try {
          Log.i("DirectoryManager deleteFile", fileName);
          newPath.delete();
          status = true;
        } catch (SecurityException se) {
          se.printStackTrace();
          status = false;
        }
      } else status = false;
    } else status = false;
    return status;
  }
  /**
   * Opens file based on parameters and options, returning a FileDescriptor encapsulating the handle
   * to the open file.
   */
  private static FileDescriptor open(
      String pathForWindows, String pathToCheck, Flags flags, long pSecurityDescriptor)
      throws WindowsException {
    // set to true if file must be truncated after open
    boolean truncateAfterOpen = false;

    // map options
    int dwDesiredAccess = 0;
    if (flags.read) dwDesiredAccess |= GENERIC_READ;
    if (flags.write) dwDesiredAccess |= GENERIC_WRITE;

    int dwShareMode = 0;
    if (flags.shareRead) dwShareMode |= FILE_SHARE_READ;
    if (flags.shareWrite) dwShareMode |= FILE_SHARE_WRITE;
    if (flags.shareDelete) dwShareMode |= FILE_SHARE_DELETE;

    int dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
    int dwCreationDisposition = OPEN_EXISTING;
    if (flags.write) {
      if (flags.createNew) {
        dwCreationDisposition = CREATE_NEW;
        // force create to fail if file is orphaned reparse point
        dwFlagsAndAttributes |= FILE_FLAG_OPEN_REPARSE_POINT;
      } else {
        if (flags.create) dwCreationDisposition = OPEN_ALWAYS;
        if (flags.truncateExisting) {
          // Windows doesn't have a creation disposition that exactly
          // corresponds to CREATE + TRUNCATE_EXISTING so we use
          // the OPEN_ALWAYS mode and then truncate the file.
          if (dwCreationDisposition == OPEN_ALWAYS) {
            truncateAfterOpen = true;
          } else {
            dwCreationDisposition = TRUNCATE_EXISTING;
          }
        }
      }
    }

    if (flags.dsync || flags.sync) dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
    if (flags.overlapped) dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
    if (flags.deleteOnClose) dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;

    // NOFOLLOW_LINKS and NOFOLLOW_REPARSEPOINT mean open reparse point
    boolean okayToFollowLinks = true;
    if (dwCreationDisposition != CREATE_NEW
        && (flags.noFollowLinks || flags.openReparsePoint || flags.deleteOnClose)) {
      if (flags.noFollowLinks || flags.deleteOnClose) okayToFollowLinks = false;
      dwFlagsAndAttributes |= FILE_FLAG_OPEN_REPARSE_POINT;
    }

    // permission check
    if (pathToCheck != null) {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null) {
        if (flags.read) sm.checkRead(pathToCheck);
        if (flags.write) sm.checkWrite(pathToCheck);
        if (flags.deleteOnClose) sm.checkDelete(pathToCheck);
      }
    }

    // open file
    long handle =
        CreateFile(
            pathForWindows,
            dwDesiredAccess,
            dwShareMode,
            pSecurityDescriptor,
            dwCreationDisposition,
            dwFlagsAndAttributes);

    // make sure this isn't a symbolic link.
    if (!okayToFollowLinks) {
      try {
        if (WindowsFileAttributes.readAttributes(handle).isSymbolicLink())
          throw new WindowsException("File is symbolic link");
      } catch (WindowsException x) {
        CloseHandle(handle);
        throw x;
      }
    }

    // truncate file (for CREATE + TRUNCATE_EXISTING case)
    if (truncateAfterOpen) {
      try {
        SetEndOfFile(handle);
      } catch (WindowsException x) {
        CloseHandle(handle);
        throw x;
      }
    }

    // make the file sparse if needed
    if (dwCreationDisposition == CREATE_NEW && flags.sparse) {
      try {
        DeviceIoControlSetSparse(handle);
      } catch (WindowsException x) {
        // ignore as sparse option is hint
      }
    }

    // create FileDescriptor and return
    FileDescriptor fdObj = new FileDescriptor();
    fdAccess.setHandle(fdObj, handle);
    return fdObj;
  }
 @Override
 public void checkDelete(final String pFile) {
   if (finalSecurityManager != null) finalSecurityManager.checkDelete(pFile);
 }
 @Override
 public void checkDelete(final String file) {
   if (securityManager != null) securityManager.checkDelete(file);
 }