如何将文件保存到Android /system目录?

发布于 2024-12-25 06:12:18 字数 456 浏览 2 评论 0原文

我正在编写一个应用程序,它将文件从其资产目录复制到 Android 的 /system 目录。

我觉得我已经接近实现这一目标,但还没有完全实现。这是我尝试过的策略:

首先,我尝试通过以下方式更改 /system 目录的权限:

Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("mount -o rw,remount -t yaffs2 /dev/block/mtdblock4");
Runtime.getRuntime().exec("chmod -R 777 /system");

其次,我尝试将文件写入 /system 目录。

我读到通过重新安装目录,我可以向其中写入文件。事实上,这似乎适用于终端。但在应用程序中,我无法弄清楚如何确保每个命令在执行下一个命令之前完成。

有人有什么建议吗?

I am writing an application that will copy files from its assets directory to the Android's /system directory.

I feel that I am getting close to achieving this, but I am not quite there. Here is the strategy that I have attempted:

First, I tried to change the permissions of the /system directory by:

Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("mount -o rw,remount -t yaffs2 /dev/block/mtdblock4");
Runtime.getRuntime().exec("chmod -R 777 /system");

Second, I tried to write a file to the /system directory.

I read that by remounting the directory, I could write files to it. Indeed, this seemed to work with a terminal. But in the application, I couldn't figure out how to make sure each command was completed before execution of the next one.

Does anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开柳相依 2025-01-01 06:12:18
Runtime.getRuntime().exec

会返回一个Process,从process类中,你可以

getInputStream  or getOutputStream or getErrorStream

从这些方法中,你可以获得命令执行结果,你可以检查它以确保命令
执行正确或错误。

Runtime.getRuntime().exec

will return a Process, from the process class, you can

getInputStream  or getOutputStream or getErrorStream

from these method, you can get the command execute result, you can check it to ensure command
execute right or wrong.

猫卆 2025-01-01 06:12:18

我假设 exec() 方法有一个返回值,您应该检查该返回值以确定执行是否正常。

但我认为您在尝试执行这一系列命令时犯了几个错误:

  • su 只能影响它自己的进程和子进程。您执行它时不带任何参数,因此它什么也不做。下一个 exec() 方法从头开始,具有进程的确切权限,而不是您可能期望的提升权限。
  • 您的 mount(8) 命令似乎不正确:您没有说明要操作哪个安装点。 (不要忘记,一个设备可能会安装在多个位置。)
  • 您的 chmod(1) 调用也不会以特权运行,并且将权限设置得过于开放。也许您不关心设备的安全性,这很好,但请确保这就是您想要的。

我相信您的 mount 命令应该看起来更像:

Runtime.getRuntime().exec("su -c \"mount -orw,remount /system\"");

我相信您的 chmod 命令应该看起来更像:

Runtime.getRuntime().exec("su -c \"chmod 777 /system/fonts\"");

再次,请确保您了解允许系统上所有用户的后果写入其他用户可以执行的任何文件的权限。

I assume there is a return value from the exec() method that you should be checking to see if the execution worked fine.

But I think you've got several mistakes in your attempts to execute this series of commands:

  • su can only influence its own process and child processes. You execute it with no arguments, so it does nothing. The next exec() method starts over from scratch with the exact permissions of your process, not the elevated privileges you might expect.
  • Your mount(8) command doesn't seem right: you're not saying which mountpoint to manipulate. (Don't forget that a device may be mounted in multiple locations.)
  • Your chmod(1) call also doesn't run with privileges and sets permissions way too open. Perhaps you don't care about security on your device, that's fine, but be certain that that is what you want.

I believe your mount command should look more like:

Runtime.getRuntime().exec("su -c \"mount -orw,remount /system\"");

And I believe your chmod command should look more like:

Runtime.getRuntime().exec("su -c \"chmod 777 /system/fonts\"");

Again, be certain you understand the consequences of allowing all users on the system privileges to write to any file that other users can execute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文