PageRenderTime 43ms CodeModel.GetById 15ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.ql.util;
  19. import java.io.BufferedReader;
  20. import java.io.BufferedWriter;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileReader;
  24. import java.io.FileWriter;
  25. import java.io.InputStreamReader;
  26. public class DosToUnix {
  27. public static String convertWindowsScriptToUnix(File windowsScriptFile) throws Exception {
  28. String windowsScriptFilename = windowsScriptFile.getName();
  29. String unixScriptFilename = getUnixScriptNameFor(windowsScriptFilename);
  30. File unixScriptFile = null;
  31. if (windowsScriptFile.getParent() != null) {
  32. unixScriptFile = new File(windowsScriptFile.getParent() + "/" + unixScriptFilename);
  33. } else {
  34. unixScriptFile = new File(unixScriptFilename);
  35. }
  36. BufferedWriter writer = new BufferedWriter(new FileWriter(unixScriptFile));
  37. try {
  38. BufferedReader reader = new BufferedReader(new FileReader(windowsScriptFile));
  39. try {
  40. int prev = reader.read();
  41. int next = reader.read();
  42. while( prev != -1 ) {
  43. if ( prev != -1 && ( prev != '\r' || next != '\n' ) ) {
  44. writer.write(prev);
  45. }
  46. prev = next;
  47. next = reader.read();
  48. }
  49. }
  50. finally {
  51. reader.close();
  52. }
  53. }
  54. finally {
  55. writer.close();
  56. }
  57. unixScriptFile.setExecutable(true);
  58. return unixScriptFile.getAbsolutePath();
  59. }
  60. public static String getUnixScriptNameFor(String windowsScriptFilename) {
  61. int pos = windowsScriptFilename.indexOf(".");
  62. String unixScriptFilename;
  63. if ( pos >= 0 ) {
  64. unixScriptFilename = windowsScriptFilename.substring(0, pos) + "_unix" + windowsScriptFilename.substring(pos);
  65. }
  66. else {
  67. unixScriptFilename = windowsScriptFilename + "_unix";
  68. }
  69. return unixScriptFilename;
  70. }
  71. public static boolean isWindowsScript(File file) {
  72. try {
  73. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
  74. char[] buffer = new char[4096];
  75. int readLength = reader.read(buffer);
  76. if (readLength >= 2 && buffer[0] == '#' && buffer[1] == '!') {
  77. for(int i=2; i<readLength; ++i) {
  78. switch(buffer[i]) {
  79. case '\r':
  80. return true;
  81. case '\n':
  82. return false;
  83. }
  84. }
  85. }
  86. } catch (Exception e) {
  87. // ignore error reading file, if we can't read it then it isn't a windows script
  88. }
  89. return false;
  90. }
  91. }