2

My code runs in the simulator, but crashes on Android with a FileNotFoundException as it tries to FileSystemStorage#openOutputStream in an non-existing directory. I create all the needed directories recursively before using

private void ensureParentDirs(String file) {
    final int j = file.lastIndexOf("/");
    final String s = file.substring(0, j);
    if (storage.isDirectory(s)) return;
    storage.mkdir(s);
    if (storage.isDirectory(s)) return;
    ensureParentDirs(s);
    storage.mkdir(s);
    if (storage.isDirectory(s)) return;
    Log.p("Cannot create directory: " + s);
}

which is supposed to work like new File(file).getParentFile().mkdirs(). It might be wrong, but then it shouldn't run in the simulator either, so I'd call it a bug.

I get the message

Cannot create directory:
file:///data/user/0/my.package.name/files//dump/000/abcd

but the parent directory ("000") has been successfully created. Using adb shell, I can create the directory using

mkdir /data/data/my.package.name/files/dump/000/abcd

so I can't see what's wrong. Any idea?

maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • Why are you invoking `mkdir()` and not `mkdirs()`? Notice the method returns a boolean if the operation was successful. What did it return? – Shai Almog Nov 13 '18 at 04:14
  • @ShaiAlmog There's no `mkdirs` in your `com.codename1.io.FileSystemStorage` and `mkdir` is `void`. I guess, you mean `File`, but that's not what I'm using. I guess, I should, but that's the third thing (I started with `Storage`). `+++` I see, I can use `FSS` with `File` easily.... however, what I'm doing doesn't look much different from `mkdirs`. – maaartinus Nov 13 '18 at 04:56
  • Does it work when you don't have the double slash (`... files//dump ...`)? – Henry Nov 13 '18 at 05:06
  • @Henry I didn't try... I know I should, but I've already spent too much time on this. I was thinking about it, but `....files//dump` could be created, so double slashes in general should work. And in the meantime, I rewrote the code (for unrelated reasons....). – maaartinus Nov 13 '18 at 05:10
  • @ShaiAlmog It looks like there was a problem with doubles slashes as Henry assumed. – maaartinus Nov 13 '18 at 07:19
  • @Henry After my rewrite, the double slashes are gone and so is the problem. – maaartinus Nov 13 '18 at 13:30

1 Answers1

0

There was (possibly still existent) problem with double slashes. My path was

/dump/000/abcd

and I transformed it via

path -> APP_HOME_PATH + "/" + path

into

file:///data/user/0/my.package.name/files//dump/000/abcd

which failed because of the double slashes, while

dump/000/abcd

transforms into

file:///data/user/0/my.package.name/files/dump/000/abcd

and works correctly.

maaartinus
  • 44,714
  • 32
  • 161
  • 320