Makefile added / pkg-config descriptor
[metux-java.git] / util / FileOps.java
blob2ba832fdcd9a6e2c37e5043182b633ceefeda642
2 package org.de.metux.util;
4 import java.util.Properties;
5 import java.io.BufferedReader;
6 import java.io.IOException;
7 import java.io.InterruptedIOException;
8 import java.io.InputStreamReader;
9 import java.io.File;
11 public class FileOps
13 // FIXME: parse the modes
14 public static boolean chmod(String fn, String mode)
16 return Exec.exec_command(
17 "chmod "+
18 ShellEscape.filename(mode)+
19 " "+
20 ShellEscape.filename(fn)
24 public static boolean cp(String source, String dest)
26 return cp(source,dest,null);
29 public static boolean cp(String source, String dest, String mode)
31 if (dest.endsWith("/"))
32 mkdir(dest);
33 else
34 mkdir(PathNormalizer.dirname(dest));
36 if (!Exec.exec_command(
37 "cp "+
38 ShellEscape.filename(source)+
39 " "+
40 ShellEscape.filename(dest)
42 return false;
44 if (!StrUtil.isEmpty(mode))
46 String dir;
48 if (new File(dest).isDirectory())
49 dir = dest+"/"+PathNormalizer.basename(source);
50 else
51 dir = dest;
53 return chmod(dir,mode);
56 return true;
59 public static boolean mkdir(String dir)
61 if ((dir==null)||(dir.length()==0))
62 return false;
64 File handle = new File(dir);
65 handle.mkdirs();
66 return handle.isDirectory();
69 public static boolean mkdir(String dirs[])
71 if ((dirs==null)||(dirs.length==0))
72 return false;
73 for (int x=0; x<dirs.length; x++)
74 mkdir(dirs[x]);
75 return true;
78 public static void symlink(String target, String name)
80 // System.err.println("TARGET="+target);
81 // System.err.println("NAME="+name);
83 String cmd =
84 "ln -s "+
85 ShellEscape.filename(target)+
86 " "+
87 ShellEscape.filename(name);
88 System.err.println("symlink# "+cmd);
89 new Exec().run(cmd);
92 public static void rm(String fn)
94 new File(fn).delete();
97 public static String getcwd()
99 File f = new File(".");
100 String filePath = f.getAbsolutePath();
101 int dotAt = filePath.lastIndexOf(".");
102 return (new String(filePath.substring(0,dotAt)));
105 public static File getcwd_handle()
107 return new File(getcwd());
110 public static boolean chdir(String dirname)
112 File d = new File(dirname);
113 if (!d.isDirectory())
114 return false;
116 System.setProperty("user.dir", d.getAbsolutePath());
117 return true;
120 public static String md5(File f)
122 String out = _run_cmd("md5sum "+f.getAbsolutePath()+" 2>&1");
123 if (out==null)
124 throw new RuntimeException("md5: exec didnt give result for: "+f);
126 String s[] = StrSplit.split(out);
128 if (s.length==0)
129 throw new RuntimeException("md5: got no result for: "+f);
131 return s[0];
134 static private String _run_cmd(String cmdline)
136 String cmds[] = cmds = new String[3];
137 Process process;
139 cmds[0] = "/bin/bash";
140 cmds[1] = "-c";
141 cmds[2] = cmdline+" 2>&1";
143 // System.err.println("_run_cmd(): "+cmdline);
145 try
147 process = Runtime.getRuntime().exec
149 cmds,
150 null,
151 new File(System.getProperty("user.dir"))
154 BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
155 String res = "";
156 String line;
158 while ((line=stdout.readLine())!=null)
159 res += line;
161 return res;
163 catch (IOException e)
165 throw new RuntimeException(e);
168 // catch (InterruptedException e)
169 // {
170 // callback.error("InterruptedException in Stage::exec()"+e);
171 // return false;
172 // }