How to set Permissions on New File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hal Vaughan

    How to set Permissions on New File

    I've been reading up on file permissions (including FilePermission,
    Permission, and Permissions). From what I see, these are temporary and
    forgotten when a program exits (if I'm wrong, tell me, please).

    I'm installing a program on a computer and that includes configuration files
    that change regularly. When I create the configuration files, I want them
    to be readable and writable by any user running the program, otherwise my
    program won't be able to function.

    So how can I, from Java, find a way to set the permissions on a file I
    create (from within Java) so all users can read and write that file
    whenever they run the program?

    My first thought was to have the program itself, whenever it was started,
    set the permissions with a FilePermission object, but that doesn't make
    sense -- if files are not readable or writable by a user, it wouldn't make
    sense to allow that user to change the permissions.

    Thanks for any info.

    Hal
  • Anthony Borla

    #2
    Re: How to set Permissions on New File

    Hal,

    "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
    news:GTmnb.4672 6$HS4.219485@at tbi_s01...[color=blue]
    >
    > I've been reading up on file permissions (including FilePermission,
    > Permission, and Permissions). From what I see, these are
    > temporary and forgotten when a program exits (if I'm wrong, tell
    > me, please).
    >[/color]

    They represent the access the current process and its children [i.e. your
    running program] has to the specified files. In other words, they make sense
    only in context of the current program, and, yes, do not actually alter any
    system-level, file-specific attributes.
    [color=blue]
    >
    > I'm installing a program on a computer and that includes
    > configuration files that change regularly. When I create the
    > configuration files, I want them to be readable and writable by
    > any user running the program, otherwise my
    > program won't be able to function.
    >[/color]

    This is easily achieved using the relevant operating system utility which is
    nothing more than an interface to the relevant operating system function(s)
    / call(s) which actually perform such tasks. A system administrator would
    normally call such utilities from a script file, or perhaps use a friendly
    graphical interface to execute them.

    For instance, on Windows-family systems the 'attrib' and 'cacls' utilities
    would likely be used, while on *NIX and Linux systems the relevant utilities
    are 'chmod' and 'chown'. In both cases, however, 'friendlier' utilities may
    well exist.
    [color=blue]
    >
    > So how can I, from Java, find a way to set the permissions
    > on a file I create (from within Java) so all users can read and
    > write that file whenever they run the program?
    >[/color]

    The simplest approach is to launch the relevant operating system utilities
    from within your application via 'Runtime.exec'. Dig out the applicable
    operating system manuals and start experimenting :)

    A much cleaner approach is to use JNI routines which tap into the relevant
    operating system functionality. A web search may help discover a suitable
    library if this approach is preferred.
    [color=blue]
    >
    > My first thought was to have the program itself, whenever it
    > was started, set the permissions with a FilePermission object,
    > but that doesn't make sense -- if files are not readable or writable
    > by a user, it wouldn't make sense to allow that user to change the
    > permissions.
    >[/color]

    Correct.
    [color=blue]
    >
    > Thanks for any info.
    >[/color]

    I hope this helps.

    Anthony Borla


    Comment

    • Hal Vaughan

      #3
      Re: How to set Permissions on New File

      Thanks! It helps a lot.

      I was looking at using native commands, since I couldn't find anything else.
      For me, that is a last resort, since it means writing a special
      implementation for each OS the installer works on, and I was hoping to keep
      it as multi-platform as possible (part of the reason I'm using Java). At
      least using chmod will work for most OSs out there and I have a book on
      Windows scripting to take care of that.

      You also provide a lot of useful and clear information. Your comments are a
      big help and greatly appreciated.

      Hal

      Anthony Borla wrote:
      [color=blue]
      > Hal,
      >
      > "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
      > news:GTmnb.4672 6$HS4.219485@at tbi_s01...[color=green]
      >>
      >> I've been reading up on file permissions (including FilePermission,
      >> Permission, and Permissions). From what I see, these are
      >> temporary and forgotten when a program exits (if I'm wrong, tell
      >> me, please).
      >>[/color]
      >
      > They represent the access the current process and its children [i.e. your
      > running program] has to the specified files. In other words, they make
      > sense only in context of the current program, and, yes, do not actually
      > alter any system-level, file-specific attributes.
      >[color=green]
      >>
      >> I'm installing a program on a computer and that includes
      >> configuration files that change regularly. When I create the
      >> configuration files, I want them to be readable and writable by
      >> any user running the program, otherwise my
      >> program won't be able to function.
      >>[/color]
      >
      > This is easily achieved using the relevant operating system utility which
      > is nothing more than an interface to the relevant operating system
      > function(s) / call(s) which actually perform such tasks. A system
      > administrator would normally call such utilities from a script file, or
      > perhaps use a friendly graphical interface to execute them.
      >
      > For instance, on Windows-family systems the 'attrib' and 'cacls' utilities
      > would likely be used, while on *NIX and Linux systems the relevant
      > utilities are 'chmod' and 'chown'. In both cases, however, 'friendlier'
      > utilities may well exist.
      >[color=green]
      >>
      >> So how can I, from Java, find a way to set the permissions
      >> on a file I create (from within Java) so all users can read and
      >> write that file whenever they run the program?
      >>[/color]
      >
      > The simplest approach is to launch the relevant operating system utilities
      > from within your application via 'Runtime.exec'. Dig out the applicable
      > operating system manuals and start experimenting :)
      >
      > A much cleaner approach is to use JNI routines which tap into the relevant
      > operating system functionality. A web search may help discover a suitable
      > library if this approach is preferred.
      >[color=green]
      >>
      >> My first thought was to have the program itself, whenever it
      >> was started, set the permissions with a FilePermission object,
      >> but that doesn't make sense -- if files are not readable or writable
      >> by a user, it wouldn't make sense to allow that user to change the
      >> permissions.
      >>[/color]
      >
      > Correct.
      >[color=green]
      >>
      >> Thanks for any info.
      >>[/color]
      >
      > I hope this helps.
      >
      > Anthony Borla[/color]

      Comment

      • Phil...

        #4
        Re: How to set Permissions on New File

        I thought JNI would not give the programm
        more permissions than that of the user that
        ran the program -- else anyone could rm -rf *


        "Anthony Borla" <ajborla@bigpon d.com> wrote in message
        news:gJpnb.1683 06$bo1.148019@n ews-server.bigpond. net.au...[color=blue]
        > Hal,
        >
        > "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
        > news:GTmnb.4672 6$HS4.219485@at tbi_s01...[color=green]
        > >
        > > I've been reading up on file permissions (including FilePermission,
        > > Permission, and Permissions). From what I see, these are
        > > temporary and forgotten when a program exits (if I'm wrong, tell
        > > me, please).
        > >[/color]
        >
        > They represent the access the current process and its children [i.e. your
        > running program] has to the specified files. In other words, they make[/color]
        sense[color=blue]
        > only in context of the current program, and, yes, do not actually alter[/color]
        any[color=blue]
        > system-level, file-specific attributes.
        >[color=green]
        > >
        > > I'm installing a program on a computer and that includes
        > > configuration files that change regularly. When I create the
        > > configuration files, I want them to be readable and writable by
        > > any user running the program, otherwise my
        > > program won't be able to function.
        > >[/color]
        >
        > This is easily achieved using the relevant operating system utility which[/color]
        is[color=blue]
        > nothing more than an interface to the relevant operating system[/color]
        function(s)[color=blue]
        > / call(s) which actually perform such tasks. A system administrator would
        > normally call such utilities from a script file, or perhaps use a friendly
        > graphical interface to execute them.
        >
        > For instance, on Windows-family systems the 'attrib' and 'cacls' utilities
        > would likely be used, while on *NIX and Linux systems the relevant[/color]
        utilities[color=blue]
        > are 'chmod' and 'chown'. In both cases, however, 'friendlier' utilities[/color]
        may[color=blue]
        > well exist.
        >[color=green]
        > >
        > > So how can I, from Java, find a way to set the permissions
        > > on a file I create (from within Java) so all users can read and
        > > write that file whenever they run the program?
        > >[/color]
        >
        > The simplest approach is to launch the relevant operating system utilities
        > from within your application via 'Runtime.exec'. Dig out the applicable
        > operating system manuals and start experimenting :)
        >
        > A much cleaner approach is to use JNI routines which tap into the relevant
        > operating system functionality. A web search may help discover a suitable
        > library if this approach is preferred.
        >[color=green]
        > >
        > > My first thought was to have the program itself, whenever it
        > > was started, set the permissions with a FilePermission object,
        > > but that doesn't make sense -- if files are not readable or writable
        > > by a user, it wouldn't make sense to allow that user to change the
        > > permissions.
        > >[/color]
        >
        > Correct.
        >[color=green]
        > >
        > > Thanks for any info.
        > >[/color]
        >
        > I hope this helps.
        >
        > Anthony Borla
        >
        >[/color]


        Comment

        • Anthony Borla

          #5
          Re: How to set Permissions on New File

          Phil,

          "Phil..." <rynes@ieee.org > wrote in message
          news:rAxnb.5145 6$HS4.234306@at tbi_s01...[color=blue]
          > I thought JNI would not give the programm
          > more permissions than that of the user that
          > ran the program -- else anyone could rm -rf *
          >[/color]

          JNI simply allows you to tap into functionality not offered in Java. When
          using JNI you bypass the JVM, and surrender control of you application to
          'foreign' code, code which can pretty well do anything it wants *within
          operating system constraints* !

          The JVM and application execute within the context of a user account [or
          similar], and are bound by the security and other restrictions imposed on
          that account. So, to use your example, a JNI routine may possibly allow you
          to perform the equivalent of a 'rm -rf', but unless the user account [or
          similar] has the requisite permission(s), the attempt will fail with the
          operating system 'trapping' the attempt, and somehow handling / logging it.
          A JNI routine will, to be considered robust, handle such possibilities
          predictably and gracefully allowing the application to handle the situation
          as required.

          It may help to think of an [Java] application's [not applet] execution
          environment as a layered entity, somewhat like an onion. The application
          executes under the supervision and control of the JVM but may, using
          facilities like JNI, temporarily bypass that layer when necessary. When
          doing so it will encounter the operating system layer, typically the
          security a sub-system. In either case the application is bound by the
          constraints imposed by that layer, constraints which would normally have
          been imposed *before* application execution, and [probably] cannot be
          altered without a restart.

          I hope this helps.

          Anthony Borla


          Comment

          • Anthony Borla

            #6
            Re: How to set Permissions on New File

            Hal,

            "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
            news:FLvnb.3853 9$ao4.82577@att bi_s51...[color=blue]
            > Thanks! It helps a lot.
            >[/color]

            A pleasure :) !
            [color=blue]
            >
            > I was looking at using native commands, since I couldn't
            > find anything else. For me, that is a last resort, since it means
            > writing a special implementation for each OS the installer works
            > on, and I was hoping to keep it as multi-platform as possible
            > (part of the reason I'm using Java). At least using chmod will
            > work for most OSs out there and I have a book on
            > Windows scripting to take care of that.
            >[/color]

            Application installation [as opposed to applet installation] is inherently
            operating-system specific [just look at all those .tar and .zip files out
            there in cyberspace !]. It certainly pays to be familiar with all the
            installation-related utilities, and scripting facilities offered by the
            target platforms.
            [color=blue]
            >
            > You also provide a lot of useful and clear information.You r
            > comments are a big help and greatly appreciated.
            >[/color]

            My belief is to try [where possible, and, of course, relavant] to explain
            general principles in forum responses. After all, answers to very-specific
            questions can easily be obtained from text books and manuals [not to mention
            Google searches :) !].

            Cheers,

            Anthony Borla


            Comment

            • Hal Vaughan

              #7
              Re: How to set Permissions on New File

              Anthony Borla wrote:
              [color=blue]
              > Hal,
              >
              > "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
              > news:FLvnb.3853 9$ao4.82577@att bi_s51...[/color]
              <snip>[color=blue][color=green]
              >>
              >> You also provide a lot of useful and clear information.You r
              >> comments are a big help and greatly appreciated.
              >>[/color]
              >
              > My belief is to try [where possible, and, of course, relavant] to explain
              > general principles in forum responses. After all, answers to very-specific
              > questions can easily be obtained from text books and manuals [not to
              > mention Google searches :) !].[/color]

              It is much appreciated. Although I had tried several Google searches and
              some other research, I didn't find anything that specifically told me I
              could not do what I wanted in Java, so getting a full answer was definately
              helpful. Your extra effort is a big help (and I'm sure, one day, it'll
              help someone else who finds this thread in a Google search).

              Hal
              [color=blue]
              > Cheers,
              >
              > Anthony Borla[/color]

              Comment

              Working...