Hello,
attached is what I did to solve the problem, for Unix.
mfg Frank
"Lloyd H. Meinholz" wrote:
>
> Is it possible to use chmod to change directory permissions? From the
> description of chmod in the manual, it appears to only work with files
> and not directories. I am on Solaris. What I want to do is the
> equivalent of "chmod -R g+w *". I know I can exec or change my umask,
> but I would prefer not to if possible. Here's what I'm doing:
>
> <chmod dir="${java.lib.dir}" perm="g+w"/>
>
> Is there a variation of this to include java.lib.dir and it's
> subdirectories in the chmod?
>
> The reason I need to do this is that we have a development area that
> more than one developer could be building into (not at the same time)
> and the builds are failing if a developer isn't owner and the file and
> dir group flags aren't +w. I don't want to change my umask because we do
> more than just compile java on this particular host and exec'ing just
> feels wrong. It also seems to me that since chmod seems based on UNIX
> chmod, it should behave like UNIX chmod and let me change dir
> permissions. :)
>
> Thanks for any help/advice.
>
> Lloyd
package com.bancos.ant;
public class ChmodEx extends BaseShSilent {
private String basedir;
public void setBasedir(String basedir) {
this.basedir=basedir;
}
private String perm;
public void setPerm(String perm) {
this.perm=perm;
}
public String[] getCmds() {
String ldir=basedir!=null?basedir:".";
String[] s={
"find "+ldir+" -type f |xargs chmod "+perm,
"exit 0"
};
return s;
}
}
package com.bancos.ant;
import java.io.*;
import org.apache.tools.ant.*;
/** Die Klasse bietet die Moeglichkeit bequem ein in
Java definiertes Script auszufuehren.
*/
public abstract class BaseSh extends Task {
public final static String sccs = "@(#)BaseSh.java
1.2\t2/12/01\t11:50:23";
/** Die Mehtode muss von Subklassen so ueberschrieben werden,
das sie die Zeilen des Scrips returnt.
Der Interpreter ist /bin/sh
Beachte das das letzte cmd so sein muss das die sh returnt,
sonst haengt der Process.
*/
public abstract String[] getCmds();
/** Die Methode setzt die beiden Output-Streams des Prozesses p */
protected void setStreams(Process p) {
new Thread(new LogDump(p.getInputStream())).start();
new Thread(new LogDump(p.getErrorStream())).start();
}
public void execute() throws BuildException {
try {
String[] args={ "/bin/sh", "-s" };
Process p=Runtime.getRuntime().exec(args);
setStreams(p);
PrintStream ps=new PrintStream(p.getOutputStream());
String[] cmds=getCmds();
for(int i=0; i<cmds.length; i++) {
if(echo)
log(cmds[i]);
ps.println(cmds[i]);
}
ps.flush();
if(wait) {
int i=p.waitFor();
if(i!=0)
throw new RuntimeException("failed: "+i);
}
}catch(Exception e) {
e.printStackTrace(System.err);
throw new BuildException(e.getMessage());
}
}
public void setWait(boolean wait) {
this.wait=wait;
}
private boolean wait=true;
public void setEcho(boolean echo) {
this.echo=echo;
}
public boolean echo=false;
private class LogDump implements Runnable {
private InputStream in;
LogDump(InputStream in) {
this.in=in;
}
public void run() {
try {
int c;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
while((c=in.read())>=0) {
if(c=='\n') {
log(new String(bos.toByteArray()));
bos=new ByteArrayOutputStream();
} else
bos.write(c);
}
}catch(Exception e) {
//empty
}
}
}
}
/*****************************************************************
Es wurde ein Property "wait" implementiert, welches es ermoeglicht
die sh "im Hintergrund" zu starten.
******************************************************************/
package com.bancos.ant;
import java.io.*;
/** Die Klasse macht dasselbe wie BaseSh, aber die Output-Streams
werden nicht gedumpt, sondern nach /dev/null geschickt.
*/
public abstract class BaseShSilent extends BaseSh {
protected void setStreams(Process p) {
new Thread(new StreamSucker(p.getInputStream())).start();
new Thread(new StreamSucker(p.getErrorStream())).start();
}
private class StreamSucker implements Runnable {
private InputStream in;
StreamSucker(InputStream in) {
this.in=in;
}
public void run() {
try {
while(in.read()>=0);
}catch(IOException e) {
// empty
}
}
}
}