Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

Re: writing to File-System; IOException on File.mkdirs() and/or File.createNewFile()

Christoph Meier

2004-04-29

Replies:

Hi

today i was debugging a lillte bit deeper (into sdk 1.4.2) to find out
where the IOException ecatly is thrown ...
in my desperation i also tried an alternative jre (blackdown); the
debugging i did with standard sun j2se

... may be this can help somebody to give me the final hint.

at the end of this mail i attach the script i'm using to start Tomcat
(onto which i added a "umask 006" ...)

-> debugging aFile.mkdirs()
-----------------------------------------------------
public class File implements java.io.Serializable, Comparable {
// ...
static private FileSystem fs = FileSystem.getFileSystem();

public boolean mkdirs() {
 if (exists()) {
    return false;
 }
 if (mkdir()) {
    return true;
  }
    File canonFile = null;
    try {
      canonFile = getCanonicalFile();
    } catch (IOException e){    // -> 1
      return false;
    }
 String parent = canonFile.getParent();
    return (parent != null) && (new File(parent).mkdirs() &&
canonFile.mkdir());
 }
// ...
 public File getCanonicalFile() throws IOException {
   return new File(getCanonicalPath());
 }
// ...
 public String getCanonicalPath() throws IOException {
  return fs.canonicalize(fs.resolve(this));
 }

// ...

}
--------------------------------------------

Exception is thrown at (1) in >>mkdirs()<<
when calling >>getCanonicalFile()<<
the IOException is passed from >>getCanonicalPath()<<
within >>getCanonicalPath()<< it is  >>fs.canonicalize()<< which fails.
>>fs<< is the plattform-dependant Implementation of abstract
java.io.FileSystem , for which i had no Source-Code to debugg into;
actually it is java.io.UnixFileSystem, and i know how it looks like
(see
http://www.cs.purdue.edu/homes/palsberg/cs565/java-source/java.1.2.2/src/solaris/classes/java/io/UnixFileSystem.java
, maybe there is a newer version somewhere )

in UnixFileSystem (see Link above) >>canonicalize()<< calls a native
method;
#---- snippet ----
public native String canonicalize(String path) throws IOException;
#----------------


the stack trace when calling directly >>aFile.getCanonicalPath()<< im
my TestService looks like:
->
.#-------------------------------------
java.io.IOException: Bad pathname
    at java.io.UnixFileSystem.canonicalize0(Native Method)
    at java.io.UnixFileSystem.canonicalize (UnixFileSystem.java:154)
    at java.io.File.getCanonicalPath (File.java:513)
    at ch.hmb.app.service.TestService.writeTo(TestService.java:78)
#---------------------------------------
.. the "Bad pathname" is nothing else than
"home/hmb/catalina_5_19/webapps/hmb/a/1.txt" which should't be that bad
...(i guessed)
... later on in my TestService ...
when calling >>aFile.createNewFile()<< next Exception is thrown
#------------------------------------------------------------------
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile (File.java:828)
    at ch.hmb.app.service.TestService.writeTo(TestService.java:94)
#----------------------------------------------------
... but that's no more mistery since >>mkdirs()<< already failed




below are the stacks from the Exceptions thrown when using
blackdown-jre ... looks the same
#-------------------------------------
java.io.IOException: Bad pathname
    at java.io.UnixFileSystem.canonicalize0(Native Method)
    at java.io.UnixFileSystem.canonicalize (UnixFileSystem.java:154)
    at java.io.File.getCanonicalPath (File.java:513)
    at ch.hmb.app.service.TestService.writeTo(TestService.java:78)
#---------------------------------------
java.io.IOException: /home/hmb/catalina_5_19/webapps/hmb/a/1.txt
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile (File.java:828)
    at ch.hmb.app.service.TestService.writeTo(TestService.java:94)
#----------------------------------------------------


to finish i would like to emphasise again, that i still have another
Class "Test" which works fine concerning
>>mkdirs()<< and >>createNewFile()<< when i start it directly from the
shell (without Tomcat)

->
tomcat5@(protected)
/home/hmb/catalina_5_19/webapps/hmb/1/2/3/4/myFile.txt
-> and the File is written!!!

well, i still hope to find a solution ... may be someone has a good hint?

thanks,
regards,
christoph



############## start-script ########################
JAVA_HOME=/usr/java
JAVA_OPTS="${JAVA_OPTS} -XX:NewSize=48m -XX:MaxNewSize=48m
-XX:SurvivorRatio=8 -Xms128m -Xmx512m"
CATALINA_OPTS=
CATALINA_BASE=/home/hmb/catalina_5_19
CATALINA_HOME=$CATALINA_BASE
CATALINA_TMPDIR="$CATALINA_BASE"/webapps/hmb
JAVA_ENDORSED_DIRS="$CATALINA_HOME"/common/endorsed

CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
$CATALINA_HOME/bin/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar

. $CATALINA_HOME/bin/custom.sh

umask 006

exec $CATALINA_HOME/bin/jsvc $JAVA_OPTS $CATALINA_OPTS \
  -user tomcat5 \
          -nodetach \
  -home $JAVA_HOME \
          -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
          -Dcatalina.base=$CATALINA_BASE \
  -Dcatalina.home=$CATALINA_HOME \
  -Djava.io.tmpdir=$CATALINA_TMPDIR \
  -outfile /home/hmb/catalina_5_19/logs/catalina.out \
  -errfile '&1' \
  $CATALINA_OPTS \
  -cp $CLASSPATH \
  org.apache.catalina.startup.Bootstrap

############ EOF: start-script ########################

> Hallo
>
> i have some problems writing Files to the local harddisk.
> running Tomcat 5.19 on a Linux with a jre1.4.2, Tomcat started without
> security-option;
> process-owner is a user called "tomcat5"
>
> i'm trying to write a File (from scratch) into
> $catalinaBase/webapps/myApp/ .
> $catalinaBase/** belongs to user tomcat5, Permissions set to 770 .
>
> i'm doing something like:
> -------------------------------------------------------------------------
>     File aFile = new File(aFilePath);
>     if (!aFile.mkdirs()){
>       //
>     }
>         if (aFile.exists()){
>        if (!aFile.delete()){
>         //
>        }
>     }
>     try {
>        aFile.createNewFile();
>     } catch (IOException e) {
>        e.printStackTrace();
>     }
>       // adding some xml into the file ...
>      addContentToFile(aFile);
> ------------------------------------------------------------
> if the parent directory of "aFilePath" already exists, everything
> works fine ...
> if one of the parent-dirs is missing, it fails ...
> e.g. $catalinaBase/webapps/myApp/myTestFile.txt wil be created (since
> $catalinaBase/webapps/myApp already exists), but
> $catalinaBase/webapps/myApp/a/b/c/myTestFile.txt fails ....
>
> when debugging i see that >>aFile.mkdirs()<< returns false ... but
> since it throws no Exception, i has no clear idea why mkdirs() fails,
> later in the code >>aFile.createNewFile()<< throws an IOException ...
> >>
> java.io.IOException: /home/hmb/catalina_5_19/webapps/hmb/a/1.txt
>     at java.io.UnixFileSystem.createFileExclusively(Native Method)
>     at java.io.File.createNewFile (File.java:828)
>     at ch.hmb.app.service.TestService.writeTo(TestService.java:93)
> <<
> when inspecting the IOException with debugger (using Eclipse build
> 30M8) root-cause of IOException or its localized message only contains
> the path of the File which i wanted to create ... (e.g.
> /home/hmb/catalina_5_19/webapps/hmb/a/1.txt)
>
> the "funny" thing is, all works fine with same Deployment on win2000
> and solaris (with same permissions set, i guess).
>
> has somebody an idea? does the problem concern rights/permissions
> and/or user/groups on the file-system or must i do a special grant in
> the cataliny.policy (and starting tomcat with the security option)?
>
> thanks in advance,
> kind regards
> christoph
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
> For additional commands, e-mail: tomcat-user-help@(protected)
>



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
For additional commands, e-mail: tomcat-user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.