View | Details | Raw Unified | Return to bug 166977 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ClassFile.java (-76 / +140 lines)
Lines 121-126 Link Here
121
	public static final int INITIAL_HEADER_SIZE = 1500;
121
	public static final int INITIAL_HEADER_SIZE = 1500;
122
	public static final int INNER_CLASSES_SIZE = 5;
122
	public static final int INNER_CLASSES_SIZE = 5;
123
	
123
	
124
	private static int MAX_TIME = -1;
125
	private static final int MAX_COUNT = 50;
124
	/**
126
	/**
125
	 * INTERNAL USE-ONLY
127
	 * INTERNAL USE-ONLY
126
	 * Build all the directories and subdirectories corresponding to the packages names
128
	 * Build all the directories and subdirectories corresponding to the packages names
Lines 142-155 Link Here
142
		char fileSeparatorChar = File.separatorChar;
144
		char fileSeparatorChar = File.separatorChar;
143
		String fileSeparator = File.separator;
145
		String fileSeparator = File.separator;
144
		File f;
146
		File f;
145
		outputPath = outputPath.replace('/', fileSeparatorChar);
147
		if (fileSeparatorChar != '/') {
146
			// these could be optimized out if we normalized paths once and for
148
			// these could be optimized out if we normalized paths once and for all
147
			// all
149
			outputPath = outputPath.replace('/', fileSeparatorChar);
148
		relativeFileName = relativeFileName.replace('/', fileSeparatorChar);
150
			relativeFileName = relativeFileName.replace('/', fileSeparatorChar);
151
		}
149
		String outputDirPath, fileName;
152
		String outputDirPath, fileName;
150
		int separatorIndex = relativeFileName.lastIndexOf(fileSeparatorChar);
153
		int separatorIndex = relativeFileName.lastIndexOf(fileSeparatorChar);
154
		boolean pathEndsWithSep = outputPath.endsWith(fileSeparator);
151
		if (separatorIndex == -1) {
155
		if (separatorIndex == -1) {
152
			if (outputPath.endsWith(fileSeparator)) {
156
			if (pathEndsWithSep) {
153
				outputDirPath = outputPath.substring(0, outputPath.length() - 1);
157
				outputDirPath = outputPath.substring(0, outputPath.length() - 1);
154
				fileName = outputPath + relativeFileName;
158
				fileName = outputPath + relativeFileName;
155
			} else {
159
			} else {
Lines 157-169 Link Here
157
				fileName = outputPath + fileSeparator + relativeFileName;
161
				fileName = outputPath + fileSeparator + relativeFileName;
158
			}
162
			}
159
		} else {
163
		} else {
160
			if (outputPath.endsWith(fileSeparator)) {
164
			if (pathEndsWithSep) {
161
				outputDirPath = outputPath + 
165
				outputDirPath = outputPath + relativeFileName.substring(0, separatorIndex);
162
					relativeFileName.substring(0, separatorIndex);
163
				fileName = outputPath + relativeFileName;
166
				fileName = outputPath + relativeFileName;
164
			} else {
167
			} else {
165
				outputDirPath = outputPath + fileSeparator +
168
				outputDirPath = outputPath + fileSeparator + relativeFileName.substring(0, separatorIndex);
166
					relativeFileName.substring(0, separatorIndex);
167
				fileName = outputPath + fileSeparator + relativeFileName;
169
				fileName = outputPath + fileSeparator + relativeFileName;
168
			}
170
			}
169
		}
171
		}
Lines 171-243 Link Here
171
		f.mkdirs();
173
		f.mkdirs();
172
		if (f.isDirectory()) {
174
		if (f.isDirectory()) {
173
			return fileName;
175
			return fileName;
176
		}
177
178
		// the directory creation failed for some reason - retry using
179
		// a slower algorithm so as to refine the diagnostic
180
		// First create the output dir
181
		if (pathEndsWithSep) {
182
			outputPath = outputPath.substring(0, outputPath.length() - 1);
183
		}
184
		f = new File(outputPath);
185
		boolean checkFileType = false;
186
		if (f.exists()) {
187
		  	checkFileType = true; // pre-existed
174
		} else {
188
		} else {
175
			// the directory creation failed for some reason - retry using
189
			// we have to create that directory
176
			// a slower algorithm so as to refine the diagnostic
190
			int count = 0;
177
			if (outputPath.endsWith(fileSeparator)) {
191
			while (!f.mkdirs() && count < MAX_COUNT) {
178
				outputPath = outputPath.substring(0, outputPath.length() - 1);
192
			  	if (f.exists()) {
193
			  	  	// someone else created f -- need to check its type
194
			  	  	checkFileType = true;
195
			  	  	break;
196
			  	}
197
				try {
198
					Thread.sleep(10);
199
					count++;
200
				} catch (InterruptedException e) {
201
					break;
202
				}
179
			}
203
			}
180
			f = new File(outputPath);
204
		  	if (!f.exists()) {
181
			boolean checkFileType = false;
205
		  		System.out.println("ClassFile: output directory "+f+" does not exist after creation!");
206
		  	  	// no one could create f -- complain
207
				throw new IOException(Messages.bind(
208
					Messages.output_notValidAll, f.getAbsolutePath()));
209
			}
210
		  	if (count > 0) {
211
				int time = count * 10;
212
				if (time > MAX_TIME) MAX_TIME = time;
213
				System.out.println("ClassFile: takes "+time+"ms (max="+MAX_TIME+"ms) before the output directory "+f+" was created...");
214
			}
215
		}
216
		if (checkFileType) {
217
		  	if (!f.isDirectory()) {
218
		  		System.out.println("ClassFile: output "+f+" is not a directory!");
219
    			throw new IOException(Messages.bind(
220
    				Messages.output_isFile, f.getAbsolutePath()));
221
		  	}
222
		}
223
		
224
		// Then create sub-directories
225
		StringBuffer outDir = new StringBuffer(outputPath);
226
		outDir.append(fileSeparator);
227
		StringTokenizer tokenizer = new StringTokenizer(relativeFileName, fileSeparator);
228
		String token = tokenizer.nextToken();
229
		while (tokenizer.hasMoreTokens()) {
230
			f = new File(outDir.append(token).append(fileSeparator).toString());
231
		  	checkFileType = false; // reset
182
			if (f.exists()) {
232
			if (f.exists()) {
183
			  	checkFileType = true; // pre-existed
233
			  	checkFileType = true; // this is suboptimal, but it catches corner cases
234
			  						  // in which a regular file pre-exists
184
			} else {
235
			} else {
185
				// we have to create that directory
236
				// we have to create that directory
186
				if (!f.mkdirs()) {
237
				int count = 0;
187
				  	if (f.exists()) {
238
    			while (!f.mkdir() && count < MAX_COUNT) {
188
				  	  	// someone else created f -- need to check its type
239
    			  	if (f.exists()) {
189
				  	  	checkFileType = true;
240
    			  	  	// someone else created f -- need to check its type
190
				  	} else {
241
    			  	  	checkFileType = true;
191
				  	  	// no one could create f -- complain
242
    			  	  	break;
192
	    				throw new IOException(Messages.bind(
243
    			  	}
193
	    					Messages.output_notValidAll, f.getAbsolutePath()));
244
    				try {
194
				  	}
245
    					// need to try again some times as some OS can take some times
195
				}
246
    					// before the directory really exists...
196
			}
247
						Thread.sleep(10);
197
			if (checkFileType) {
248
						count++;
198
			  	if (!f.isDirectory()) {
249
					} catch (InterruptedException e) {
199
	    			throw new IOException(Messages.bind(
250
						break;
200
	    				Messages.output_isFile, f.getAbsolutePath()));
251
					}
252
    			}
253
			  	if (!f.exists()) {
254
			  		System.out.println("ClassFile: output sub-directory "+f+" does not exist after creation!");
255
			  	  	// no one could create f -- complain
256
    				throw new IOException(Messages.bind(
257
    					Messages.output_notValid, 
258
    						outDir.substring(outputPath.length() + 1, 
259
    							outDir.length() - 1),
260
    						outputPath));
201
			  	}
261
			  	}
262
    			if (count > 0) {
263
    				int time = count * 10;
264
    				if (time > MAX_TIME) MAX_TIME = time;
265
					System.out.println("ClassFile: takes "+time+"ms (max="+MAX_TIME+"ms) before the output sub-directory "+f+" was created...");
266
    			}
202
			}
267
			}
203
			StringBuffer outDir = new StringBuffer(outputPath);
268
    		if (checkFileType) {
204
			outDir.append(fileSeparator);
269
    		  	if (!f.isDirectory()) {
205
			StringTokenizer tokenizer =
270
			  		System.out.println("ClassFile: sub output "+f+" is not a directory!");
206
				new StringTokenizer(relativeFileName, fileSeparator);
271
        			throw new IOException(Messages.bind(
207
			String token = tokenizer.nextToken();
272
        				Messages.output_isFile, f.getAbsolutePath()));
208
			while (tokenizer.hasMoreTokens()) {
273
    		  	}
209
				f = new File(outDir.append(token).append(fileSeparator).toString());
274
    		}
210
			  	checkFileType = false; // reset
275
			token = tokenizer.nextToken();
211
				if (f.exists()) {
212
				  	checkFileType = true; // this is suboptimal, but it catches corner cases
213
				  						  // in which a regular file pre-exists
214
				} else {
215
				// we have to create that directory
216
	    			if (!f.mkdir()) {
217
	    			  	if (f.exists()) {
218
	    			  	  	// someone else created f -- need to check its type
219
	    			  	  	checkFileType = true;
220
	    			  	} else {
221
	    			  	  	// no one could create f -- complain
222
	        				throw new IOException(Messages.bind(
223
	        					Messages.output_notValid, 
224
	        						outDir.substring(outputPath.length() + 1, 
225
	        							outDir.length() - 1),
226
	        						outputPath));
227
	    			  	}
228
	    			}
229
				}
230
	    		if (checkFileType) {
231
	    		  	if (!f.isDirectory()) {
232
	        			throw new IOException(Messages.bind(
233
	        				Messages.output_isFile, f.getAbsolutePath()));
234
	    		  	}
235
	    		}
236
				token = tokenizer.nextToken();
237
			}
238
			// token contains the last one
239
			return outDir.append(token).toString();
240
		}
276
		}
277
		// token contains the last one
278
		return outDir.append(token).toString();
241
	}
279
	}
242
	/**
280
	/**
243
	 * INTERNAL USE-ONLY
281
	 * INTERNAL USE-ONLY
Lines 343-353 Link Here
343
		String relativeFileName,
381
		String relativeFileName,
344
		ClassFile classFile) throws IOException {
382
		ClassFile classFile) throws IOException {
345
			
383
			
346
		BufferedOutputStream output = null;
384
//		BufferedOutputStream output = null;
385
		File file = null;
347
    	if (generatePackagesStructure) {
386
    	if (generatePackagesStructure) {
348
    		output = new BufferedOutputStream(
387
//    		output = new BufferedOutputStream(
349
    			new FileOutputStream(
388
//    			new FileOutputStream(
350
    				new File(buildAllDirectoriesInto(outputPath, relativeFileName))));
389
//    					new File(buildAllDirectoriesInto(outputPath, relativeFileName))));
390
			file = new File(buildAllDirectoriesInto(outputPath, relativeFileName));
351
    	} else {
391
    	} else {
352
    		String fileName = null;
392
    		String fileName = null;
353
    		char fileSeparatorChar = File.separatorChar;
393
    		char fileSeparatorChar = File.separatorChar;
Lines 370-385 Link Here
370
    				fileName = outputPath + fileSeparator + relativeFileName.substring(indexOfPackageSeparator + 1, length);
410
    				fileName = outputPath + fileSeparator + relativeFileName.substring(indexOfPackageSeparator + 1, length);
371
    			}
411
    			}
372
    		}
412
    		}
373
    		output = new BufferedOutputStream(
413
//    		output = new BufferedOutputStream(
374
    			new FileOutputStream(
414
//    			new FileOutputStream(
375
    					new File(fileName)));
415
//    					new File(fileName)));
416
			file = new File(fileName);
376
    	}
417
    	}
418
    	
419
    	// Create file
420
		BufferedOutputStream output = new BufferedOutputStream(
421
			new FileOutputStream(file));
422
    	
423
    	// Wait for file creation
424
		int count = 0, max = 50;
425
		while (!file.exists() && count < max) {
426
			try {
427
				Thread.sleep(10);
428
				count++;
429
			} catch (InterruptedException e) {
430
				break;
431
			}
432
		}
433
		if (count > 0) {
434
			int time = count * 10;
435
			if (time > MAX_TIME) MAX_TIME = time;
436
			System.out.println("ClassFile: takes "+time+"ms (max="+MAX_TIME+"ms) before the file "+file+" was created...");
437
		}
438
		
439
		// Write contents
377
		try {
440
		try {
378
			// if no IOException occured, output cannot be null
441
			// if no IOException occured, output cannot be null
379
			output.write(classFile.header, 0, classFile.headerOffset);
442
			output.write(classFile.header, 0, classFile.headerOffset);
380
			output.write(classFile.contents, 0, classFile.contentsOffset);
443
			output.write(classFile.contents, 0, classFile.contentsOffset);
381
			output.flush();
444
			output.flush();
382
		} catch(IOException e) {
445
		} catch(IOException e) {
446
	  		System.out.println("ClassFile: exception ["+e.getMessage()+"] while writing class file "+file);
383
			throw e;
447
			throw e;
384
		} finally {
448
		} finally {
385
			output.close();
449
			output.close();
(-)src/org/eclipse/jdt/core/tests/compiler/regression/Requestor.java (+2 lines)
Lines 105-110 Link Here
105
				try {
105
				try {
106
					ClassFile.writeToDisk(true, outputPath, relativeName, classFile);
106
					ClassFile.writeToDisk(true, outputPath, relativeName, classFile);
107
				} catch(IOException e) {
107
				} catch(IOException e) {
108
					System.out.println("Still got an exception ["+e.getMessage()+"] while writing class file:"+new String(classFile.fileName()));
109
					e.printStackTrace();
108
				}
110
				}
109
			}
111
			}
110
		}
112
		}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/AbstractRegressionTest.java (-1 / +1 lines)
Lines 1313-1319 Link Here
1313
		// clean up output dir
1313
		// clean up output dir
1314
		File outputDir = new File(OUTPUT_DIR);
1314
		File outputDir = new File(OUTPUT_DIR);
1315
		if (outputDir.exists()) {
1315
		if (outputDir.exists()) {
1316
			Util.rmdir(outputDir);
1316
			Util.flushDirectoryContent(outputDir);
1317
		}
1317
		}
1318
		super.tearDown();
1318
		super.tearDown();
1319
		if (RUN_JAVAC) {
1319
		if (RUN_JAVAC) {
(-)src/org/eclipse/jdt/core/tests/util/Util.java (-3 / +53 lines)
Lines 175-180 Link Here
175
		File file = new File(classpaths[i]);
175
		File file = new File(classpaths[i]);
176
		if (!file.exists()) {
176
		if (!file.exists()) {
177
			file.mkdirs();
177
			file.mkdirs();
178
			if (!file.exists())
179
				waitUntilReady(file, true/* should exist */);
178
		} 
180
		} 
179
	}
181
	}
180
	return defaultClassPaths;
182
	return defaultClassPaths;
Lines 183-190 Link Here
183
	String[] classLibs = getJavaClassLibs();
185
	String[] classLibs = getJavaClassLibs();
184
	final int length = classLibs.length;
186
	final int length = classLibs.length;
185
	File dir = new File(classpath);
187
	File dir = new File(classpath);
186
	if (!dir.exists())
188
	if (!dir.exists()) {
187
		dir.mkdirs();
189
		dir.mkdirs();
190
		if (!dir.exists())
191
			waitUntilReady(dir, true/* should exist */);
192
	}
188
	String[] defaultClassPaths = new String[length + 1];
193
	String[] defaultClassPaths = new String[length + 1];
189
	if (inFront) {
194
	if (inFront) {
190
		System.arraycopy(classLibs, 0, defaultClassPaths, 1, length);
195
		System.arraycopy(classLibs, 0, defaultClassPaths, 1, length);
Lines 551-557 Link Here
551
				flushDirectoryContent(current);
556
				flushDirectoryContent(current);
552
			}
557
			}
553
			if (!current.delete()) 
558
			if (!current.delete()) 
554
				System.err.println("Could not delete " + current.getName());
559
//				System.err.println("Could not delete " + current.getName());
560
				waitUntilReady(current, false /* should not exist */);
555
		}
561
		}
556
	}
562
	}
557
}
563
}
Lines 752-758 Link Here
752
public static void zip(File rootDir, String zipPath) throws IOException {
758
public static void zip(File rootDir, String zipPath) throws IOException {
753
	ZipOutputStream zip = null;
759
	ZipOutputStream zip = null;
754
	try {
760
	try {
755
		zip  = new ZipOutputStream(new FileOutputStream(zipPath));
761
		File zipFile = new File(zipPath);
762
		zip  = new ZipOutputStream(new FileOutputStream(zipFile));
763
		waitUntilReady(zipFile, true /* shouldExist */);
756
		zip(rootDir, zip, rootDir.getPath().length()+1); // 1 for last slash
764
		zip(rootDir, zip, rootDir.getPath().length()+1); // 1 for last slash
757
	} finally {
765
	} finally {
758
		if (zip != null) {
766
		if (zip != null) {
Lines 779-782 Link Here
779
		}
787
		}
780
	}
788
	}
781
}
789
}
790
private static int MAX_TIME = 0;
791
private static void waitUntilReady(File dir, boolean shouldExist) {
792
		int count = 0;
793
		while (dir.exists() != shouldExist && count < 50) {
794
			if (count == 0) {
795
				if (shouldExist) {
796
					System.out.println(dir+" was not created after mkdirs()!");
797
				} else {
798
					System.out.println(dir+" was not removed after delete()!");
799
				}
800
				System.out.print("	- retry (500ms max): ");
801
			}
802
			try {
803
				count++;
804
				Thread.sleep(10);
805
				System.out.print('.');
806
				if (shouldExist) {
807
					dir.mkdirs();
808
				} else {
809
					dir.delete();
810
				}
811
			}
812
			catch (InterruptedException ie) {
813
				break; // end loop
814
			}
815
		}
816
		if (count != 0) {
817
			if (dir.exists() != shouldExist) {
818
				System.out.println(": Still not there => throws exception!");
819
				if (shouldExist) {
820
					throw new RuntimeException(dir+" should exist after its creation!!!");
821
				} else {
822
					throw new RuntimeException(dir+" should not exist after its deletion!!!");
823
				}
824
			}
825
			String str = shouldExist ? "exists" : "was removed";
826
			int time = count * 10;
827
			if (time > MAX_TIME) MAX_TIME = time;
828
			System.out.println(" ("+time+"ms [max="+MAX_TIME+"ms] before the directory "+str+"!)");
829
		}
830
	
831
}
782
}
832
}

Return to bug 166977