Java Code Examples for android.system.Os#chmod()

The following examples show how to use android.system.Os#chmod() . 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
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Common-path handling of app data dir creation
 */
private static File ensurePrivateDirExists(File file) {
    if (!file.exists()) {
        try {
            Os.mkdir(file.getAbsolutePath(), 0771);
            Os.chmod(file.getAbsolutePath(), 0771);
        } catch (ErrnoException e) {
            if (e.errno == OsConstants.EEXIST) {
                // We must have raced with someone; that's okay
            } else {
                Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
            }
        }
    }
    return file;
}
 
Example 2
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Common-path handling of app data dir creation
 */
private static File ensurePrivateDirExists(File file) {
    if (!file.exists()) {
        try {
            Os.mkdir(file.getAbsolutePath(), 0771);
            Os.chmod(file.getAbsolutePath(), 0771);
        } catch (ErrnoException e) {
            if (e.errno == OsConstants.EEXIST) {
                // We must have raced with someone; that's okay
            } else {
                Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
            }
        }
    }
    return file;
}
 
Example 3
Source File: PackageInstallerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
static void prepareStageDir(File stageDir) throws IOException {
    if (stageDir.exists()) {
        throw new IOException("Session dir already exists: " + stageDir);
    }

    try {
        Os.mkdir(stageDir.getAbsolutePath(), 0755);
        Os.chmod(stageDir.getAbsolutePath(), 0755);
    } catch (ErrnoException e) {
        // This purposefully throws if directory already exists
        throw new IOException("Failed to prepare session dir: " + stageDir, e);
    }

    if (!SELinux.restorecon(stageDir)) {
        throw new IOException("Failed to restorecon session dir: " + stageDir);
    }
}
 
Example 4
Source File: PackageManagerServiceUtils.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public static int decompressFile(File srcFile, File dstFile) throws ErrnoException {
    if (DEBUG_COMPRESSION) {
        Slog.i(TAG, "Decompress file"
                + "; src: " + srcFile.getAbsolutePath()
                + ", dst: " + dstFile.getAbsolutePath());
    }
    try (
            InputStream fileIn = new GZIPInputStream(new FileInputStream(srcFile));
            OutputStream fileOut = new FileOutputStream(dstFile, false /*append*/);
    ) {
        FileUtils.copy(fileIn, fileOut);
        Os.chmod(dstFile.getAbsolutePath(), 0644);
        return PackageManager.INSTALL_SUCCEEDED;
    } catch (IOException e) {
        logCriticalInfo(Log.ERROR, "Failed to decompress file"
                + "; src: " + srcFile.getAbsolutePath()
                + ", dst: " + dstFile.getAbsolutePath());
    }
    return PackageManager.INSTALL_FAILED_INTERNAL_ERROR;
}
 
Example 5
Source File: PackageInstallerSession.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void createRemoveSplitMarkerLocked(String splitName) throws IOException {
    try {
        final String markerName = splitName + REMOVE_SPLIT_MARKER_EXTENSION;
        if (!FileUtils.isValidExtFilename(markerName)) {
            throw new IllegalArgumentException("Invalid marker: " + markerName);
        }
        final File target = new File(resolveStageDirLocked(), markerName);
        target.createNewFile();
        Os.chmod(target.getAbsolutePath(), 0 /*mode*/);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
 
Example 6
Source File: PackageInstallerSession.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static void copyFiles(List<File> fromFiles, File toDir) throws IOException {
    // Remove any partial files from previous attempt
    for (File file : toDir.listFiles()) {
        if (file.getName().endsWith(".tmp")) {
            file.delete();
        }
    }

    for (File fromFile : fromFiles) {
        final File tmpFile = File.createTempFile("inherit", ".tmp", toDir);
        if (LOGD) Slog.d(TAG, "Copying " + fromFile + " to " + tmpFile);
        if (!FileUtils.copyFile(fromFile, tmpFile)) {
            throw new IOException("Failed to copy " + fromFile + " to " + tmpFile);
        }
        try {
            Os.chmod(tmpFile.getAbsolutePath(), 0644);
        } catch (ErrnoException e) {
            throw new IOException("Failed to chmod " + tmpFile);
        }
        final File toFile = new File(toDir, fromFile.getName());
        if (LOGD) Slog.d(TAG, "Renaming " + tmpFile + " to " + toFile);
        if (!tmpFile.renameTo(toFile)) {
            throw new IOException("Failed to rename " + tmpFile + " to " + toFile);
        }
    }
    Slog.d(TAG, "Copied " + fromFiles.size() + " files into " + toDir);
}
 
Example 7
Source File: FileUtils.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public static void copyPermissions(File from, File to) throws IOException {
    try {
        final StructStat stat = Os.stat(from.getAbsolutePath());
        Os.chmod(to.getAbsolutePath(), stat.st_mode);
        Os.chown(to.getAbsolutePath(), stat.st_uid, stat.st_gid);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
 
Example 8
Source File: PathUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressLint("NewApi")
private static void chmod(String path, int mode) {
    // Both Os.chmod and ErrnoException require SDK >= 21. But while Dalvik on < 21 tolerates
    // Os.chmod, it throws VerifyError for ErrnoException, so catch Exception instead.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
    try {
        Os.chmod(path, mode);
    } catch (Exception e) {
        Log.e(TAG, "Failed to set permissions for path \"" + path + "\"");
    }
}
 
Example 9
Source File: PackageInstallerSession.java    From container with GNU General Public License v3.0 5 votes vote down vote up
private void createRemoveSplitMarker(String splitName) throws IOException {
    try {
        final String markerName = splitName + REMOVE_SPLIT_MARKER_EXTENSION;
        if (!FileUtils.isValidExtFilename(markerName)) {
            throw new IllegalArgumentException("Invalid marker: " + markerName);
        }
        final File target = new File(resolveStageDir(), markerName);
        target.createNewFile();
        Os.chmod(target.getAbsolutePath(), 0 /*mode*/);
    } catch (ErrnoException e) {
        throw new IOException(e);
    }
}
 
Example 10
Source File: FLog.java    From AppOpsX with MIT License 5 votes vote down vote up
private static void chmod(String path,int mode){
  try {
    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
      Os.chmod(path, mode);
    }else {
      Runtime.getRuntime().exec("chmod "+mode+" "+path).destroy();
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 11
Source File: PackageManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private int runSnapshotProfile() throws RemoteException {
    PrintWriter pw = getOutPrintWriter();

    // Parse the arguments
    final String packageName = getNextArg();
    final boolean isBootImage = "android".equals(packageName);

    String codePath = null;
    String opt;
    while ((opt = getNextArg()) != null) {
        switch (opt) {
            case "--code-path":
                if (isBootImage) {
                    pw.write("--code-path cannot be used for the boot image.");
                    return -1;
                }
                codePath = getNextArg();
                break;
            default:
                pw.write("Unknown arg: " + opt);
                return -1;
        }
    }

    // If no code path was explicitly requested, select the base code path.
    String baseCodePath = null;
    if (!isBootImage) {
        PackageInfo packageInfo = mInterface.getPackageInfo(packageName, /* flags */ 0,
                /* userId */0);
        if (packageInfo == null) {
            pw.write("Package not found " + packageName);
            return -1;
        }
        baseCodePath = packageInfo.applicationInfo.getBaseCodePath();
        if (codePath == null) {
            codePath = baseCodePath;
        }
    }

    // Create the profile snapshot.
    final SnapshotRuntimeProfileCallback callback = new SnapshotRuntimeProfileCallback();
    // The calling package is needed to debug permission access.
    final String callingPackage = (Binder.getCallingUid() == Process.ROOT_UID)
            ? "root" : "com.android.shell";
    final int profileType = isBootImage
            ? ArtManager.PROFILE_BOOT_IMAGE : ArtManager.PROFILE_APPS;
    if (!mInterface.getArtManager().isRuntimeProfilingEnabled(profileType, callingPackage)) {
        pw.println("Error: Runtime profiling is not enabled");
        return -1;
    }
    mInterface.getArtManager().snapshotRuntimeProfile(profileType, packageName,
            codePath, callback, callingPackage);
    if (!callback.waitTillDone()) {
        pw.println("Error: callback not called");
        return callback.mErrCode;
    }

    // Copy the snapshot profile to the output profile file.
    try (InputStream inStream = new AutoCloseInputStream(callback.mProfileReadFd)) {
        final String outputFileSuffix = isBootImage || Objects.equals(baseCodePath, codePath)
                ? "" : ("-" + new File(codePath).getName());
        final String outputProfilePath =
                ART_PROFILE_SNAPSHOT_DEBUG_LOCATION + packageName + outputFileSuffix + ".prof";
        try (OutputStream outStream = new FileOutputStream(outputProfilePath)) {
            Streams.copy(inStream, outStream);
        }
        // Give read permissions to the other group.
        Os.chmod(outputProfilePath, /*mode*/ 0644 );
    } catch (IOException | ErrnoException e) {
        pw.println("Error when reading the profile fd: " + e.getMessage());
        e.printStackTrace(pw);
        return -1;
    }
    return 0;
}