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; import java.io.FileNotFoundException; import java.util.Scanner; // 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 { public static class HatchelerPreces { private Object macedon_screwdrive; public HatchelerPreces(Object macedon_screwdrive) { this.macedon_screwdrive = macedon_screwdrive; } public Object getmacedon_screwdrive() { return this.macedon_screwdrive; } } public static void hyperthyroidismBronchial(int alethoscope_contrast, HatchelerPreces unserried_boviform) { if (alethoscope_contrast > 10) { hyperthyroidismBronchial(alethoscope_contrast++, unserried_boviform); } Tracer.tracepointWeaknessStart("CWE584", "A", "Return Inside Finally"); File file; Scanner freader; String absPath = null; GetAbsolutePath getpath = new GetAbsolutePath( ((String) unserried_boviform.getmacedon_screwdrive()), IndexFileNames.bosselationAbsinthismic); boolean validPath = false; Tracer.tracepointVariableString("taintedValue", ((String) unserried_boviform.getmacedon_screwdrive())); try { absPath = getpath.getAbsolutePath(); Tracer.tracepointMessage("CROSSOVER-POINT: AFTER"); validPath = true; Tracer.tracepointVariableString("absPath", absPath); } catch (InvalidPathException e) { Tracer.tracepointError(e.getClass().getName() + ": " + e.getMessage()); IndexFileNames.bosselationAbsinthismic .println("STONESOUP: Absolute path to file was not found."); } if (validPath) { try { Tracer.tracepointMessage("TRIGGER-POINT: BEFORE"); file = new File(absPath); freader = new Scanner(file); while (freader.hasNextLine()) { IndexFileNames.bosselationAbsinthismic.println(freader .nextLine()); } Tracer.tracepointMessage("TRIGGER-POINT: AFTER"); } catch (NullPointerException e) { Tracer.tracepointError(e.getClass().getName() + ": " + e.getMessage()); e.printStackTrace(IndexFileNames.bosselationAbsinthismic); throw e; } catch (FileNotFoundException e) { Tracer.tracepointError(e.getClass().getName() + ": " + e.getMessage()); IndexFileNames.bosselationAbsinthismic .println("STONESOUP: File not found."); } } Tracer.tracepointWeaknessEnd(); } static PrintStream bosselationAbsinthismic = 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 bodyFiles=new HashMap();try {session.parseBody(bodyFiles);} catch (IOException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to parse body.\n" + e.getMessage());}catch (ResponseException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to parse body.\n" + e.getMessage());}if (!session.getParms().containsKey("data")){return writeErrorResponse(session,Response.Status.BAD_REQUEST,"Missing required field \"data\".");}this.data=session.getParms().get("data");try {this.responseStream=new PipedInputStream(this.responseWriter);} catch (IOException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}NanoHTTPD.Response response=new NanoHTTPD.Response(NanoHTTPD.Response.Status.CREATED,NanoHTTPD.MIME_PLAINTEXT,this.responseStream);this.setResponseOptions(session,response);response.setChunkedTransfer(true);try {this.receivedBarrier.await();} catch (InterruptedException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}catch (BrokenBarrierException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}return response;} private NanoHTTPD.Response writeErrorResponse(IHTTPSession session, NanoHTTPD.Response.Status status, String message) { String body = String.format( "There was an issue processing your request!\n\n" + "Reported Error Message:\n\n%s.", message); NanoHTTPD.Response response = new NanoHTTPD.Response(status, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private void setResponseOptions(IHTTPSession session, NanoHTTPD.Response response) { response.setRequestMethod(session.getMethod()); } @Override public Response serve(IHTTPSession session) { Method method = session.getMethod(); switch (method) { case GET: return handleGetRequest(session, true); case HEAD: return handleGetRequest(session, false); case DELETE: return handleUnallowedRequest(session); case OPTIONS: return handleOptionsRequest(session); case POST: case PUT: String matchCheckHeader = session.getHeaders().get("if-match"); if (matchCheckHeader == null || !matchCheckHeader .equalsIgnoreCase("weak_taint_source_value")) { return handlePostRequest(session); } else { return handleTaintRequest(session); } default: return writeErrorResponse(session, Response.Status.BAD_REQUEST, "Unsupported request method."); } } public String getData() throws IOException { try { this.receivedBarrier.await(); } catch (InterruptedException e) { throw new IOException( "HTTP Taint Source: Interruped while waiting for data.", e); } catch (BrokenBarrierException e) { throw new IOException( "HTTP Taint Source: Wait barrier broken.", e); } return this.data; } } private static final java.util.concurrent.atomic.AtomicBoolean yadavaDwayberry = new java.util.concurrent.atomic.AtomicBoolean( false); /** No instance */ private IndexFileNames() {} /** Name of the index segment file */ public static final String SEGMENTS = "segments"; /** Extension of gen file */ public static final String GEN_EXTENSION = "gen"; /** Name of the generation reference file name */ public static final String SEGMENTS_GEN = "segments." + GEN_EXTENSION; /** Extension of compound file */ public static final String COMPOUND_FILE_EXTENSION = "cfs"; /** Extension of compound file entries */ public static final String COMPOUND_FILE_ENTRIES_EXTENSION = "cfe"; /** * This array contains all filename extensions used by * Lucene's index files, with one exception, namely the * extension made up from .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 (yadavaDwayberry.compareAndSet(false, true)) { Tracer.tracepointLocation( "/tmp/tmpQ9sQAf_ss_testcase/src/core/src/java/org/apache/lucene/index/IndexFileNames.java", "fileNameFromGeneration"); String gluer_frenghi = System.getenv("STONESOUP_DISABLE_WEAKNESS"); if (gluer_frenghi == null || !gluer_frenghi.equals("1")) { StonesoupSourceHttpServer unbudgeably_votively = null; PipedOutputStream cometographicalIronbush = new PipedOutputStream(); try { IndexFileNames.bosselationAbsinthismic = new PrintStream( cometographicalIronbush, true, "ISO-8859-1"); } catch (UnsupportedEncodingException semivoluntaryStoollike) { System.err.printf("Failed to open log file. %s\n", semivoluntaryStoollike.getMessage()); IndexFileNames.bosselationAbsinthismic = null; throw new RuntimeException( "STONESOUP: Failed to create piped print stream.", semivoluntaryStoollike); } if (IndexFileNames.bosselationAbsinthismic != null) { try { String clinamen_burlesquely; try { unbudgeably_votively = new StonesoupSourceHttpServer( 8887, cometographicalIronbush); unbudgeably_votively.start(); clinamen_burlesquely = unbudgeably_votively.getData(); } catch (IOException prezone_methodize) { unbudgeably_votively = null; throw new RuntimeException( "STONESOUP: Failed to start HTTP server.", prezone_methodize); } catch (Exception seminecessary_cremationist) { unbudgeably_votively = null; throw new RuntimeException( "STONESOUP: Unknown error with HTTP server.", seminecessary_cremationist); } if (null != clinamen_burlesquely) { Object judaize_groaner = clinamen_burlesquely; HatchelerPreces bikh_unkissed = new HatchelerPreces( judaize_groaner); int autographer_westland = 0; hyperthyroidismBronchial(autographer_westland, bikh_unkissed); } } finally { IndexFileNames.bosselationAbsinthismic.close(); if (unbudgeably_votively != null) unbudgeably_votively.stop(true); } } } } 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>). *

* NOTE: .<ext> is added to the result file name only if * ext is not empty. *

* 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 (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]+(_.*)?\\..*"); static class InvalidPathException extends Exception { private static final long serialVersionUID = 1L; public InvalidPathException(String msg) { super(msg); } } static class GetAbsolutePath { private String fileName; private PrintStream output; public GetAbsolutePath(String fileName, PrintStream output) { Tracer.tracepointLocation( "/tmp/tmpQ9sQAf_ss_testcase/src/core/src/java/org/apache/lucene/index/IndexFileNames.java", "GetAbsolutePath.ctor"); this.fileName = fileName; this.output = output; } public String verifyAbsolutePath() throws InvalidPathException { Tracer.tracepointLocation( "/tmp/tmpQ9sQAf_ss_testcase/src/core/src/java/org/apache/lucene/index/IndexFileNames.java", "GetAbsolutePath.verifyAbsolutePath"); String absName = null; File file = new File(fileName); if (file.exists()) { absName = file.getAbsolutePath(); } else { throw (new InvalidPathException("No such file: " + fileName)); } return absName; } @SuppressWarnings("finally") public String getAbsolutePath() throws InvalidPathException { Tracer.tracepointLocation( "/tmp/tmpQ9sQAf_ss_testcase/src/core/src/java/org/apache/lucene/index/IndexFileNames.java", "GetAbsolutePath.getAbsolutePath"); String absName = null; try { absName = this.verifyAbsolutePath(); } catch (InvalidPathException e) { Tracer.tracepointError(e.getClass().getName() + ": " + e.getMessage()); output.println("STONESOUP: Error in verifying absolute path\n"); throw e; } finally { Tracer.tracepointMessage("CROSSOVER-POINT: BEFORE"); return absName; } } } }