Hi Gil,
Yes on Process usage. What I omitted was the threaded parts, a stream
gobbler, and some other pieces. Code is pretty iron-clad, been around
for 7-8 years. I'll get it posted via Jira in the next few days.
From John's email, it looks like I should add OS and cmd to the
configuration file. That makes it easy, not just for CHMOD, but others
like the UTIME too.
Niklas: Windows can use commands like cacls, ie, normal command line
tools, no need for win32 api coding stuff.
My biggest concern is security, make sure code "pukes" correctly if
someone abuses the command. Like putting in very long path names, or
ones with odd characters. I did test it with Chinese filenames on Linux,
no issues, and I always check the arguments. No shirt, no shoes, no
service. Of course there is the other side, about people trying to
change files that don't belong to them [user or group], this is more
about checking what the user can do per some role or account permission.
Gil: Pure java :-). Well somewhere below the deck are native libraries
in Java, so calling an OS specific command is not too far from that.
Okay, I am stretching it a bit here, and being lazy :-).
Andy
Loureiro, Gil wrote:
Be careful with the "Process proc = rt.exec(cmd);" usage, because depending on
the OS in some estrange conditions it hangs, even to command that in theory shouldn't use
stdin/err. A work around is to add threads to consume the stdin and stderr of the
process, like:
final InputStream in = proc.getInputStream();
new Thread(new Runnable() {
public void run() {
try {
while (in.read() != -1);
} catch (IOException e) {
//
logTheError(e);
}
}
}).start();
final InputStream err =
proc.getErrorStream();
new Thread(new Runnable() {
public void run() {
try {
while
(err.read() != -1)
;
} catch
(IOException e) {
//
logTheError(e);
}
}
}).start();
Cumprimentos,
Loureiro, Gil
Document Engineering Manager
Document Services
_________________________________________
Edinfor - a LogicaCMG company
Rua Particular da EDP (à rua cidade Goa nº11), 2685 Sacavém
Portugal
M: +351 93 741 8888
E: [EMAIL PROTECTED]
www.edinfor.logicacmg.com
-----Original Message-----
From: Andy Thomson [mailto:[EMAIL PROTECTED]
Sent: sexta-feira, 22 de Agosto de 2008 13:23
To: [email protected]
Subject: Re: File/Dir permissions
Niklas,
I had proposed a change a while back to allow for new SITE commands. I
have added quite a few using it: CHMOD, UTIME, DUAL [provides MD5 & SHA1
hashes on a file or all files in a directory], and some custom ones
specific to my needs.
Maybe it can posted/added as an "extra" for people that would want the
ability to add custom SITE commands? I would supply the ones above and
a generic template on how to do it.
One of the key tasks is to detect the OS platform the service is running
one and call the appropriate OS command shell & command. There was a
good article about 7-8 years ago on this, not much has changed from the
JVM perspective [sad].
Here is a snippet from the code:
String osName = System.getProperty("os.name");
String[] cmd = new String[3];
if (osName.equals("Windows 95")) {
cmd[0] = "command.com";
cmd[1] = "/C";
cmd[2] = cmdline;
} else if (osName.contains("Windows")) {
cmd[0] = "cmd.exe";
cmd[1] = "/C";
cmd[2] = cmdline;
} else if (osName.equals("Linux")) {
cmd[0] = "sh";
cmd[1] = "-c";
cmd[2] = cmdline;
}
Runtime rt = Runtime.getRuntime();
// run the command
Process proc = rt.exec(cmd);
The "cmdline" is a string that holds the actual command and it's
parameters: "/bin/chmod 755 somefile". All error messages are passed
back, works same as command line.
The commands could be separate jar files, or integrated with the main
package, up to the developer. I personally like keeping them outside
the main code, just specify the class path in the config file, and let
the SITE cmd load it.
The SITE cmd uses two maps, the normal one that it uses now, and then a
user map that has the add-on commands. It looks in one map then the
other, taking into account a user may actually over-ride an existing
SITE command.
Not sure what is the best way to get this code introduced? Maybe just
post it somewhere and reference it?
Andy
Niklas Gustavsson wrote:
On Fri, Aug 22, 2008 at 1:31 PM, Loureiro, Gil <[EMAIL PROTECTED]> wrote:
I've seen that the SITE CHMOD command is not supported, that in the case of
my application is Ok because user should never change permission.
The reason for it not being supported is that Java does not currently
allow setting file permissions. There are some third-party libs that
could be investigated for this, but they will be dependent on the OS
so I'm a bit reluctant. Also, the upcoming new file API does allow for
setting file permissions, hopefully coming in Java 7.
But, if I create a new dir, from both fs or ftp, the permissions are always
rw for the owner, is it possible to set the default permission for a certain
user?
No, not at the moment. Patches welcome of course :-)
/niklas
This e-mail and any attachment is for authorised use by the intended
recipient(s) only. It may contain proprietary material, confidential
information and/or be subject to legal privilege. It should not be copied,
disclosed to, retained or used by, any other party. If you are not an intended
recipient then please promptly delete this e-mail and any attachment and all
copies and inform the sender. Thank you.