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

New, overhauled data storage management #1954

Merged
merged 11 commits into from
Jan 15, 2018
Merged

Conversation

mario
Copy link
Contributor

@mario mario commented Jan 11, 2018

Improved & rebased version of the awesome community PR found here:
#1836

We don't do files migration automatically yet, I'd suggest we implement that for 2.2 since it needs some UI as well, but push keys are properly migrated if they exist.

@mario mario added this to the Nextcloud App 2.1.0 milestone Jan 11, 2018

String keyPath = getAppContext().getFilesDir().getAbsolutePath() +
File.separator + MainApp.getDataFolder() + File.separator + "nc-keypair";
File privateKey = new File(keyPath + File.separator + "push_key.priv");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equivalent of: File privateKey = new File(keyPath, "push_key.priv");
same for the line up and down

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be so kind to improve in the branch directly all things you think need improving? :)

File publicKey = new File(keyPath + File.separator + "push_key.pub");

if (oldPrivateKey.exists() && oldPublicKey.exists()) {
if (oldPrivateKey.renameTo(privateKey) && oldPublicKey.renameTo(publicKey)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't handle the scenario when first rename succeeds and other fails.

}
// Add private internal storage data directory.
result.add(new StoragePoint(defaultStringDesc,
MainApp.getAppContext().getFilesDir().getAbsolutePath()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of this storage point using context. IMO there should be a method in MainApp which directly returns the files dir and use that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see we use this at a couple other places, so not worried about this much.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would indeed be better to centralize this, but I agree with @mario that this does not need to be done now.

@mario
Copy link
Contributor Author

mario commented Jan 11, 2018 via email

@mario
Copy link
Contributor Author

mario commented Jan 11, 2018

Fixed everything @przybylski except for the context thing which I'm unsure if we should fix.

@mario mario force-pushed the new-ardevd-storage branch from 1d879ee to 10923fb Compare January 11, 2018 23:57
@@ -286,6 +287,11 @@ public static boolean getLegacyClean(Context context) {
return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
}

public static boolean getKeysMigration(Context context) {
return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_MIGRATION, false);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we wanted to start using ArbitraryDataProvider for stuff like this instead of shared preferences?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but later. Not less than a day before RC :P

PreferenceManager.setKeysMigration(context, true);
} else {
if (oldPrivateKey.exists()) {
oldPrivateKey.renameTo(privateKey);
Copy link
Member

@tobiasKaminsky tobiasKaminsky Jan 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is renameTo working if used across filesystem boundary, e.g. from external sdcard to internal storage? As far as I remember this does not work, but we have to copy and delete instead.

Edit: doc says:

Both paths be on the same mount point. On Android, applications are most likely to hit this restriction when attempting to copy between internal storage and an SD card.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will investigate.

oldPublicKey.renameTo(publicKey);
}

if (privateKey.exists() && publicKey.exists()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check will always be true as those files are created at Line 245/246, or? A more sane check would be if length > 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not always. Maybe just one file was created :)


// Add external storage directory if available.
if (isExternalStorageWritable()) {
mCachedStoragePoints.add(new StoragePoint(MainApp.getAppContext().getExternalFilesDir(null).getAbsolutePath(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"getExternalFilesDir(null)" can be null:

May return null if shared storage is not currently available.

So this should be checked, otherwise we get a NPE

selection = "(?)";
selectionArgs = new String[]{selection};
Copy link
Member

@tobiasKaminsky tobiasKaminsky Jan 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done to prevent sql injections: instead of querying the args directly those are inserted via "(?):

select * where test = " + $foo; (foo can be dangerous)
becomes to
"select * where test = (?)", $foo --> then we have use of built in sql prevention.

Changing this, would mean that selectionArgs is always "(?)" which is not what we want to have: if args is null, then we want to "force" selection to use "(?)":
selection: "1=1"
--> select * from xy where "1=1"
becomes
--> select * from xy where (?), "1" and this sql injection save

So this must be reverted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then the Uploads view crashes for me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When? With which uploads? Do you have a stacktrace?
This is for quite a time in dev version and I never experienced a crash on upload view (except of the TimSort exception).
Or it is a new thing with this branch...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, was a problem with master. The Uploads view. But no stacktrace :(

Copy link
Member

@tobiasKaminsky tobiasKaminsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sql prevention stuff needs to be changed/discussed.

@mario mario force-pushed the new-ardevd-storage branch from 19e8b0b to a71e3aa Compare January 12, 2018 18:11
@tobiasKaminsky
Copy link
Member

There is another severe bug:

  • fresh installation
  • open auto upload and enable any folder
  • close auto upload view
  • re-open: folder is not enabled and also upload does not work

(works on master)

@mario
Copy link
Contributor Author

mario commented Jan 12, 2018

I will definitely take a look, but what about the Uploads? That is crashing for me (sometimes) on master, and my change fixes it.

@nextcloud nextcloud deleted a comment Jan 12, 2018
@tobiasKaminsky
Copy link
Member

As said, give me a stacktrace/way to reproduce.
I think that the fix reverts sql injection protection...

@mario
Copy link
Contributor Author

mario commented Jan 12, 2018

@tobiasKaminsky ok, will revert the fix for now and we'll see in RC :)

@mario
Copy link
Contributor Author

mario commented Jan 12, 2018

@tobiasKaminsky just tried, works. so this PR is ready.

@nextcloud nextcloud deleted a comment Jan 12, 2018
@tobiasKaminsky
Copy link
Member

Will test it with real device with external storage on monday.

@nextcloud nextcloud deleted a comment Jan 13, 2018
mario added 5 commits January 15, 2018 11:23
Signed-off-by: Mario Danic <[email protected]>
Signed-off-by: Mario Danic <[email protected]>
Signed-off-by: Mario Danic <[email protected]>
Signed-off-by: Mario Danic <[email protected]>
Signed-off-by: Mario Danic <[email protected]>
tobiasKaminsky and others added 4 commits January 15, 2018 11:23
Signed-off-by: tobiaskaminsky <[email protected]>
Signed-off-by: Mario Danic <[email protected]>
Signed-off-by: Mario Danic <[email protected]>
Signed-off-by: Mario Danic <[email protected]>
@@ -236,6 +243,53 @@ public static void initAutoUpload() {
}
}

public static void migratePushKeys() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only use in PushUtils on gplay flavor, so this can be moved there, or?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. :) Or just leave it here and don't worry :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be moved to not have the dependency in the f-droid build where this might be an issue then, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it already :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be a problem because it's just moving files, no Google Play deps 📦

@mario
Copy link
Contributor Author

mario commented Jan 15, 2018

👍

@nextcloud nextcloud deleted a comment Jan 15, 2018
@nextcloud nextcloud deleted a comment Jan 15, 2018
@AndyScherzinger AndyScherzinger changed the title New ardevd storage New, overhauled data storage management Jan 15, 2018
@nextcloud nextcloud deleted a comment Jan 15, 2018
@AndyScherzinger
Copy link
Member

AndyScherzinger commented Jan 15, 2018

👍 only one minor comment about the f-droid build and the PushUtils dependency: #1954 (comment)

@tobiasKaminsky
Copy link
Member

Welcome back @AndyScherzinger 🎉

@@ -335,4 +336,44 @@ private static int saveKeyToFile(Key key, String path) {

return -1;
}

public static void migratePushKeys() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be private void migratePushKeys() then :P

@@ -99,8 +99,9 @@ public static String bytesToHex(byte[] bytes) {
}

private static int generateRsa2048KeyPair() {
String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator
+ KEYPAIR_FOLDER;
MainApp.migratePushKeys();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this won't work if you remove it from the MainApp :P

fix modified, but will soon be removed

Signed-off-by: tobiasKaminsky <[email protected]>
@nextcloud nextcloud deleted a comment Jan 15, 2018
@nextcloud nextcloud deleted a comment Jan 15, 2018
@tobiasKaminsky tobiasKaminsky merged commit 4394320 into master Jan 15, 2018
@AndyScherzinger AndyScherzinger deleted the new-ardevd-storage branch January 15, 2018 13:09
This was referenced Jan 15, 2018
This was referenced Jan 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants