You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FS.java 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import java.io.BufferedReader;
  45. import java.io.ByteArrayInputStream;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.InputStreamReader;
  50. import java.io.OutputStream;
  51. import java.io.PrintStream;
  52. import java.nio.charset.Charset;
  53. import java.security.AccessController;
  54. import java.security.PrivilegedAction;
  55. import java.text.MessageFormat;
  56. import java.util.Arrays;
  57. import java.util.HashMap;
  58. import java.util.Map;
  59. import java.util.Objects;
  60. import java.util.concurrent.Callable;
  61. import java.util.concurrent.ExecutorService;
  62. import java.util.concurrent.Executors;
  63. import java.util.concurrent.TimeUnit;
  64. import java.util.concurrent.atomic.AtomicBoolean;
  65. import java.util.concurrent.atomic.AtomicReference;
  66. import org.eclipse.jgit.annotations.Nullable;
  67. import org.eclipse.jgit.api.errors.JGitInternalException;
  68. import org.eclipse.jgit.errors.CommandFailedException;
  69. import org.eclipse.jgit.internal.JGitText;
  70. import org.eclipse.jgit.lib.Constants;
  71. import org.eclipse.jgit.lib.Repository;
  72. import org.eclipse.jgit.util.ProcessResult.Status;
  73. import org.slf4j.Logger;
  74. import org.slf4j.LoggerFactory;
  75. /** Abstraction to support various file system operations not in Java. */
  76. public abstract class FS {
  77. /**
  78. * This class creates FS instances. It will be overridden by a Java7 variant
  79. * if such can be detected in {@link #detect(Boolean)}.
  80. *
  81. * @since 3.0
  82. */
  83. public static class FSFactory {
  84. /**
  85. * Constructor
  86. */
  87. protected FSFactory() {
  88. // empty
  89. }
  90. /**
  91. * Detect the file system
  92. *
  93. * @param cygwinUsed
  94. * @return FS instance
  95. */
  96. public FS detect(Boolean cygwinUsed) {
  97. if (SystemReader.getInstance().isWindows()) {
  98. if (cygwinUsed == null)
  99. cygwinUsed = Boolean.valueOf(FS_Win32_Cygwin.isCygwin());
  100. if (cygwinUsed.booleanValue())
  101. return new FS_Win32_Cygwin();
  102. else
  103. return new FS_Win32();
  104. } else {
  105. return new FS_POSIX();
  106. }
  107. }
  108. }
  109. /**
  110. * Result of an executed process. The caller is responsible to close the
  111. * contained {@link TemporaryBuffer}s
  112. *
  113. * @since 4.2
  114. */
  115. public static class ExecutionResult {
  116. private TemporaryBuffer stdout;
  117. private TemporaryBuffer stderr;
  118. private int rc;
  119. /**
  120. * @param stdout
  121. * @param stderr
  122. * @param rc
  123. */
  124. public ExecutionResult(TemporaryBuffer stdout, TemporaryBuffer stderr,
  125. int rc) {
  126. this.stdout = stdout;
  127. this.stderr = stderr;
  128. this.rc = rc;
  129. }
  130. /**
  131. * @return buffered standard output stream
  132. */
  133. public TemporaryBuffer getStdout() {
  134. return stdout;
  135. }
  136. /**
  137. * @return buffered standard error stream
  138. */
  139. public TemporaryBuffer getStderr() {
  140. return stderr;
  141. }
  142. /**
  143. * @return the return code of the process
  144. */
  145. public int getRc() {
  146. return rc;
  147. }
  148. }
  149. private final static Logger LOG = LoggerFactory.getLogger(FS.class);
  150. /** The auto-detected implementation selected for this operating system and JRE. */
  151. public static final FS DETECTED = detect();
  152. private volatile static FSFactory factory;
  153. /**
  154. * Auto-detect the appropriate file system abstraction.
  155. *
  156. * @return detected file system abstraction
  157. */
  158. public static FS detect() {
  159. return detect(null);
  160. }
  161. /**
  162. * Auto-detect the appropriate file system abstraction, taking into account
  163. * the presence of a Cygwin installation on the system. Using jgit in
  164. * combination with Cygwin requires a more elaborate (and possibly slower)
  165. * resolution of file system paths.
  166. *
  167. * @param cygwinUsed
  168. * <ul>
  169. * <li><code>Boolean.TRUE</code> to assume that Cygwin is used in
  170. * combination with jgit</li>
  171. * <li><code>Boolean.FALSE</code> to assume that Cygwin is
  172. * <b>not</b> used with jgit</li>
  173. * <li><code>null</code> to auto-detect whether a Cygwin
  174. * installation is present on the system and in this case assume
  175. * that Cygwin is used</li>
  176. * </ul>
  177. *
  178. * Note: this parameter is only relevant on Windows.
  179. *
  180. * @return detected file system abstraction
  181. */
  182. public static FS detect(Boolean cygwinUsed) {
  183. if (factory == null) {
  184. factory = new FS.FSFactory();
  185. }
  186. return factory.detect(cygwinUsed);
  187. }
  188. private volatile Holder<File> userHome;
  189. private volatile Holder<File> gitSystemConfig;
  190. /**
  191. * Constructs a file system abstraction.
  192. */
  193. protected FS() {
  194. // Do nothing by default.
  195. }
  196. /**
  197. * Initialize this FS using another's current settings.
  198. *
  199. * @param src
  200. * the source FS to copy from.
  201. */
  202. protected FS(FS src) {
  203. userHome = src.userHome;
  204. gitSystemConfig = src.gitSystemConfig;
  205. }
  206. /** @return a new instance of the same type of FS. */
  207. public abstract FS newInstance();
  208. /**
  209. * Does this operating system and JRE support the execute flag on files?
  210. *
  211. * @return true if this implementation can provide reasonably accurate
  212. * executable bit information; false otherwise.
  213. */
  214. public abstract boolean supportsExecute();
  215. /**
  216. * Does this file system support atomic file creation via
  217. * java.io.File#createNewFile()? In certain environments (e.g. on NFS) it is
  218. * not guaranteed that when two file system clients run createNewFile() in
  219. * parallel only one will succeed. In such cases both clients may think they
  220. * created a new file.
  221. *
  222. * @return true if this implementation support atomic creation of new
  223. * Files by {@link File#createNewFile()}
  224. * @since 4.5
  225. */
  226. public boolean supportsAtomicCreateNewFile() {
  227. return true;
  228. }
  229. /**
  230. * Does this operating system and JRE supports symbolic links. The
  231. * capability to handle symbolic links is detected at runtime.
  232. *
  233. * @return true if symbolic links may be used
  234. * @since 3.0
  235. */
  236. public boolean supportsSymlinks() {
  237. return false;
  238. }
  239. /**
  240. * Is this file system case sensitive
  241. *
  242. * @return true if this implementation is case sensitive
  243. */
  244. public abstract boolean isCaseSensitive();
  245. /**
  246. * Determine if the file is executable (or not).
  247. * <p>
  248. * Not all platforms and JREs support executable flags on files. If the
  249. * feature is unsupported this method will always return false.
  250. * <p>
  251. * <em>If the platform supports symbolic links and <code>f</code> is a symbolic link
  252. * this method returns false, rather than the state of the executable flags
  253. * on the target file.</em>
  254. *
  255. * @param f
  256. * abstract path to test.
  257. * @return true if the file is believed to be executable by the user.
  258. */
  259. public abstract boolean canExecute(File f);
  260. /**
  261. * Set a file to be executable by the user.
  262. * <p>
  263. * Not all platforms and JREs support executable flags on files. If the
  264. * feature is unsupported this method will always return false and no
  265. * changes will be made to the file specified.
  266. *
  267. * @param f
  268. * path to modify the executable status of.
  269. * @param canExec
  270. * true to enable execution; false to disable it.
  271. * @return true if the change succeeded; false otherwise.
  272. */
  273. public abstract boolean setExecute(File f, boolean canExec);
  274. /**
  275. * Get the last modified time of a file system object. If the OS/JRE support
  276. * symbolic links, the modification time of the link is returned, rather
  277. * than that of the link target.
  278. *
  279. * @param f
  280. * @return last modified time of f
  281. * @throws IOException
  282. * @since 3.0
  283. */
  284. public long lastModified(File f) throws IOException {
  285. return FileUtils.lastModified(f);
  286. }
  287. /**
  288. * Set the last modified time of a file system object. If the OS/JRE support
  289. * symbolic links, the link is modified, not the target,
  290. *
  291. * @param f
  292. * @param time
  293. * @throws IOException
  294. * @since 3.0
  295. */
  296. public void setLastModified(File f, long time) throws IOException {
  297. FileUtils.setLastModified(f, time);
  298. }
  299. /**
  300. * Get the length of a file or link, If the OS/JRE supports symbolic links
  301. * it's the length of the link, else the length of the target.
  302. *
  303. * @param path
  304. * @return length of a file
  305. * @throws IOException
  306. * @since 3.0
  307. */
  308. public long length(File path) throws IOException {
  309. return FileUtils.getLength(path);
  310. }
  311. /**
  312. * Delete a file. Throws an exception if delete fails.
  313. *
  314. * @param f
  315. * @throws IOException
  316. * this may be a Java7 subclass with detailed information
  317. * @since 3.3
  318. */
  319. public void delete(File f) throws IOException {
  320. FileUtils.delete(f);
  321. }
  322. /**
  323. * Resolve this file to its actual path name that the JRE can use.
  324. * <p>
  325. * This method can be relatively expensive. Computing a translation may
  326. * require forking an external process per path name translated. Callers
  327. * should try to minimize the number of translations necessary by caching
  328. * the results.
  329. * <p>
  330. * Not all platforms and JREs require path name translation. Currently only
  331. * Cygwin on Win32 require translation for Cygwin based paths.
  332. *
  333. * @param dir
  334. * directory relative to which the path name is.
  335. * @param name
  336. * path name to translate.
  337. * @return the translated path. <code>new File(dir,name)</code> if this
  338. * platform does not require path name translation.
  339. */
  340. public File resolve(final File dir, final String name) {
  341. final File abspn = new File(name);
  342. if (abspn.isAbsolute())
  343. return abspn;
  344. return new File(dir, name);
  345. }
  346. /**
  347. * Determine the user's home directory (location where preferences are).
  348. * <p>
  349. * This method can be expensive on the first invocation if path name
  350. * translation is required. Subsequent invocations return a cached result.
  351. * <p>
  352. * Not all platforms and JREs require path name translation. Currently only
  353. * Cygwin on Win32 requires translation of the Cygwin HOME directory.
  354. *
  355. * @return the user's home directory; null if the user does not have one.
  356. */
  357. public File userHome() {
  358. Holder<File> p = userHome;
  359. if (p == null) {
  360. p = new Holder<File>(userHomeImpl());
  361. userHome = p;
  362. }
  363. return p.value;
  364. }
  365. /**
  366. * Set the user's home directory location.
  367. *
  368. * @param path
  369. * the location of the user's preferences; null if there is no
  370. * home directory for the current user.
  371. * @return {@code this}.
  372. */
  373. public FS setUserHome(File path) {
  374. userHome = new Holder<File>(path);
  375. return this;
  376. }
  377. /**
  378. * Does this file system have problems with atomic renames?
  379. *
  380. * @return true if the caller should retry a failed rename of a lock file.
  381. */
  382. public abstract boolean retryFailedLockFileCommit();
  383. /**
  384. * Determine the user's home directory (location where preferences are).
  385. *
  386. * @return the user's home directory; null if the user does not have one.
  387. */
  388. protected File userHomeImpl() {
  389. final String home = AccessController
  390. .doPrivileged(new PrivilegedAction<String>() {
  391. public String run() {
  392. return System.getProperty("user.home"); //$NON-NLS-1$
  393. }
  394. });
  395. if (home == null || home.length() == 0)
  396. return null;
  397. return new File(home).getAbsoluteFile();
  398. }
  399. /**
  400. * Searches the given path to see if it contains one of the given files.
  401. * Returns the first it finds. Returns null if not found or if path is null.
  402. *
  403. * @param path
  404. * List of paths to search separated by File.pathSeparator
  405. * @param lookFor
  406. * Files to search for in the given path
  407. * @return the first match found, or null
  408. * @since 3.0
  409. **/
  410. protected static File searchPath(final String path, final String... lookFor) {
  411. if (path == null)
  412. return null;
  413. for (final String p : path.split(File.pathSeparator)) {
  414. for (String command : lookFor) {
  415. final File e = new File(p, command);
  416. if (e.isFile())
  417. return e.getAbsoluteFile();
  418. }
  419. }
  420. return null;
  421. }
  422. /**
  423. * Execute a command and return a single line of output as a String
  424. *
  425. * @param dir
  426. * Working directory for the command
  427. * @param command
  428. * as component array
  429. * @param encoding
  430. * to be used to parse the command's output
  431. * @return the one-line output of the command or {@code null} if there is
  432. * none
  433. * @throws CommandFailedException
  434. * thrown when the command failed (return code was non-zero)
  435. */
  436. @Nullable
  437. protected static String readPipe(File dir, String[] command,
  438. String encoding) throws CommandFailedException {
  439. return readPipe(dir, command, encoding, null);
  440. }
  441. /**
  442. * Execute a command and return a single line of output as a String
  443. *
  444. * @param dir
  445. * Working directory for the command
  446. * @param command
  447. * as component array
  448. * @param encoding
  449. * to be used to parse the command's output
  450. * @param env
  451. * Map of environment variables to be merged with those of the
  452. * current process
  453. * @return the one-line output of the command or {@code null} if there is
  454. * none
  455. * @throws CommandFailedException
  456. * thrown when the command failed (return code was non-zero)
  457. * @since 4.0
  458. */
  459. @Nullable
  460. protected static String readPipe(File dir, String[] command,
  461. String encoding, Map<String, String> env)
  462. throws CommandFailedException {
  463. final boolean debug = LOG.isDebugEnabled();
  464. try {
  465. if (debug) {
  466. LOG.debug("readpipe " + Arrays.asList(command) + "," //$NON-NLS-1$ //$NON-NLS-2$
  467. + dir);
  468. }
  469. ProcessBuilder pb = new ProcessBuilder(command);
  470. pb.directory(dir);
  471. if (env != null) {
  472. pb.environment().putAll(env);
  473. }
  474. Process p = pb.start();
  475. p.getOutputStream().close();
  476. GobblerThread gobbler = new GobblerThread(p, command, dir);
  477. gobbler.start();
  478. String r = null;
  479. try (BufferedReader lineRead = new BufferedReader(
  480. new InputStreamReader(p.getInputStream(), encoding))) {
  481. r = lineRead.readLine();
  482. if (debug) {
  483. LOG.debug("readpipe may return '" + r + "'"); //$NON-NLS-1$ //$NON-NLS-2$
  484. LOG.debug("remaining output:\n"); //$NON-NLS-1$
  485. String l;
  486. while ((l = lineRead.readLine()) != null) {
  487. LOG.debug(l);
  488. }
  489. }
  490. }
  491. for (;;) {
  492. try {
  493. int rc = p.waitFor();
  494. gobbler.join();
  495. if (rc == 0 && !gobbler.fail.get()) {
  496. return r;
  497. } else {
  498. if (debug) {
  499. LOG.debug("readpipe rc=" + rc); //$NON-NLS-1$
  500. }
  501. throw new CommandFailedException(rc,
  502. gobbler.errorMessage.get(),
  503. gobbler.exception.get());
  504. }
  505. } catch (InterruptedException ie) {
  506. // Stop bothering me, I have a zombie to reap.
  507. }
  508. }
  509. } catch (IOException e) {
  510. LOG.error("Caught exception in FS.readPipe()", e); //$NON-NLS-1$
  511. }
  512. if (debug) {
  513. LOG.debug("readpipe returns null"); //$NON-NLS-1$
  514. }
  515. return null;
  516. }
  517. private static class GobblerThread extends Thread {
  518. private final Process p;
  519. private final String desc;
  520. private final String dir;
  521. final AtomicBoolean fail = new AtomicBoolean();
  522. final AtomicReference<String> errorMessage = new AtomicReference<>();
  523. final AtomicReference<Throwable> exception = new AtomicReference<>();
  524. GobblerThread(Process p, String[] command, File dir) {
  525. this.p = p;
  526. this.desc = Arrays.toString(command);
  527. this.dir = Objects.toString(dir);
  528. }
  529. @Override
  530. public void run() {
  531. StringBuilder err = new StringBuilder();
  532. try (InputStream is = p.getErrorStream()) {
  533. int ch;
  534. while ((ch = is.read()) != -1) {
  535. err.append((char) ch);
  536. }
  537. } catch (IOException e) {
  538. if (p.exitValue() != 0) {
  539. setError(e, e.getMessage());
  540. fail.set(true);
  541. } else {
  542. // ignore. command terminated faster and stream was just closed
  543. }
  544. } finally {
  545. if (err.length() > 0) {
  546. setError(null, err.toString());
  547. if (p.exitValue() != 0) {
  548. fail.set(true);
  549. }
  550. }
  551. }
  552. }
  553. private void setError(IOException e, String message) {
  554. exception.set(e);
  555. errorMessage.set(MessageFormat.format(
  556. JGitText.get().exceptionCaughtDuringExcecutionOfCommand,
  557. desc, dir, Integer.valueOf(p.exitValue()), message));
  558. }
  559. }
  560. /**
  561. * @return the path to the Git executable or {@code null} if it cannot be
  562. * determined.
  563. * @since 4.0
  564. */
  565. protected abstract File discoverGitExe();
  566. /**
  567. * @return the path to the system-wide Git configuration file or
  568. * {@code null} if it cannot be determined.
  569. * @since 4.0
  570. */
  571. protected File discoverGitSystemConfig() {
  572. File gitExe = discoverGitExe();
  573. if (gitExe == null) {
  574. return null;
  575. }
  576. // Bug 480782: Check if the discovered git executable is JGit CLI
  577. String v;
  578. try {
  579. v = readPipe(gitExe.getParentFile(),
  580. new String[] { "git", "--version" }, //$NON-NLS-1$ //$NON-NLS-2$
  581. Charset.defaultCharset().name());
  582. } catch (CommandFailedException e) {
  583. LOG.warn(e.getMessage());
  584. return null;
  585. }
  586. if (StringUtils.isEmptyOrNull(v)
  587. || (v != null && v.startsWith("jgit"))) { //$NON-NLS-1$
  588. return null;
  589. }
  590. // Trick Git into printing the path to the config file by using "echo"
  591. // as the editor.
  592. Map<String, String> env = new HashMap<>();
  593. env.put("GIT_EDITOR", "echo"); //$NON-NLS-1$ //$NON-NLS-2$
  594. String w;
  595. try {
  596. w = readPipe(gitExe.getParentFile(),
  597. new String[] { "git", "config", "--system", "--edit" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  598. Charset.defaultCharset().name(), env);
  599. } catch (CommandFailedException e) {
  600. LOG.warn(e.getMessage());
  601. return null;
  602. }
  603. if (StringUtils.isEmptyOrNull(w)) {
  604. return null;
  605. }
  606. return new File(w);
  607. }
  608. /**
  609. * @return the currently used path to the system-wide Git configuration
  610. * file or {@code null} if none has been set.
  611. * @since 4.0
  612. */
  613. public File getGitSystemConfig() {
  614. if (gitSystemConfig == null) {
  615. gitSystemConfig = new Holder<File>(discoverGitSystemConfig());
  616. }
  617. return gitSystemConfig.value;
  618. }
  619. /**
  620. * Set the path to the system-wide Git configuration file to use.
  621. *
  622. * @param configFile
  623. * the path to the config file.
  624. * @return {@code this}
  625. * @since 4.0
  626. */
  627. public FS setGitSystemConfig(File configFile) {
  628. gitSystemConfig = new Holder<File>(configFile);
  629. return this;
  630. }
  631. /**
  632. * @param grandchild
  633. * @return the parent directory of this file's parent directory or
  634. * {@code null} in case there's no grandparent directory
  635. * @since 4.0
  636. */
  637. protected static File resolveGrandparentFile(File grandchild) {
  638. if (grandchild != null) {
  639. File parent = grandchild.getParentFile();
  640. if (parent != null)
  641. return parent.getParentFile();
  642. }
  643. return null;
  644. }
  645. /**
  646. * Check if a file is a symbolic link and read it
  647. *
  648. * @param path
  649. * @return target of link or null
  650. * @throws IOException
  651. * @since 3.0
  652. */
  653. public String readSymLink(File path) throws IOException {
  654. return FileUtils.readSymLink(path);
  655. }
  656. /**
  657. * @param path
  658. * @return true if the path is a symbolic link (and we support these)
  659. * @throws IOException
  660. * @since 3.0
  661. */
  662. public boolean isSymLink(File path) throws IOException {
  663. return FileUtils.isSymlink(path);
  664. }
  665. /**
  666. * Tests if the path exists, in case of a symbolic link, true even if the
  667. * target does not exist
  668. *
  669. * @param path
  670. * @return true if path exists
  671. * @since 3.0
  672. */
  673. public boolean exists(File path) {
  674. return FileUtils.exists(path);
  675. }
  676. /**
  677. * Check if path is a directory. If the OS/JRE supports symbolic links and
  678. * path is a symbolic link to a directory, this method returns false.
  679. *
  680. * @param path
  681. * @return true if file is a directory,
  682. * @since 3.0
  683. */
  684. public boolean isDirectory(File path) {
  685. return FileUtils.isDirectory(path);
  686. }
  687. /**
  688. * Examine if path represents a regular file. If the OS/JRE supports
  689. * symbolic links the test returns false if path represents a symbolic link.
  690. *
  691. * @param path
  692. * @return true if path represents a regular file
  693. * @since 3.0
  694. */
  695. public boolean isFile(File path) {
  696. return FileUtils.isFile(path);
  697. }
  698. /**
  699. * @param path
  700. * @return true if path is hidden, either starts with . on unix or has the
  701. * hidden attribute in windows
  702. * @throws IOException
  703. * @since 3.0
  704. */
  705. public boolean isHidden(File path) throws IOException {
  706. return FileUtils.isHidden(path);
  707. }
  708. /**
  709. * Set the hidden attribute for file whose name starts with a period.
  710. *
  711. * @param path
  712. * @param hidden
  713. * @throws IOException
  714. * @since 3.0
  715. */
  716. public void setHidden(File path, boolean hidden) throws IOException {
  717. FileUtils.setHidden(path, hidden);
  718. }
  719. /**
  720. * Create a symbolic link
  721. *
  722. * @param path
  723. * @param target
  724. * @throws IOException
  725. * @since 3.0
  726. */
  727. public void createSymLink(File path, String target) throws IOException {
  728. FileUtils.createSymLink(path, target);
  729. }
  730. /**
  731. * Create a new file. See {@link File#createNewFile()}. Subclasses of this
  732. * class may take care to provide a safe implementation for this even if
  733. * {@link #supportsAtomicCreateNewFile()} is <code>false</code>
  734. *
  735. * @param path
  736. * the file to be created
  737. * @return <code>true</code> if the file was created, <code>false</code> if
  738. * the file already existed
  739. * @throws IOException
  740. * @since 4.5
  741. */
  742. public boolean createNewFile(File path) throws IOException {
  743. return path.createNewFile();
  744. }
  745. /**
  746. * See {@link FileUtils#relativize(String, String)}.
  747. *
  748. * @param base
  749. * The path against which <code>other</code> should be
  750. * relativized.
  751. * @param other
  752. * The path that will be made relative to <code>base</code>.
  753. * @return A relative path that, when resolved against <code>base</code>,
  754. * will yield the original <code>other</code>.
  755. * @see FileUtils#relativize(String, String)
  756. * @since 3.7
  757. */
  758. public String relativize(String base, String other) {
  759. return FileUtils.relativize(base, other);
  760. }
  761. /**
  762. * Checks whether the given hook is defined for the given repository, then
  763. * runs it with the given arguments.
  764. * <p>
  765. * The hook's standard output and error streams will be redirected to
  766. * <code>System.out</code> and <code>System.err</code> respectively. The
  767. * hook will have no stdin.
  768. * </p>
  769. *
  770. * @param repository
  771. * The repository for which a hook should be run.
  772. * @param hookName
  773. * The name of the hook to be executed.
  774. * @param args
  775. * Arguments to pass to this hook. Cannot be <code>null</code>,
  776. * but can be an empty array.
  777. * @return The ProcessResult describing this hook's execution.
  778. * @throws JGitInternalException
  779. * if we fail to run the hook somehow. Causes may include an
  780. * interrupted process or I/O errors.
  781. * @since 4.0
  782. */
  783. public ProcessResult runHookIfPresent(Repository repository,
  784. final String hookName,
  785. String[] args) throws JGitInternalException {
  786. return runHookIfPresent(repository, hookName, args, System.out, System.err,
  787. null);
  788. }
  789. /**
  790. * Checks whether the given hook is defined for the given repository, then
  791. * runs it with the given arguments.
  792. *
  793. * @param repository
  794. * The repository for which a hook should be run.
  795. * @param hookName
  796. * The name of the hook to be executed.
  797. * @param args
  798. * Arguments to pass to this hook. Cannot be <code>null</code>,
  799. * but can be an empty array.
  800. * @param outRedirect
  801. * A print stream on which to redirect the hook's stdout. Can be
  802. * <code>null</code>, in which case the hook's standard output
  803. * will be lost.
  804. * @param errRedirect
  805. * A print stream on which to redirect the hook's stderr. Can be
  806. * <code>null</code>, in which case the hook's standard error
  807. * will be lost.
  808. * @param stdinArgs
  809. * A string to pass on to the standard input of the hook. May be
  810. * <code>null</code>.
  811. * @return The ProcessResult describing this hook's execution.
  812. * @throws JGitInternalException
  813. * if we fail to run the hook somehow. Causes may include an
  814. * interrupted process or I/O errors.
  815. * @since 4.0
  816. */
  817. public ProcessResult runHookIfPresent(Repository repository,
  818. final String hookName,
  819. String[] args, PrintStream outRedirect, PrintStream errRedirect,
  820. String stdinArgs) throws JGitInternalException {
  821. return new ProcessResult(Status.NOT_SUPPORTED);
  822. }
  823. /**
  824. * See
  825. * {@link #runHookIfPresent(Repository, String, String[], PrintStream, PrintStream, String)}
  826. * . Should only be called by FS supporting shell scripts execution.
  827. *
  828. * @param repository
  829. * The repository for which a hook should be run.
  830. * @param hookName
  831. * The name of the hook to be executed.
  832. * @param args
  833. * Arguments to pass to this hook. Cannot be <code>null</code>,
  834. * but can be an empty array.
  835. * @param outRedirect
  836. * A print stream on which to redirect the hook's stdout. Can be
  837. * <code>null</code>, in which case the hook's standard output
  838. * will be lost.
  839. * @param errRedirect
  840. * A print stream on which to redirect the hook's stderr. Can be
  841. * <code>null</code>, in which case the hook's standard error
  842. * will be lost.
  843. * @param stdinArgs
  844. * A string to pass on to the standard input of the hook. May be
  845. * <code>null</code>.
  846. * @return The ProcessResult describing this hook's execution.
  847. * @throws JGitInternalException
  848. * if we fail to run the hook somehow. Causes may include an
  849. * interrupted process or I/O errors.
  850. * @since 4.0
  851. */
  852. protected ProcessResult internalRunHookIfPresent(Repository repository,
  853. final String hookName, String[] args, PrintStream outRedirect,
  854. PrintStream errRedirect, String stdinArgs)
  855. throws JGitInternalException {
  856. final File hookFile = findHook(repository, hookName);
  857. if (hookFile == null)
  858. return new ProcessResult(Status.NOT_PRESENT);
  859. final String hookPath = hookFile.getAbsolutePath();
  860. final File runDirectory;
  861. if (repository.isBare())
  862. runDirectory = repository.getDirectory();
  863. else
  864. runDirectory = repository.getWorkTree();
  865. final String cmd = relativize(runDirectory.getAbsolutePath(),
  866. hookPath);
  867. ProcessBuilder hookProcess = runInShell(cmd, args);
  868. hookProcess.directory(runDirectory);
  869. try {
  870. return new ProcessResult(runProcess(hookProcess, outRedirect,
  871. errRedirect, stdinArgs), Status.OK);
  872. } catch (IOException e) {
  873. throw new JGitInternalException(MessageFormat.format(
  874. JGitText.get().exceptionCaughtDuringExecutionOfHook,
  875. hookName), e);
  876. } catch (InterruptedException e) {
  877. throw new JGitInternalException(MessageFormat.format(
  878. JGitText.get().exceptionHookExecutionInterrupted,
  879. hookName), e);
  880. }
  881. }
  882. /**
  883. * Tries to find a hook matching the given one in the given repository.
  884. *
  885. * @param repository
  886. * The repository within which to find a hook.
  887. * @param hookName
  888. * The name of the hook we're trying to find.
  889. * @return The {@link File} containing this particular hook if it exists in
  890. * the given repository, <code>null</code> otherwise.
  891. * @since 4.0
  892. */
  893. public File findHook(Repository repository, final String hookName) {
  894. File gitDir = repository.getDirectory();
  895. if (gitDir == null)
  896. return null;
  897. final File hookFile = new File(new File(gitDir,
  898. Constants.HOOKS), hookName);
  899. return hookFile.isFile() ? hookFile : null;
  900. }
  901. /**
  902. * Runs the given process until termination, clearing its stdout and stderr
  903. * streams on-the-fly.
  904. *
  905. * @param processBuilder
  906. * The process builder configured for this process.
  907. * @param outRedirect
  908. * A OutputStream on which to redirect the processes stdout. Can
  909. * be <code>null</code>, in which case the processes standard
  910. * output will be lost.
  911. * @param errRedirect
  912. * A OutputStream on which to redirect the processes stderr. Can
  913. * be <code>null</code>, in which case the processes standard
  914. * error will be lost.
  915. * @param stdinArgs
  916. * A string to pass on to the standard input of the hook. Can be
  917. * <code>null</code>.
  918. * @return the exit value of this process.
  919. * @throws IOException
  920. * if an I/O error occurs while executing this process.
  921. * @throws InterruptedException
  922. * if the current thread is interrupted while waiting for the
  923. * process to end.
  924. * @since 4.2
  925. */
  926. public int runProcess(ProcessBuilder processBuilder,
  927. OutputStream outRedirect, OutputStream errRedirect, String stdinArgs)
  928. throws IOException, InterruptedException {
  929. InputStream in = (stdinArgs == null) ? null : new ByteArrayInputStream(
  930. stdinArgs.getBytes(Constants.CHARACTER_ENCODING));
  931. return runProcess(processBuilder, outRedirect, errRedirect, in);
  932. }
  933. /**
  934. * Runs the given process until termination, clearing its stdout and stderr
  935. * streams on-the-fly.
  936. *
  937. * @param processBuilder
  938. * The process builder configured for this process.
  939. * @param outRedirect
  940. * An OutputStream on which to redirect the processes stdout. Can
  941. * be <code>null</code>, in which case the processes standard
  942. * output will be lost.
  943. * @param errRedirect
  944. * An OutputStream on which to redirect the processes stderr. Can
  945. * be <code>null</code>, in which case the processes standard
  946. * error will be lost.
  947. * @param inRedirect
  948. * An InputStream from which to redirect the processes stdin. Can
  949. * be <code>null</code>, in which case the process doesn't get
  950. * any data over stdin. It is assumed that the whole InputStream
  951. * will be consumed by the process. The method will close the
  952. * inputstream after all bytes are read.
  953. * @return the return code of this process.
  954. * @throws IOException
  955. * if an I/O error occurs while executing this process.
  956. * @throws InterruptedException
  957. * if the current thread is interrupted while waiting for the
  958. * process to end.
  959. * @since 4.2
  960. */
  961. public int runProcess(ProcessBuilder processBuilder,
  962. OutputStream outRedirect, OutputStream errRedirect,
  963. InputStream inRedirect) throws IOException,
  964. InterruptedException {
  965. final ExecutorService executor = Executors.newFixedThreadPool(2);
  966. Process process = null;
  967. // We'll record the first I/O exception that occurs, but keep on trying
  968. // to dispose of our open streams and file handles
  969. IOException ioException = null;
  970. try {
  971. process = processBuilder.start();
  972. final Callable<Void> errorGobbler = new StreamGobbler(
  973. process.getErrorStream(), errRedirect);
  974. final Callable<Void> outputGobbler = new StreamGobbler(
  975. process.getInputStream(), outRedirect);
  976. executor.submit(errorGobbler);
  977. executor.submit(outputGobbler);
  978. OutputStream outputStream = process.getOutputStream();
  979. if (inRedirect != null) {
  980. new StreamGobbler(inRedirect, outputStream)
  981. .call();
  982. }
  983. try {
  984. outputStream.close();
  985. } catch (IOException e) {
  986. // When the process exits before consuming the input, the OutputStream
  987. // is replaced with the null output stream. This null output stream
  988. // throws IOException for all write calls. When StreamGobbler fails to
  989. // flush the buffer because of this, this close call tries to flush it
  990. // again. This causes another IOException. Since we ignore the
  991. // IOException in StreamGobbler, we also ignore the exception here.
  992. }
  993. return process.waitFor();
  994. } catch (IOException e) {
  995. ioException = e;
  996. } finally {
  997. shutdownAndAwaitTermination(executor);
  998. if (process != null) {
  999. try {
  1000. process.waitFor();
  1001. } catch (InterruptedException e) {
  1002. // Thrown by the outer try.
  1003. // Swallow this one to carry on our cleanup, and clear the
  1004. // interrupted flag (processes throw the exception without
  1005. // clearing the flag).
  1006. Thread.interrupted();
  1007. }
  1008. // A process doesn't clean its own resources even when destroyed
  1009. // Explicitly try and close all three streams, preserving the
  1010. // outer I/O exception if any.
  1011. if (inRedirect != null) {
  1012. inRedirect.close();
  1013. }
  1014. try {
  1015. process.getErrorStream().close();
  1016. } catch (IOException e) {
  1017. ioException = ioException != null ? ioException : e;
  1018. }
  1019. try {
  1020. process.getInputStream().close();
  1021. } catch (IOException e) {
  1022. ioException = ioException != null ? ioException : e;
  1023. }
  1024. try {
  1025. process.getOutputStream().close();
  1026. } catch (IOException e) {
  1027. ioException = ioException != null ? ioException : e;
  1028. }
  1029. process.destroy();
  1030. }
  1031. }
  1032. // We can only be here if the outer try threw an IOException.
  1033. throw ioException;
  1034. }
  1035. /**
  1036. * Shuts down an {@link ExecutorService} in two phases, first by calling
  1037. * {@link ExecutorService#shutdown() shutdown} to reject incoming tasks, and
  1038. * then calling {@link ExecutorService#shutdownNow() shutdownNow}, if
  1039. * necessary, to cancel any lingering tasks. Returns true if the pool has
  1040. * been properly shutdown, false otherwise.
  1041. * <p>
  1042. *
  1043. * @param pool
  1044. * the pool to shutdown
  1045. * @return <code>true</code> if the pool has been properly shutdown,
  1046. * <code>false</code> otherwise.
  1047. */
  1048. private static boolean shutdownAndAwaitTermination(ExecutorService pool) {
  1049. boolean hasShutdown = true;
  1050. pool.shutdown(); // Disable new tasks from being submitted
  1051. try {
  1052. // Wait a while for existing tasks to terminate
  1053. if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
  1054. pool.shutdownNow(); // Cancel currently executing tasks
  1055. // Wait a while for tasks to respond to being canceled
  1056. if (!pool.awaitTermination(60, TimeUnit.SECONDS))
  1057. hasShutdown = false;
  1058. }
  1059. } catch (InterruptedException ie) {
  1060. // (Re-)Cancel if current thread also interrupted
  1061. pool.shutdownNow();
  1062. // Preserve interrupt status
  1063. Thread.currentThread().interrupt();
  1064. hasShutdown = false;
  1065. }
  1066. return hasShutdown;
  1067. }
  1068. /**
  1069. * Initialize a ProcessBuilder to run a command using the system shell.
  1070. *
  1071. * @param cmd
  1072. * command to execute. This string should originate from the
  1073. * end-user, and thus is platform specific.
  1074. * @param args
  1075. * arguments to pass to command. These should be protected from
  1076. * shell evaluation.
  1077. * @return a partially completed process builder. Caller should finish
  1078. * populating directory, environment, and then start the process.
  1079. */
  1080. public abstract ProcessBuilder runInShell(String cmd, String[] args);
  1081. /**
  1082. * Execute a command defined by a {@link ProcessBuilder}.
  1083. *
  1084. * @param pb
  1085. * The command to be executed
  1086. * @param in
  1087. * The standard input stream passed to the process
  1088. * @return The result of the executed command
  1089. * @throws InterruptedException
  1090. * @throws IOException
  1091. * @since 4.2
  1092. */
  1093. public ExecutionResult execute(ProcessBuilder pb, InputStream in)
  1094. throws IOException, InterruptedException {
  1095. TemporaryBuffer stdout = new TemporaryBuffer.LocalFile(null);
  1096. TemporaryBuffer stderr = new TemporaryBuffer.Heap(1024, 1024 * 1024);
  1097. try {
  1098. int rc = runProcess(pb, stdout, stderr, in);
  1099. return new ExecutionResult(stdout, stderr, rc);
  1100. } finally {
  1101. stdout.close();
  1102. stderr.close();
  1103. }
  1104. }
  1105. private static class Holder<V> {
  1106. final V value;
  1107. Holder(V value) {
  1108. this.value = value;
  1109. }
  1110. }
  1111. /**
  1112. * File attributes we typically care for.
  1113. *
  1114. * @since 3.3
  1115. */
  1116. public static class Attributes {
  1117. /**
  1118. * @return true if this are the attributes of a directory
  1119. */
  1120. public boolean isDirectory() {
  1121. return isDirectory;
  1122. }
  1123. /**
  1124. * @return true if this are the attributes of an executable file
  1125. */
  1126. public boolean isExecutable() {
  1127. return isExecutable;
  1128. }
  1129. /**
  1130. * @return true if this are the attributes of a symbolic link
  1131. */
  1132. public boolean isSymbolicLink() {
  1133. return isSymbolicLink;
  1134. }
  1135. /**
  1136. * @return true if this are the attributes of a regular file
  1137. */
  1138. public boolean isRegularFile() {
  1139. return isRegularFile;
  1140. }
  1141. /**
  1142. * @return the time when the file was created
  1143. */
  1144. public long getCreationTime() {
  1145. return creationTime;
  1146. }
  1147. /**
  1148. * @return the time (milliseconds since 1970-01-01) when this object was
  1149. * last modified
  1150. */
  1151. public long getLastModifiedTime() {
  1152. return lastModifiedTime;
  1153. }
  1154. private final boolean isDirectory;
  1155. private final boolean isSymbolicLink;
  1156. private final boolean isRegularFile;
  1157. private final long creationTime;
  1158. private final long lastModifiedTime;
  1159. private final boolean isExecutable;
  1160. private final File file;
  1161. private final boolean exists;
  1162. /**
  1163. * file length
  1164. */
  1165. protected long length = -1;
  1166. final FS fs;
  1167. Attributes(FS fs, File file, boolean exists, boolean isDirectory,
  1168. boolean isExecutable, boolean isSymbolicLink,
  1169. boolean isRegularFile, long creationTime,
  1170. long lastModifiedTime, long length) {
  1171. this.fs = fs;
  1172. this.file = file;
  1173. this.exists = exists;
  1174. this.isDirectory = isDirectory;
  1175. this.isExecutable = isExecutable;
  1176. this.isSymbolicLink = isSymbolicLink;
  1177. this.isRegularFile = isRegularFile;
  1178. this.creationTime = creationTime;
  1179. this.lastModifiedTime = lastModifiedTime;
  1180. this.length = length;
  1181. }
  1182. /**
  1183. * Constructor when there are issues with reading. All attributes except
  1184. * given will be set to the default values.
  1185. *
  1186. * @param fs
  1187. * @param path
  1188. */
  1189. public Attributes(File path, FS fs) {
  1190. this(fs, path, false, false, false, false, false, 0L, 0L, 0L);
  1191. }
  1192. /**
  1193. * @return length of this file object
  1194. */
  1195. public long getLength() {
  1196. if (length == -1)
  1197. return length = file.length();
  1198. return length;
  1199. }
  1200. /**
  1201. * @return the filename
  1202. */
  1203. public String getName() {
  1204. return file.getName();
  1205. }
  1206. /**
  1207. * @return the file the attributes apply to
  1208. */
  1209. public File getFile() {
  1210. return file;
  1211. }
  1212. boolean exists() {
  1213. return exists;
  1214. }
  1215. }
  1216. /**
  1217. * @param path
  1218. * @return the file attributes we care for
  1219. * @since 3.3
  1220. */
  1221. public Attributes getAttributes(File path) {
  1222. boolean isDirectory = isDirectory(path);
  1223. boolean isFile = !isDirectory && path.isFile();
  1224. assert path.exists() == isDirectory || isFile;
  1225. boolean exists = isDirectory || isFile;
  1226. boolean canExecute = exists && !isDirectory && canExecute(path);
  1227. boolean isSymlink = false;
  1228. long lastModified = exists ? path.lastModified() : 0L;
  1229. long createTime = 0L;
  1230. return new Attributes(this, path, exists, isDirectory, canExecute,
  1231. isSymlink, isFile, createTime, lastModified, -1);
  1232. }
  1233. /**
  1234. * Normalize the unicode path to composed form.
  1235. *
  1236. * @param file
  1237. * @return NFC-format File
  1238. * @since 3.3
  1239. */
  1240. public File normalize(File file) {
  1241. return file;
  1242. }
  1243. /**
  1244. * Normalize the unicode path to composed form.
  1245. *
  1246. * @param name
  1247. * @return NFC-format string
  1248. * @since 3.3
  1249. */
  1250. public String normalize(String name) {
  1251. return name;
  1252. }
  1253. /**
  1254. * This runnable will consume an input stream's content into an output
  1255. * stream as soon as it gets available.
  1256. * <p>
  1257. * Typically used to empty processes' standard output and error, preventing
  1258. * them to choke.
  1259. * </p>
  1260. * <p>
  1261. * <b>Note</b> that a {@link StreamGobbler} will never close either of its
  1262. * streams.
  1263. * </p>
  1264. */
  1265. private static class StreamGobbler implements Callable<Void> {
  1266. private InputStream in;
  1267. private OutputStream out;
  1268. public StreamGobbler(InputStream stream, OutputStream output) {
  1269. this.in = stream;
  1270. this.out = output;
  1271. }
  1272. public Void call() throws IOException {
  1273. boolean writeFailure = false;
  1274. byte buffer[] = new byte[4096];
  1275. int readBytes;
  1276. while ((readBytes = in.read(buffer)) != -1) {
  1277. // Do not try to write again after a failure, but keep
  1278. // reading as long as possible to prevent the input stream
  1279. // from choking.
  1280. if (!writeFailure && out != null) {
  1281. try {
  1282. out.write(buffer, 0, readBytes);
  1283. out.flush();
  1284. } catch (IOException e) {
  1285. writeFailure = true;
  1286. }
  1287. }
  1288. }
  1289. return null;
  1290. }
  1291. }
  1292. }