boolean recursive = false;
String jar = null;
JarFile jf = null;
if(isAbsolutePath(dir, 0, dir.length)) {
st = new JavaSecuredFile(newStringFromUTF8(dir));
} else if(isJarFilePath(dir, 0, dir.length)) {
int ix = dir.length;
for(int i = 0;i<dir.length;i++) {
if(dir[i] == '!') {
ix = i;
break;
}
}
st = new JavaSecuredFile(newStringFromUTF8(dir, 5, ix-5));
if(ix<dir.length) {
jar = newStringFromUTF8(dir, ix+1, dir.length-(ix+1));
try {
jf = new JarFile(st);
if (jar.startsWith("/")) jar = jar.substring(1);
if (jf.getEntry(jar + "/") != null) jar = jar + "/";
} catch(Exception e) {
jar = null;
jf = null;
}
}
} else {
st = new JavaSecuredFile(cwd, newStringFromUTF8(dir));
}
if((jf != null && ("".equals(jar) || (jf.getJarEntry(jar) != null &&
jf.getJarEntry(jar).isDirectory()))) || st.isDirectory()) {
if(m != -1 && Arrays.equals(magic, DOUBLE_STAR)) {
int n = base.length;
recursive = true;
buf.length(0);
buf.append(base);
buf.append(bytes, (base.length > 0 ? m : m + 1), end - (base.length > 0 ? m : m + 1));
if (jf != null) {
buf = fixBytesForJarInUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize());
}
status = glob_helper(cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), n, flags, func, arg);
if(status != 0) {
break finalize;
}
}
} else {
break mainLoop;
}
if(jar == null) {
String[] dirp = files(st);
for(int i=0;i<dirp.length;i++) {
if(recursive) {
byte[] bs = getBytesInUTF8(dirp[i]);
if (fnmatch(STAR,0,1,bs,0,bs.length,flags) != 0) {
continue;
}
buf.length(0);
buf.append(base);
buf.append( BASE(base) ? SLASH : EMPTY );
buf.append(getBytesInUTF8(dirp[i]));
if (isAbsolutePath(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize())) {
st = new JavaSecuredFile(newStringFromUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize()));
} else {
st = new JavaSecuredFile(cwd, newStringFromUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize()));
}
if(st.isDirectory() && !".".equals(dirp[i]) && !"..".equals(dirp[i])) {
int t = buf.getRealSize();
buf.append(SLASH);
buf.append(DOUBLE_STAR);
buf.append(bytes, m, end - m);
status = glob_helper(cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), t, flags, func, arg);
if(status != 0) {
break;
}
}
continue;
}
byte[] bs = getBytesInUTF8(dirp[i]);
if(fnmatch(magic,0,magic.length,bs,0, bs.length,flags) == 0) {
buf.length(0);
buf.append(base);
buf.append( BASE(base) ? SLASH : EMPTY );
buf.append(getBytesInUTF8(dirp[i]));
if(m == -1) {
status = func.call(buf.getUnsafeBytes(),0, buf.getRealSize(),arg);
if(status != 0) {
break;
}
continue;
}
link.add(new DirGlobber(buf, null));
buf = new ByteList(20);
}
}
} else {
try {
List<JarEntry> dirp = new ArrayList<JarEntry>();
for(Enumeration<JarEntry> eje = jf.entries(); eje.hasMoreElements(); ) {
JarEntry je = eje.nextElement();
String name = je.getName();
int ix = name.indexOf('/', jar.length());
if (ix == -1 || ix == name.length()-1) {
if("/".equals(jar) || (name.startsWith(jar) && name.length()>jar.length())) {
dirp.add(je);
}
}
}
for(JarEntry je : dirp) {
String basename = (new File(je.getName())).getName();
byte[] bs = getBytesInUTF8(basename);
byte[] absoluteName = getBytesInUTF8(je.getName());
int len = bs.length;
int absoluteLen = absoluteName.length;
if(je.isDirectory()) {
len--;
absoluteLen--;
}
if(recursive) {
if(fnmatch(STAR,0,1,bs,0,len,flags) != 0) {
continue;
}
buf.length(0);
buf.append(base, 0, base.length - jar.length());
buf.append( BASE(base) ? SLASH : EMPTY );
buf.append(absoluteName, 0, absoluteLen);
if(je.isDirectory()) {
int t = buf.getRealSize();
buf.append(SLASH);
buf.append(DOUBLE_STAR);
buf.append(bytes, m, end - m);
buf = fixBytesForJarInUTF8(buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize());
status = glob_helper(cwd, buf.getUnsafeBytes(), buf.getBegin(), buf.getRealSize(), t, flags, func, arg);
if(status != 0) {
break;
}
}
continue;
}
if(fnmatch(magic,0,magic.length,bs,0,len,flags) == 0) {
buf.length(0);
buf.append(base, 0, base.length - jar.length());
buf.append( BASE(base) ? SLASH : EMPTY );
buf.append(absoluteName, 0, absoluteLen);
buf = fixBytesForJarInUTF8(buf.getUnsafeBytes(), 0, buf.getRealSize());
if(m == -1) {
status = func.call(buf.getUnsafeBytes(),0, buf.getRealSize(),arg);
if(status != 0) {
break;
}
continue;
}
link.add(new DirGlobber(buf, je));
buf = new ByteList(20);
}
}
} catch(Exception e) {}
}
} while(false);
if (link.size() > 0) {
for (DirGlobber globber : link) {
ByteList b = globber.link;
if (status == 0) {
if(isAbsolutePath(b.getUnsafeBytes(), b.begin(), b.getRealSize())) {
st = new JavaSecuredFile(newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize()));
} else {
st = new JavaSecuredFile(cwd, newStringFromUTF8(b.getUnsafeBytes(), 0, b.getRealSize()));
}
if(st.isDirectory() || (globber.jarEntry != null && globber.jarEntry.isDirectory()) ) {
int len = b.getRealSize();
buf.length(0);
buf.append(b);