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.

DirCacheEntry.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com>
  5. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.dircache;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.EOFException;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.OutputStream;
  52. import java.nio.ByteBuffer;
  53. import java.security.MessageDigest;
  54. import java.text.MessageFormat;
  55. import java.util.Arrays;
  56. import org.eclipse.jgit.JGitText;
  57. import org.eclipse.jgit.lib.AnyObjectId;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.lib.FileMode;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.util.IO;
  62. import org.eclipse.jgit.util.MutableInteger;
  63. import org.eclipse.jgit.util.NB;
  64. /**
  65. * A single file (or stage of a file) in a {@link DirCache}.
  66. * <p>
  67. * An entry represents exactly one stage of a file. If a file path is unmerged
  68. * then multiple DirCacheEntry instances may appear for the same path name.
  69. */
  70. public class DirCacheEntry {
  71. private static final byte[] nullpad = new byte[8];
  72. /** The standard (fully merged) stage for an entry. */
  73. public static final int STAGE_0 = 0;
  74. /** The base tree revision for an entry. */
  75. public static final int STAGE_1 = 1;
  76. /** The first tree revision (usually called "ours"). */
  77. public static final int STAGE_2 = 2;
  78. /** The second tree revision (usually called "theirs"). */
  79. public static final int STAGE_3 = 3;
  80. // private static final int P_CTIME = 0;
  81. // private static final int P_CTIME_NSEC = 4;
  82. private static final int P_MTIME = 8;
  83. // private static final int P_MTIME_NSEC = 12;
  84. // private static final int P_DEV = 16;
  85. // private static final int P_INO = 20;
  86. private static final int P_MODE = 24;
  87. // private static final int P_UID = 28;
  88. // private static final int P_GID = 32;
  89. private static final int P_SIZE = 36;
  90. private static final int P_OBJECTID = 40;
  91. private static final int P_FLAGS = 60;
  92. private static final int P_FLAGS2 = 62;
  93. /** Mask applied to data in {@link #P_FLAGS} to get the name length. */
  94. private static final int NAME_MASK = 0xfff;
  95. private static final int INTENT_TO_ADD = 0x20000000;
  96. private static final int SKIP_WORKTREE = 0x40000000;
  97. private static final int EXTENDED_FLAGS = (INTENT_TO_ADD | SKIP_WORKTREE);
  98. private static final int INFO_LEN = 62;
  99. private static final int INFO_LEN_EXTENDED = 64;
  100. private static final int EXTENDED = 0x40;
  101. private static final int ASSUME_VALID = 0x80;
  102. /** In-core flag signaling that the entry should be considered as modified. */
  103. private static final int UPDATE_NEEDED = 0x1;
  104. /** (Possibly shared) header information storage. */
  105. private final byte[] info;
  106. /** First location within {@link #info} where our header starts. */
  107. private final int infoOffset;
  108. /** Our encoded path name, from the root of the repository. */
  109. final byte[] path;
  110. /** Flags which are never stored to disk. */
  111. private byte inCoreFlags;
  112. DirCacheEntry(final byte[] sharedInfo, final MutableInteger infoAt,
  113. final InputStream in, final MessageDigest md) throws IOException {
  114. info = sharedInfo;
  115. infoOffset = infoAt.value;
  116. IO.readFully(in, info, infoOffset, INFO_LEN);
  117. final int len;
  118. if (isExtended()) {
  119. len = INFO_LEN_EXTENDED;
  120. IO.readFully(in, info, infoOffset + INFO_LEN, INFO_LEN_EXTENDED - INFO_LEN);
  121. if ((getExtendedFlags() & ~EXTENDED_FLAGS) != 0)
  122. throw new IOException(MessageFormat.format(JGitText.get()
  123. .DIRCUnrecognizedExtendedFlags, String.valueOf(getExtendedFlags())));
  124. } else
  125. len = INFO_LEN;
  126. infoAt.value += len;
  127. md.update(info, infoOffset, len);
  128. int pathLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
  129. int skipped = 0;
  130. if (pathLen < NAME_MASK) {
  131. path = new byte[pathLen];
  132. IO.readFully(in, path, 0, pathLen);
  133. md.update(path, 0, pathLen);
  134. } else {
  135. final ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  136. {
  137. final byte[] buf = new byte[NAME_MASK];
  138. IO.readFully(in, buf, 0, NAME_MASK);
  139. tmp.write(buf);
  140. }
  141. for (;;) {
  142. final int c = in.read();
  143. if (c < 0)
  144. throw new EOFException(JGitText.get().shortReadOfBlock);
  145. if (c == 0)
  146. break;
  147. tmp.write(c);
  148. }
  149. path = tmp.toByteArray();
  150. pathLen = path.length;
  151. skipped = 1; // we already skipped 1 '\0' above to break the loop.
  152. md.update(path, 0, pathLen);
  153. md.update((byte) 0);
  154. }
  155. // Index records are padded out to the next 8 byte alignment
  156. // for historical reasons related to how C Git read the files.
  157. //
  158. final int actLen = len + pathLen;
  159. final int expLen = (actLen + 8) & ~7;
  160. final int padLen = expLen - actLen - skipped;
  161. if (padLen > 0) {
  162. IO.skipFully(in, padLen);
  163. md.update(nullpad, 0, padLen);
  164. }
  165. }
  166. /**
  167. * Create an empty entry at stage 0.
  168. *
  169. * @param newPath
  170. * name of the cache entry.
  171. * @throws IllegalArgumentException
  172. * If the path starts or ends with "/", or contains "//" either
  173. * "\0". These sequences are not permitted in a git tree object
  174. * or DirCache file.
  175. */
  176. public DirCacheEntry(final String newPath) {
  177. this(Constants.encode(newPath));
  178. }
  179. /**
  180. * Create an empty entry at the specified stage.
  181. *
  182. * @param newPath
  183. * name of the cache entry.
  184. * @param stage
  185. * the stage index of the new entry.
  186. * @throws IllegalArgumentException
  187. * If the path starts or ends with "/", or contains "//" either
  188. * "\0". These sequences are not permitted in a git tree object
  189. * or DirCache file. Or if {@code stage} is outside of the
  190. * range 0..3, inclusive.
  191. */
  192. public DirCacheEntry(final String newPath, final int stage) {
  193. this(Constants.encode(newPath), stage);
  194. }
  195. /**
  196. * Create an empty entry at stage 0.
  197. *
  198. * @param newPath
  199. * name of the cache entry, in the standard encoding.
  200. * @throws IllegalArgumentException
  201. * If the path starts or ends with "/", or contains "//" either
  202. * "\0". These sequences are not permitted in a git tree object
  203. * or DirCache file.
  204. */
  205. public DirCacheEntry(final byte[] newPath) {
  206. this(newPath, STAGE_0);
  207. }
  208. /**
  209. * Create an empty entry at the specified stage.
  210. *
  211. * @param newPath
  212. * name of the cache entry, in the standard encoding.
  213. * @param stage
  214. * the stage index of the new entry.
  215. * @throws IllegalArgumentException
  216. * If the path starts or ends with "/", or contains "//" either
  217. * "\0". These sequences are not permitted in a git tree object
  218. * or DirCache file. Or if {@code stage} is outside of the
  219. * range 0..3, inclusive.
  220. */
  221. public DirCacheEntry(final byte[] newPath, final int stage) {
  222. if (!isValidPath(newPath))
  223. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidPath
  224. , toString(newPath)));
  225. if (stage < 0 || 3 < stage)
  226. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidStageForPath
  227. , stage, toString(newPath)));
  228. info = new byte[INFO_LEN];
  229. infoOffset = 0;
  230. path = newPath;
  231. int flags = ((stage & 0x3) << 12);
  232. if (path.length < NAME_MASK)
  233. flags |= path.length;
  234. else
  235. flags |= NAME_MASK;
  236. NB.encodeInt16(info, infoOffset + P_FLAGS, flags);
  237. }
  238. void write(final OutputStream os) throws IOException {
  239. final int len = isExtended() ? INFO_LEN_EXTENDED : INFO_LEN;
  240. final int pathLen = path.length;
  241. os.write(info, infoOffset, len);
  242. os.write(path, 0, pathLen);
  243. // Index records are padded out to the next 8 byte alignment
  244. // for historical reasons related to how C Git read the files.
  245. //
  246. final int actLen = len + pathLen;
  247. final int expLen = (actLen + 8) & ~7;
  248. if (actLen != expLen)
  249. os.write(nullpad, 0, expLen - actLen);
  250. }
  251. /**
  252. * Is it possible for this entry to be accidentally assumed clean?
  253. * <p>
  254. * The "racy git" problem happens when a work file can be updated faster
  255. * than the filesystem records file modification timestamps. It is possible
  256. * for an application to edit a work file, update the index, then edit it
  257. * again before the filesystem will give the work file a new modification
  258. * timestamp. This method tests to see if file was written out at the same
  259. * time as the index.
  260. *
  261. * @param smudge_s
  262. * seconds component of the index's last modified time.
  263. * @param smudge_ns
  264. * nanoseconds component of the index's last modified time.
  265. * @return true if extra careful checks should be used.
  266. */
  267. public final boolean mightBeRacilyClean(final int smudge_s, final int smudge_ns) {
  268. // If the index has a modification time then it came from disk
  269. // and was not generated from scratch in memory. In such cases
  270. // the entry is 'racily clean' if the entry's cached modification
  271. // time is equal to or later than the index modification time. In
  272. // such cases the work file is too close to the index to tell if
  273. // it is clean or not based on the modification time alone.
  274. //
  275. final int base = infoOffset + P_MTIME;
  276. final int mtime = NB.decodeInt32(info, base);
  277. if (smudge_s == mtime)
  278. return smudge_ns <= NB.decodeInt32(info, base + 4);
  279. return false;
  280. }
  281. /**
  282. * Force this entry to no longer match its working tree file.
  283. * <p>
  284. * This avoids the "racy git" problem by making this index entry no longer
  285. * match the file in the working directory. Later git will be forced to
  286. * compare the file content to ensure the file matches the working tree.
  287. */
  288. public final void smudgeRacilyClean() {
  289. // To mark an entry racily clean we set its length to 0 (like native git
  290. // does). Entries which are not racily clean and have zero length can be
  291. // distinguished from racily clean entries by checking P_OBJECTID
  292. // against the SHA1 of empty content. When length is 0 and P_OBJECTID is
  293. // different from SHA1 of empty content we know the entry is marked
  294. // racily clean
  295. final int base = infoOffset + P_SIZE;
  296. Arrays.fill(info, base, base + 4, (byte) 0);
  297. }
  298. /**
  299. * Check whether this entry has been smudged or not
  300. * <p>
  301. * If a blob has length 0 we know his id see {@link Constants#EMPTY_BLOB_ID}. If an entry
  302. * has length 0 and an ID different from the one for empty blob we know this
  303. * entry was smudged.
  304. *
  305. * @return <code>true</code> if the entry is smudged, <code>false</code>
  306. * otherwise
  307. */
  308. public final boolean isSmudged() {
  309. final int base = infoOffset + P_OBJECTID;
  310. return (getLength() == 0) && (Constants.EMPTY_BLOB_ID.compareTo(info, base) != 0);
  311. }
  312. final byte[] idBuffer() {
  313. return info;
  314. }
  315. final int idOffset() {
  316. return infoOffset + P_OBJECTID;
  317. }
  318. /**
  319. * Is this entry always thought to be unmodified?
  320. * <p>
  321. * Most entries in the index do not have this flag set. Users may however
  322. * set them on if the file system stat() costs are too high on this working
  323. * directory, such as on NFS or SMB volumes.
  324. *
  325. * @return true if we must assume the entry is unmodified.
  326. */
  327. public boolean isAssumeValid() {
  328. return (info[infoOffset + P_FLAGS] & ASSUME_VALID) != 0;
  329. }
  330. /**
  331. * Set the assume valid flag for this entry,
  332. *
  333. * @param assume
  334. * true to ignore apparent modifications; false to look at last
  335. * modified to detect file modifications.
  336. */
  337. public void setAssumeValid(final boolean assume) {
  338. if (assume)
  339. info[infoOffset + P_FLAGS] |= ASSUME_VALID;
  340. else
  341. info[infoOffset + P_FLAGS] &= ~ASSUME_VALID;
  342. }
  343. /**
  344. * @return true if this entry should be checked for changes
  345. */
  346. public boolean isUpdateNeeded() {
  347. return (inCoreFlags & UPDATE_NEEDED) != 0;
  348. }
  349. /**
  350. * Set whether this entry must be checked for changes
  351. *
  352. * @param updateNeeded
  353. */
  354. public void setUpdateNeeded(boolean updateNeeded) {
  355. if (updateNeeded)
  356. inCoreFlags |= UPDATE_NEEDED;
  357. else
  358. inCoreFlags &= ~UPDATE_NEEDED;
  359. }
  360. /**
  361. * Get the stage of this entry.
  362. * <p>
  363. * Entries have one of 4 possible stages: 0-3.
  364. *
  365. * @return the stage of this entry.
  366. */
  367. public int getStage() {
  368. return (info[infoOffset + P_FLAGS] >>> 4) & 0x3;
  369. }
  370. /**
  371. * Returns whether this entry should be skipped from the working tree.
  372. *
  373. * @return true if this entry should be skipepd.
  374. */
  375. public boolean isSkipWorkTree() {
  376. return (getExtendedFlags() & SKIP_WORKTREE) != 0;
  377. }
  378. /**
  379. * Returns whether this entry is intent to be added to the Index.
  380. *
  381. * @return true if this entry is intent to add.
  382. */
  383. public boolean isIntentToAdd() {
  384. return (getExtendedFlags() & INTENT_TO_ADD) != 0;
  385. }
  386. /**
  387. * Obtain the raw {@link FileMode} bits for this entry.
  388. *
  389. * @return mode bits for the entry.
  390. * @see FileMode#fromBits(int)
  391. */
  392. public int getRawMode() {
  393. return NB.decodeInt32(info, infoOffset + P_MODE);
  394. }
  395. /**
  396. * Obtain the {@link FileMode} for this entry.
  397. *
  398. * @return the file mode singleton for this entry.
  399. */
  400. public FileMode getFileMode() {
  401. return FileMode.fromBits(getRawMode());
  402. }
  403. /**
  404. * Set the file mode for this entry.
  405. *
  406. * @param mode
  407. * the new mode constant.
  408. * @throws IllegalArgumentException
  409. * If {@code mode} is {@link FileMode#MISSING},
  410. * {@link FileMode#TREE}, or any other type code not permitted
  411. * in a tree object.
  412. */
  413. public void setFileMode(final FileMode mode) {
  414. switch (mode.getBits() & FileMode.TYPE_MASK) {
  415. case FileMode.TYPE_MISSING:
  416. case FileMode.TYPE_TREE:
  417. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidModeForPath
  418. , mode, getPathString()));
  419. }
  420. NB.encodeInt32(info, infoOffset + P_MODE, mode.getBits());
  421. }
  422. /**
  423. * Get the cached last modification date of this file, in milliseconds.
  424. * <p>
  425. * One of the indicators that the file has been modified by an application
  426. * changing the working tree is if the last modification time for the file
  427. * differs from the time stored in this entry.
  428. *
  429. * @return last modification time of this file, in milliseconds since the
  430. * Java epoch (midnight Jan 1, 1970 UTC).
  431. */
  432. public long getLastModified() {
  433. return decodeTS(P_MTIME);
  434. }
  435. /**
  436. * Set the cached last modification date of this file, using milliseconds.
  437. *
  438. * @param when
  439. * new cached modification date of the file, in milliseconds.
  440. */
  441. public void setLastModified(final long when) {
  442. encodeTS(P_MTIME, when);
  443. }
  444. /**
  445. * Get the cached size (in bytes) of this file.
  446. * <p>
  447. * One of the indicators that the file has been modified by an application
  448. * changing the working tree is if the size of the file (in bytes) differs
  449. * from the size stored in this entry.
  450. * <p>
  451. * Note that this is the length of the file in the working directory, which
  452. * may differ from the size of the decompressed blob if work tree filters
  453. * are being used, such as LF<->CRLF conversion.
  454. *
  455. * @return cached size of the working directory file, in bytes.
  456. */
  457. public int getLength() {
  458. return NB.decodeInt32(info, infoOffset + P_SIZE);
  459. }
  460. /**
  461. * Set the cached size (in bytes) of this file.
  462. *
  463. * @param sz
  464. * new cached size of the file, as bytes.
  465. */
  466. public void setLength(final int sz) {
  467. NB.encodeInt32(info, infoOffset + P_SIZE, sz);
  468. }
  469. /**
  470. * Set the cached size (in bytes) of this file.
  471. *
  472. * @param sz
  473. * new cached size of the file, as bytes.
  474. * @throws IllegalArgumentException
  475. * if the size exceeds the 2 GiB barrier imposed by current file
  476. * format limitations.
  477. */
  478. public void setLength(final long sz) {
  479. if (Integer.MAX_VALUE <= sz)
  480. throw new IllegalArgumentException(MessageFormat.format(JGitText
  481. .get().sizeExceeds2GB, getPathString(), sz));
  482. setLength((int) sz);
  483. }
  484. /**
  485. * Obtain the ObjectId for the entry.
  486. * <p>
  487. * Using this method to compare ObjectId values between entries is
  488. * inefficient as it causes memory allocation.
  489. *
  490. * @return object identifier for the entry.
  491. */
  492. public ObjectId getObjectId() {
  493. return ObjectId.fromRaw(idBuffer(), idOffset());
  494. }
  495. /**
  496. * Set the ObjectId for the entry.
  497. *
  498. * @param id
  499. * new object identifier for the entry. May be
  500. * {@link ObjectId#zeroId()} to remove the current identifier.
  501. */
  502. public void setObjectId(final AnyObjectId id) {
  503. id.copyRawTo(idBuffer(), idOffset());
  504. }
  505. /**
  506. * Set the ObjectId for the entry from the raw binary representation.
  507. *
  508. * @param bs
  509. * the raw byte buffer to read from. At least 20 bytes after p
  510. * must be available within this byte array.
  511. * @param p
  512. * position to read the first byte of data from.
  513. */
  514. public void setObjectIdFromRaw(final byte[] bs, final int p) {
  515. final int n = Constants.OBJECT_ID_LENGTH;
  516. System.arraycopy(bs, p, idBuffer(), idOffset(), n);
  517. }
  518. /**
  519. * Get the entry's complete path.
  520. * <p>
  521. * This method is not very efficient and is primarily meant for debugging
  522. * and final output generation. Applications should try to avoid calling it,
  523. * and if invoked do so only once per interesting entry, where the name is
  524. * absolutely required for correct function.
  525. *
  526. * @return complete path of the entry, from the root of the repository. If
  527. * the entry is in a subtree there will be at least one '/' in the
  528. * returned string.
  529. */
  530. public String getPathString() {
  531. return toString(path);
  532. }
  533. /**
  534. * Copy the ObjectId and other meta fields from an existing entry.
  535. * <p>
  536. * This method copies everything except the path from one entry to another,
  537. * supporting renaming.
  538. *
  539. * @param src
  540. * the entry to copy ObjectId and meta fields from.
  541. */
  542. public void copyMetaData(final DirCacheEntry src) {
  543. final int pLen = NB.decodeUInt16(info, infoOffset + P_FLAGS) & NAME_MASK;
  544. System.arraycopy(src.info, src.infoOffset, info, infoOffset, INFO_LEN);
  545. NB.encodeInt16(info, infoOffset + P_FLAGS, pLen
  546. | NB.decodeUInt16(info, infoOffset + P_FLAGS) & ~NAME_MASK);
  547. }
  548. /**
  549. * @return true if the entry contains extended flags.
  550. */
  551. boolean isExtended() {
  552. return (info[infoOffset + P_FLAGS] & EXTENDED) != 0;
  553. }
  554. private long decodeTS(final int pIdx) {
  555. final int base = infoOffset + pIdx;
  556. final int sec = NB.decodeInt32(info, base);
  557. final int ms = NB.decodeInt32(info, base + 4) / 1000000;
  558. return 1000L * sec + ms;
  559. }
  560. private void encodeTS(final int pIdx, final long when) {
  561. final int base = infoOffset + pIdx;
  562. NB.encodeInt32(info, base, (int) (when / 1000));
  563. NB.encodeInt32(info, base + 4, ((int) (when % 1000)) * 1000000);
  564. }
  565. private int getExtendedFlags() {
  566. if (isExtended())
  567. return NB.decodeUInt16(info, infoOffset + P_FLAGS2) << 16;
  568. else
  569. return 0;
  570. }
  571. private static String toString(final byte[] path) {
  572. return Constants.CHARSET.decode(ByteBuffer.wrap(path)).toString();
  573. }
  574. static boolean isValidPath(final byte[] path) {
  575. if (path.length == 0)
  576. return false; // empty path is not permitted.
  577. boolean componentHasChars = false;
  578. for (final byte c : path) {
  579. switch (c) {
  580. case 0:
  581. return false; // NUL is never allowed within the path.
  582. case '/':
  583. if (componentHasChars)
  584. componentHasChars = false;
  585. else
  586. return false;
  587. break;
  588. default:
  589. componentHasChars = true;
  590. }
  591. }
  592. return componentHasChars;
  593. }
  594. static int getMaximumInfoLength(boolean extended) {
  595. return extended ? INFO_LEN_EXTENDED : INFO_LEN;
  596. }
  597. }