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