KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > tools > rights > UnixChown


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005-2006 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.tools.rights;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * It tries to change owner of a file invoking a system command
28  * (fork + exec), so it's slow, relies on system.
29  * Anyway, it should work on unixes
30  *
31  * Another solution would be a JNI call on a so, but it would be less
32  * portable, require a C compiler a build time, etc...
33  *
34  * @author Jean-Francois POUX
35  */

36 public class UnixChown implements IChown {
37     private Log log = LogFactory.getLog(UnixChown.class);
38     
39     public void chown(String JavaDoc file, String JavaDoc user) throws RightException {
40         String JavaDoc[] args = new String JavaDoc [3];
41         args[0] = "chown";
42         args[1] = user;
43         args[2] = file;
44         if (file.contains(".."))
45             throw new RightException("There is a .. in the file path.");
46         try {
47             Process JavaDoc child = Runtime.getRuntime().exec(args);
48             child.waitFor();
49             int ret = child.exitValue();
50             if (ret!=0)
51                 throw new RightException ("Could not perform chown "+user+" "+file);
52         } catch (Exception JavaDoc e) {
53             throw new RightException (e);
54         }
55         log.debug("Performed chown "+user+" "+file);
56     }
57
58     public void recursiveChown(String JavaDoc file, String JavaDoc user) throws RightException {
59         String JavaDoc[] args = new String JavaDoc [4];
60         args[0] = "chown";
61         args[1] ="-R";
62         args[2] = user;
63         args[3] = file;
64         if (file.contains(".."))
65             throw new RightException("There is a .. in the file path.");
66         try {
67             Process JavaDoc child = Runtime.getRuntime().exec(args);
68             child.waitFor();
69             int ret = child.exitValue();
70             if (ret!=0)
71                 throw new RightException ("Could not perform chown "+user+" "+file);
72         } catch (Exception JavaDoc e) {
73             throw new RightException (e);
74         }
75         log.debug("Performed chown -R "+user+" "+file);
76     }
77
78 }
79
Popular Tags