11package top .trumeet .mipush .settings .ini ;
22
3+ import android .annotation .SuppressLint ;
34import android .content .Context ;
5+ import android .os .FileUtils ;
46import android .util .Log ;
57
68import androidx .annotation .NonNull ;
1820import java .util .Map ;
1921import java .util .Objects ;
2022
23+ import static top .trumeet .mipush .settings .ini .IniConstants .CONF_FILE ;
24+ import static top .trumeet .mipush .settings .ini .IniConstants .CONF_PATH ;
2125import static top .trumeet .mipush .settings .ini .IniConstants .INI_DEFAULT ;
2226import static top .trumeet .mipush .settings .ini .IniConstants .TAG ;
2327import static top .trumeet .mipush .settings .ini .IniConstants .rwxrwxrwx ;
@@ -52,19 +56,28 @@ public class IniConf {
5256 * If the file is absent, it will be created.
5357 * If the file is null, write() will do nothing.
5458 */
55- public IniConf (@ Nullable final File file ) throws IOException {
56- if (file != null ) {
57- setFilePermissionsFromMode (file , rwxrwxrwx );
59+ @ SuppressWarnings ({"ResultOfMethodCallIgnored" })
60+ public IniConf (final File file ) throws IOException {
61+ final File conf_path = new File (file , CONF_PATH );
62+ final File conf_file = new File (conf_path , CONF_FILE );
63+
64+ FileUtils .setPermissions (file .getAbsolutePath () + "/" , rwxrwxrwx , -1 , -1 );
65+
66+ if (!conf_path .exists ()) {
67+ conf_path .mkdir ();
5868 }
59- mIni = createDefaultConfig (file );
69+
70+ FileUtils .setPermissions (conf_path .getAbsolutePath () + "/" , rwxrwxrwx , -1 , -1 );
71+
72+ mIni = createDefaultConfig (conf_file );
6073 // TODO: File locking?
6174 }
6275
6376 /**
6477 * Construct using the default path
6578 */
66- public IniConf (Context context ) throws IOException {
67- this (IniUtils .getConfigPath (context ));
79+ public IniConf (int userId ) throws IOException {
80+ this (IniUtils .getConfigPath (userId ));
6881 }
6982
7083 /**
@@ -74,15 +87,13 @@ public IniConf(Context context) throws IOException {
7487 * @return The upgraded ini. It will not be stored in file system.
7588 * @throws IOException In case there's errors.
7689 */
77- private Ini createDefaultConfig (@ Nullable final File file ) throws IOException {
90+ private Ini createDefaultConfig (final File file ) throws IOException {
7891 final Ini defaultIni = new Ini ();
7992 defaultIni .load (new ByteArrayInputStream (INI_DEFAULT .getBytes (StandardCharsets .UTF_8 )));
8093 final Ini currentIni = new Ini ();
81- if (file != null ) {
82- currentIni .setFile (file );
83- if (file .exists ()) {
84- currentIni .load (file );
85- }
94+ currentIni .setFile (file );
95+ if (file .exists ()) {
96+ currentIni .load (file );
8697 }
8798 // Instead of just copying, we compare and proceed the diffs.
8899 // Check for absent options to be added.
@@ -139,19 +150,21 @@ public Map<IniKey, Object> getAll() {
139150 /**
140151 * Write all changes to the file system. If the file is absent, it will be created.
141152 */
142- @ SuppressWarnings ("ResultOfMethodCallIgnored" )
153+ @ SuppressLint ("WorldReadableFiles" )
154+ @ SuppressWarnings ({"ResultOfMethodCallIgnored" , "deprecation" })
143155 public void write () throws IOException {
144156// Log.d(TAG, "Saving configuration");
145157 if (mIni .getFile () != null ) {
146158 // Create the parent if needed.
147159 final File parent = mIni .getFile ().getParentFile ();
160+ FileUtils .setPermissions (parent .getParent () + "/" , 511 , -1 , -1 );
148161 if (!Objects .requireNonNull (parent ).exists ()) {
149162 parent .mkdir ();
150163 }
151164 // Write, or create.
152165 mIni .store ();
153166
154- setFilePermissionsFromMode (mIni .getFile (), rwxrwxrwx );
167+ setFilePermissionsFromMode (mIni .getFile (). getAbsolutePath (), Context . MODE_WORLD_READABLE );
155168 } else {
156169 Log .w (TAG , "File is null. Ignoring." );
157170 }
0 commit comments