Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] temp file not being deleted when building #2739

Closed
MFinkBK opened this issue Jan 13, 2022 · 8 comments · Fixed by #2742 or #2745
Closed

[BUG] temp file not being deleted when building #2739

MFinkBK opened this issue Jan 13, 2022 · 8 comments · Fixed by #2742 or #2745

Comments

@MFinkBK
Copy link

MFinkBK commented Jan 13, 2022

Information

  1. Apktool Version (apktool -version) - 2.6.0
  2. Operating System (Mac, Linux, Windows) - Windows
  3. APK From? (Playstore, ROM, Other) - probably not relevant

Steps to Reproduce

  1. apktool b -o out extractedFolder
  2. Monitor the %TEMP% folder; during build, two APKTOOL{longnumber}.tmp files are created, and one of the is removed again. The other stays there.

Note that this only occurs when (re-)building, not when decompiling an .apk. After digging a bit in the source, I suspect the following lines in buildManifest() to be the problem:

File apkFile = File.createTempFile("APKTOOL", null);
apkFile.delete();

Here, the temporary file is being created and then deleted again (I guess to get the temp filename). Then the filename is used in a call to aaptPackage(), but afterwards it isn't deleted.

Note that buildResourcesFull() also creates a temp filename, but deletes the created file again in line 512, and that's the difference to buildManifest():

@iBotPeaches
Copy link
Owner

Thanks for the research, I'll use this research and dig around in a bit and see what I can find.

@iBotPeaches
Copy link
Owner

Thanks, found em. PR is up.

@MFinkBK
Copy link
Author

MFinkBK commented Jan 17, 2022

I just tested with the apktool .jar file built from the GitHub Action (the build artifact .jar named apktool-2.6.1-ddc4bb-SNAPSHOT-small.jar, from the run https://github.com/iBotPeaches/Apktool/actions/runs/1707904289). Unfortunately the code change doesn't fix the bug (yet). I have no skills in Java debugging, but I checked out what's logged in SysInternal's Process Monitor tool when a build runs. I attached the CSV export of the relevant log lines, here: apktool-procmon-logfile.csv

The APKTOOL.tmp file that isn't being removed is opened several times using Win32 API CreateFile(), which it seems is used for reading, writing, getting attributes from a file and even for deleting. The last CreateFile() call that should remove the file gets a Sharing Violation, so my guess is that some object still has a handle on the file. And there's a CreateFile call that isn't matched with a CloseFile call that may be the reason. From trying to match the calls to the code in buildResourcesFull(), my guess is that it's the call to new ExtFile() in this line that keeps the temp file open:

Directory tmpDir = new ExtFile(apkFile).getDirectory();

(I guess the GC at some point collects the object and closes the file, but the apkFile.delete() call happens earlier than that.

@iBotPeaches
Copy link
Owner

:(

Thanks will reopen.

@iBotPeaches iBotPeaches reopened this Jan 17, 2022
@MFinkBK
Copy link
Author

MFinkBK commented Jan 17, 2022

My guess is that something like that would help:

ExtFile apkFileExt = new ExtFile(apkFile);
Directory tmpDir = apkFileExt.getDirectory();
apkFileExt.close();

I'm not quite sure if there's another way to do that in Java.

@iBotPeaches
Copy link
Owner

Okay, next PR has to be perfect. I've done a looped build/decode of 10 applications 20 times and I have no files in /tmp. Additionally, scripted the actual building of Apktool 20 times and no /tmp files.

@MFinkBK
Copy link
Author

MFinkBK commented Jan 18, 2022

I'd like to test the PR, but unfortunately the GitHub action doesn't provide a built apktool.jar, only when merged to master...

@MFinkBK
Copy link
Author

MFinkBK commented Jan 18, 2022

Works! I tested with apktool-2.6.1-797fc3-SNAPSHOT-small.jar. Thanks for the effort to fix this bug, and for making apktool in the first place!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants