View Javadoc
1   /*
2    * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
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.lib;
44  
45  import static java.lang.Long.valueOf;
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.io.FileOutputStream;
51  import java.io.IOException;
52  import java.util.TreeSet;
53  
54  import org.eclipse.jgit.api.Git;
55  import org.eclipse.jgit.junit.RepositoryTestCase;
56  import org.eclipse.jgit.treewalk.FileTreeIterator;
57  import org.eclipse.jgit.treewalk.FileTreeIteratorWithTimeControl;
58  import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
59  import org.eclipse.jgit.util.FileUtils;
60  import org.junit.Test;
61  
62  public class RacyGitTests extends RepositoryTestCase {
63  	@Test
64  	public void testIterator() throws IllegalStateException, IOException,
65  			InterruptedException {
66  		TreeSet<Long> modTimes = new TreeSet<>();
67  		File lastFile = null;
68  		for (int i = 0; i < 10; i++) {
69  			lastFile = new File(db.getWorkTree(), "0." + i);
70  			FileUtils.createNewFile(lastFile);
71  			if (i == 5)
72  				fsTick(lastFile);
73  		}
74  		modTimes.add(valueOf(fsTick(lastFile)));
75  		for (int i = 0; i < 10; i++) {
76  			lastFile = new File(db.getWorkTree(), "1." + i);
77  			FileUtils.createNewFile(lastFile);
78  		}
79  		modTimes.add(valueOf(fsTick(lastFile)));
80  		for (int i = 0; i < 10; i++) {
81  			lastFile = new File(db.getWorkTree(), "2." + i);
82  			FileUtils.createNewFile(lastFile);
83  			if (i % 4 == 0)
84  				fsTick(lastFile);
85  		}
86  		FileTreeIteratorWithTimeControl fileIt = new FileTreeIteratorWithTimeControl(
87  				db, modTimes);
88  		try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
89  			tw.addTree(fileIt);
90  			tw.setRecursive(true);
91  			FileTreeIterator t;
92  			long t0 = 0;
93  			for (int i = 0; i < 10; i++) {
94  				assertTrue(tw.next());
95  				t = tw.getTree(0, FileTreeIterator.class);
96  				if (i == 0) {
97  					t0 = t.getEntryLastModified();
98  				} else {
99  					assertEquals(t0, t.getEntryLastModified());
100 				}
101 			}
102 			long t1 = 0;
103 			for (int i = 0; i < 10; i++) {
104 				assertTrue(tw.next());
105 				t = tw.getTree(0, FileTreeIterator.class);
106 				if (i == 0) {
107 					t1 = t.getEntryLastModified();
108 					assertTrue(t1 > t0);
109 				} else {
110 					assertEquals(t1, t.getEntryLastModified());
111 				}
112 			}
113 			long t2 = 0;
114 			for (int i = 0; i < 10; i++) {
115 				assertTrue(tw.next());
116 				t = tw.getTree(0, FileTreeIterator.class);
117 				if (i == 0) {
118 					t2 = t.getEntryLastModified();
119 					assertTrue(t2 > t1);
120 				} else {
121 					assertEquals(t2, t.getEntryLastModified());
122 				}
123 			}
124 		}
125 	}
126 
127 	@Test
128 	public void testRacyGitDetection() throws Exception {
129 		TreeSet<Long> modTimes = new TreeSet<>();
130 		File lastFile;
131 
132 		// Reset to force creation of index file
133 		try (Git git = new Git(db)) {
134 			git.reset().call();
135 		}
136 
137 		// wait to ensure that modtimes of the file doesn't match last index
138 		// file modtime
139 		modTimes.add(valueOf(fsTick(db.getIndexFile())));
140 
141 		// create two files
142 		addToWorkDir("a", "a");
143 		lastFile = addToWorkDir("b", "b");
144 
145 		// wait to ensure that file-modTimes and therefore index entry modTime
146 		// doesn't match the modtime of index-file after next persistance
147 		modTimes.add(valueOf(fsTick(lastFile)));
148 
149 		// now add both files to the index. No racy git expected
150 		resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
151 
152 		assertEquals(
153 				"[a, mode:100644, time:t0, length:1, content:a]" +
154 				"[b, mode:100644, time:t0, length:1, content:b]",
155 				indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT));
156 
157 		// Remember the last modTime of index file. All modifications times of
158 		// further modification are translated to this value so it looks that
159 		// files have been modified in the same time slot as the index file
160 		modTimes.add(Long.valueOf(db.getIndexFile().lastModified()));
161 
162 		// modify one file
163 		addToWorkDir("a", "a2");
164 		// now update the index the index. 'a' has to be racily clean -- because
165 		// it's modification time is exactly the same as the previous index file
166 		// mod time.
167 		resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
168 
169 		db.readDirCache();
170 		// although racily clean a should not be reported as being dirty
171 		assertEquals(
172 				"[a, mode:100644, time:t1, smudged, length:0, content:a2]" +
173 				"[b, mode:100644, time:t0, length:1, content:b]",
174 				indexState(SMUDGE|MOD_TIME|LENGTH|CONTENT));
175 	}
176 
177 	private File addToWorkDir(String path, String content) throws IOException {
178 		File f = new File(db.getWorkTree(), path);
179 		FileOutputStream fos = new FileOutputStream(f);
180 		try {
181 			fos.write(content.getBytes(Constants.CHARACTER_ENCODING));
182 			return f;
183 		} finally {
184 			fos.close();
185 		}
186 	}
187 }