PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/util/DosToUnix.java

#
Java | 98 lines | 73 code | 7 blank | 18 comment | 18 complexity | e76f4f6a82363318e7ea07c2917ee658 MD5 | raw file
 1/**
 2 * Licensed to the Apache Software Foundation (ASF) under one
 3 * or more contributor license agreements.  See the NOTICE file
 4 * distributed with this work for additional information
 5 * regarding copyright ownership.  The ASF licenses this file
 6 * to you under the Apache License, Version 2.0 (the
 7 * "License"); you may not use this file except in compliance
 8 * with the License.  You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.hive.ql.util;
20
21import java.io.BufferedReader;
22import java.io.BufferedWriter;
23import java.io.File;
24import java.io.FileInputStream;
25import java.io.FileReader;
26import java.io.FileWriter;
27import java.io.InputStreamReader;
28
29public class DosToUnix {
30
31  public static String convertWindowsScriptToUnix(File windowsScriptFile) throws Exception {
32    String windowsScriptFilename = windowsScriptFile.getName();
33    String unixScriptFilename = getUnixScriptNameFor(windowsScriptFilename);
34    File unixScriptFile = null;
35    if (windowsScriptFile.getParent() != null) {
36      unixScriptFile = new File(windowsScriptFile.getParent() + "/" + unixScriptFilename);
37    } else {
38      unixScriptFile = new File(unixScriptFilename);
39    }
40    BufferedWriter writer = new BufferedWriter(new FileWriter(unixScriptFile));
41    try {
42      BufferedReader reader = new BufferedReader(new FileReader(windowsScriptFile));
43      try {
44        int prev = reader.read();
45        int next = reader.read();
46        while( prev != -1 ) {
47          if ( prev != -1 && ( prev != '\r' || next != '\n' ) ) {
48            writer.write(prev);
49          }
50          prev = next;
51          next = reader.read();
52        }
53      }
54      finally {
55        reader.close();
56      }
57    }
58    finally {
59      writer.close();
60    }
61    unixScriptFile.setExecutable(true);
62    return unixScriptFile.getAbsolutePath();
63  }
64
65  public static String getUnixScriptNameFor(String windowsScriptFilename) {
66    int pos = windowsScriptFilename.indexOf(".");
67    String unixScriptFilename;
68    if ( pos >= 0 ) {
69      unixScriptFilename = windowsScriptFilename.substring(0, pos) + "_unix" + windowsScriptFilename.substring(pos);
70    }
71    else {
72      unixScriptFilename = windowsScriptFilename + "_unix";
73    }
74    return unixScriptFilename;
75  }
76
77  public static boolean isWindowsScript(File file) {
78    try {
79      BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
80      char[] buffer = new char[4096];
81      int readLength = reader.read(buffer);
82      if (readLength >= 2 && buffer[0] == '#' && buffer[1] == '!') {
83        for(int i=2; i<readLength; ++i) {
84          switch(buffer[i]) {
85          case '\r':
86            return true;
87          case '\n':
88            return false;
89          }
90        }
91      }
92    } catch (Exception e) {
93      // ignore error reading file, if we can't read it then it isn't a windows script
94    }
95    return false;
96  }
97
98}