Restore file truncation where expected.
Several years ago ParcelFileDescriptor.parseMode() was fixed to match the behavior of fopen(), since developers expect consistent behavior between managed and native code. FileUtilsTest.testTranslateMode() verifies that all these modes are correctly translated. However, this unintentionally changed the behavior of ContentResolver.openOutputStream(), which only sends the 'w' mode to the remote process. Developers expect this API to behave like the FileOutputStream constructor, which always truncates the file unless opened with the append mode. Since some remote providers may not be prepared to handle the 't' mode, this change carefully uses Os.ftruncate() to restore this expected behavior in all cases. For other APIs that return opened files, this strategy is applied to restore the original behavior, but only when the target SDK of the app is expecting this truncation to take place. The reason for this is that moving forward our goal should always enable ContentInterface APIs to be a transparent conversation between apps without attempting to alter the behavior. Apps talking with older providers can apply the Os.ftruncate() logic themselves, if desired, once they target Android Q or higher. Bug: 157888856, 180680924 Test: atest CtsContentTestCases:ContentResolverTest Change-Id: Iacec49164c4ce3891db0270635e9f458dea7becd
Loading
Please register or sign in to comment