Issue
I would like to write a file in this path using a springboot servlet that runs on Tomcat9
/mnt/data-new/data/USERPROFILE/607/file.txt
When I try to write that file, i get this error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Jul 03 14:11:39 UTC 2019
There was an unexpected error (type=Internal Server Error, status=500).
/mnt/data-new/data/USERPROFILE/607/file.txt (Read-only file system)
I already set permissions on that path with 777 but nothing changed.
Servlet code is quite simple:
@GetMapping("/")
public String index() throws IOException {
FileWriter fw = new FileWriter("/mnt/data-new/data/USERPROFILE/607/file.txt");
fw.write("File wrote");
fw.close();
return "OK";
}
I created a file too named "file2.txt" that I can read with this example code:
@ResponseBody
@GetMapping("/read")
public String read() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/mnt/data-new/data/USERPROFILE/607/file2.txt"));
String st;
while ((st = br.readLine()) != null) return st;
return null;
}
I thought it was a catalina policy, but it seems disabled:
root@devel-spring:/etc/tomcat9# echo $CATALINA_OPTS
-Djava.security.debug=all
and, even with this option, i get no security manager logs in "/var/log/syslog"
What should i do to write files in that absolute path?
Edit: Filesystem is mounted RW, I can create that file using bash.
Solution
Thanks to Emmanuel Bourg in debian bugs.debian.org link i found that Tomcat9
"is sandboxed to write only into its own directory. You'll have to tweak the systemd configuration to allow Tomcat to write into ..." [that] "... directory".
so i nano'ed /etc/systemd/system/multi-user.target.wants/tomcat9.service adding
ReadWritePaths=/mnt/data-new/data/
then
systemctl daemon-reload
service tomcat9 restart
Now,
with the code above, i managed successfully to write my test file.
Answered By - Polletto