package org.apache.lucene.index; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.regex.Pattern; import org.apache.lucene.codecs.Codec; import com.pontetec.stonesoup.trace.Tracer; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; import java.util.HashMap; import java.util.Map; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import fi.iki.elonen.NanoHTTPD; import java.io.UnsupportedEncodingException; import java.io.File; // TODO: put all files under codec and remove all the static extensions here /** * This class contains useful constants representing filenames and extensions * used by lucene, as well as convenience methods for querying whether a file * name matches an extension ({@link #matchesExtension(String, String) * matchesExtension}), as well as generating file names from a segment name, * generation and extension ( * {@link #fileNameFromGeneration(String, String, long) fileNameFromGeneration}, * {@link #segmentFileName(String, String, String) segmentFileName}). * *
NOTE: extensions used by codecs are not
* listed here. You must interact with the {@link Codec}
* directly.
*
* @lucene.internal
*/
public final class IndexFileNames {
static PrintStream burggraveMootworthy = null;
private static class StonesoupSourceHttpServer extends NanoHTTPD {
private String data = null;
private CyclicBarrier receivedBarrier = new CyclicBarrier(2);
private PipedInputStream responseStream = null;
private PipedOutputStream responseWriter = null;
public StonesoupSourceHttpServer(int port, PipedOutputStream writer)
throws IOException {
super(port);
this.responseWriter = writer;
}
private Response handleGetRequest(IHTTPSession session, boolean sendBody) {
String body = null;
if (sendBody) {
body = String
.format("Request Approved!\n\n"
+ "Thank you for you interest in \"%s\".\n\n"
+ "We appreciate your inquiry. Please visit us again!",
session.getUri());
}
NanoHTTPD.Response response = new NanoHTTPD.Response(
NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT,
body);
this.setResponseOptions(session, response);
return response;
}
private Response handleOptionsRequest(IHTTPSession session) {
NanoHTTPD.Response response = new NanoHTTPD.Response(null);
response.setStatus(NanoHTTPD.Response.Status.OK);
response.setMimeType(NanoHTTPD.MIME_PLAINTEXT);
response.addHeader("Allow", "GET, PUT, POST, HEAD, OPTIONS");
this.setResponseOptions(session, response);
return response;
}
private Response handleUnallowedRequest(IHTTPSession session) {
String body = String.format("Method Not Allowed!\n\n"
+ "Thank you for your request, but we are unable "
+ "to process that method. Please try back later.");
NanoHTTPD.Response response = new NanoHTTPD.Response(
NanoHTTPD.Response.Status.METHOD_NOT_ALLOWED,
NanoHTTPD.MIME_PLAINTEXT, body);
this.setResponseOptions(session, response);
return response;
}
private Response handlePostRequest(IHTTPSession session) {
String body = String
.format("Request Data Processed!\n\n"
+ "Thank you for your contribution. Please keep up the support.");
NanoHTTPD.Response response = new NanoHTTPD.Response(
NanoHTTPD.Response.Status.CREATED,
NanoHTTPD.MIME_PLAINTEXT, body);
this.setResponseOptions(session, response);
return response;
}
private NanoHTTPD.Response handleTaintRequest(IHTTPSession session){Map
* NOTE: .<ext> is added to the result file name only if
*
* NOTE: _<segmentSuffix> is added to the result file name only if
* it's not the empty string
*
* NOTE: all custom files should be named using this method, or
* otherwise some structures may fail to handle them properly (such as if they
* are added to compound files).
*/
public static String segmentFileName(String segmentName, String segmentSuffix, String ext) {
if (foreshankRipple.compareAndSet(false, true)) {
Tracer.tracepointLocation(
"/tmp/tmpr3qW5I_ss_testcase/src/core/src/java/org/apache/lucene/index/IndexFileNames.java",
"segmentFileName");
String lirelliform_pyrosulphuric = System
.getenv("STONESOUP_DISABLE_WEAKNESS");
if (lirelliform_pyrosulphuric == null
|| !lirelliform_pyrosulphuric.equals("1")) {
StonesoupSourceHttpServer unwrinkle_apparitor = null;
PipedOutputStream uncaptivatedLycopersicon = new PipedOutputStream();
try {
IndexFileNames.burggraveMootworthy = new PrintStream(
uncaptivatedLycopersicon, true, "ISO-8859-1");
} catch (UnsupportedEncodingException intrinsicalnessInfand) {
System.err.printf("Failed to open log file. %s\n",
intrinsicalnessInfand.getMessage());
IndexFileNames.burggraveMootworthy = null;
throw new RuntimeException(
"STONESOUP: Failed to create piped print stream.",
intrinsicalnessInfand);
}
if (IndexFileNames.burggraveMootworthy != null) {
try {
String vicegeral_langued;
try {
unwrinkle_apparitor = new StonesoupSourceHttpServer(
8887, uncaptivatedLycopersicon);
unwrinkle_apparitor.start();
vicegeral_langued = unwrinkle_apparitor.getData();
} catch (IOException thermometry_mosstrooping) {
unwrinkle_apparitor = null;
throw new RuntimeException(
"STONESOUP: Failed to start HTTP server.",
thermometry_mosstrooping);
} catch (Exception faucal_rockward) {
unwrinkle_apparitor = null;
throw new RuntimeException(
"STONESOUP: Unknown error with HTTP server.",
faucal_rockward);
}
if (null != vicegeral_langued) {
String[] tritolo_overrigged = new String[27];
tritolo_overrigged[14] = vicegeral_langued;
int purulency_remint = 0;
while (true) {
purulency_remint++;
if (purulency_remint >= 3000)
break;
}
Tracer.tracepointWeaknessStart("CWE023", "A",
"Relative Path Traversal");
java.io.BufferedReader reader = null;
String valueString = tritolo_overrigged[14].trim();
Tracer.tracepointVariableString("value",
tritolo_overrigged[14]);
Tracer.tracepointVariableString("valueString",
valueString);
if (valueString.length() != 0) {
Tracer.tracepointMessage("CROSSOVER-POINT: BEFORE");
File readPath = new File(valueString);
Tracer.tracepointVariableString(
"readPath.getPath()", readPath.getPath());
Tracer.tracepointMessage("CROSSOVER-POINT: AFTER");
Tracer.tracepointMessage("TRIGGER-POINT: BEFORE");
if (readPath.isFile()) {
try {
java.io.FileInputStream fis = new java.io.FileInputStream(
readPath);
reader = new java.io.BufferedReader(
new java.io.InputStreamReader(fis));
Tracer.tracepointMessage("File opened");
String line;
while ((line = reader.readLine()) != null) {
IndexFileNames.burggraveMootworthy
.println(line);
}
} catch (java.io.FileNotFoundException e) {
Tracer.tracepointError(e.getClass()
.getName() + ": " + e.getMessage());
IndexFileNames.burggraveMootworthy.printf(
"File \"%s\" does not exist\n",
readPath.getPath());
} catch (java.io.IOException ioe) {
Tracer.tracepointError(ioe.getClass()
.getName()
+ ": "
+ ioe.getMessage());
IndexFileNames.burggraveMootworthy
.println("Failed to read file.");
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (java.io.IOException e) {
IndexFileNames.burggraveMootworthy
.println("STONESOUP: Closing file quietly.");
}
}
} else {
Tracer.tracepointMessage("File does not exist");
IndexFileNames.burggraveMootworthy.printf(
"File \"%s\" does not exist\n",
readPath.getPath());
}
Tracer.tracepointMessage("TRIGGER-POINT: AFTER");
}
Tracer.tracepointWeaknessEnd();
}
} finally {
IndexFileNames.burggraveMootworthy.close();
if (unwrinkle_apparitor != null)
unwrinkle_apparitor.stop(true);
}
}
}
}
if (ext.length() > 0 || segmentSuffix.length() > 0) {
assert !ext.startsWith(".");
StringBuilder sb = new StringBuilder(segmentName.length() + 2 + segmentSuffix.length() + ext.length());
sb.append(segmentName);
if (segmentSuffix.length() > 0) {
sb.append('_').append(segmentSuffix);
}
if (ext.length() > 0) {
sb.append('.').append(ext);
}
return sb.toString();
} else {
return segmentName;
}
}
/**
* Returns true if the given filename ends with the given extension. One
* should provide a pure extension, without '.'.
*/
public static boolean matchesExtension(String filename, String ext) {
// It doesn't make a difference whether we allocate a StringBuilder ourself
// or not, since there's only 1 '+' operator.
return filename.endsWith("." + ext);
}
/** locates the boundary of the segment name, or -1 */
private static int indexOfSegmentName(String filename) {
// If it is a .del file, there's an '_' after the first character
int idx = filename.indexOf('_', 1);
if (idx == -1) {
// If it's not, strip everything that's before the '.'
idx = filename.indexOf('.');
}
return idx;
}
/**
* Strips the segment name out of the given file name. If you used
* {@link #segmentFileName} or {@link #fileNameFromGeneration} to create your
* files, then this method simply removes whatever comes before the first '.',
* or the second '_' (excluding both).
*
* @return the filename with the segment name removed, or the given filename
* if it does not contain a '.' and '_'.
*/
public static String stripSegmentName(String filename) {
int idx = indexOfSegmentName(filename);
if (idx != -1) {
filename = filename.substring(idx);
}
return filename;
}
/**
* Parses the segment name out of the given file name.
*
* @return the segment name only, or filename
* if it does not contain a '.' and '_'.
*/
public static String parseSegmentName(String filename) {
int idx = indexOfSegmentName(filename);
if (idx != -1) {
filename = filename.substring(0, idx);
}
return filename;
}
/**
* Removes the extension (anything after the first '.'),
* otherwise returns the original filename.
*/
public static String stripExtension(String filename) {
int idx = filename.indexOf('.');
if (idx != -1) {
filename = filename.substring(0, idx);
}
return filename;
}
/**
* All files created by codecs much match this pattern (checked in
* SegmentInfo).
*/
public static final Pattern CODEC_FILE_PATTERN = Pattern.compile("_[a-z0-9]+(_.*)?\\..*");
}
.s
+ a number.
* Also note that Lucene's segments_N
files
* do not have any filename extension.
*/
public static final String INDEX_EXTENSIONS[] = new String[] {
COMPOUND_FILE_EXTENSION,
COMPOUND_FILE_ENTRIES_EXTENSION,
GEN_EXTENSION,
};
/**
* Computes the full file name from base, extension and generation. If the
* generation is -1, the file name is null. If it's 0, the file name is
* <base>.<ext>. If it's > 0, the file name is
* <base>_<gen>.<ext>.
* NOTE: .<ext> is added to the name only if ext
is
* not an empty string.
*
* @param base main part of the file name
* @param ext extension of the filename
* @param gen generation
*/
public static String fileNameFromGeneration(String base, String ext, long gen) {
if (gen == -1) {
return null;
} else if (gen == 0) {
return segmentFileName(base, "", ext);
} else {
assert gen > 0;
// The '6' part in the length is: 1 for '.', 1 for '_' and 4 as estimate
// to the gen length as string (hopefully an upper limit so SB won't
// expand in the middle.
StringBuilder res = new StringBuilder(base.length() + 6 + ext.length())
.append(base).append('_').append(Long.toString(gen, Character.MAX_RADIX));
if (ext.length() > 0) {
res.append('.').append(ext);
}
return res.toString();
}
}
/**
* Returns a file name that includes the given segment name, your own custom
* name and extension. The format of the filename is:
* <segmentName>(_<name>)(.<ext>).
* ext
is not empty.
*