Skip to content

Commit af1dee3

Browse files
committed
Fix for Path Too Long Exception
* Fix for the issue #1 * Renamed the SourceCodeReader.Web directory to Web * Zipballs are extracted to the top level directory
1 parent e59fdc2 commit af1dee3

File tree

135 files changed

+112
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+112
-39
lines changed

SourceCodeReader.sln

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 2012
4-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceCodeReader.Web", "SourceCodeReader.Web\SourceCodeReader.Web.csproj", "{B80F76F9-73B8-472A-8592-3DCC46CBC29D}"
5-
EndProject
64
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{2F59898C-53FE-4396-878A-A949E0B59A97}"
75
ProjectSection(SolutionItems) = preProject
86
.nuget\NuGet.Config = .nuget\NuGet.Config
@@ -14,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{07EA8E8F
1412
EndProject
1513
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceCodeReader.Web.Tests", "Tests\SourceCodeReader.Web.Tests\SourceCodeReader.Web.Tests.csproj", "{23867FD0-DA89-44A7-ABF9-FADF4A2DF541}"
1614
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceCodeReader.Web", "Web\SourceCodeReader.Web.csproj", "{B80F76F9-73B8-472A-8592-3DCC46CBC29D}"
16+
EndProject
1717
Global
1818
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1919
Debug|Any CPU = Debug|Any CPU

Tests/SourceCodeReader.Web.Tests/SourceCodeReader.Web.Tests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
<None Include="packages.config" />
5353
</ItemGroup>
5454
<ItemGroup>
55-
<ProjectReference Include="..\..\SourceCodeReader.Web\SourceCodeReader.Web.csproj">
56-
<Project>{B80F76F9-73B8-472A-8592-3DCC46CBC29D}</Project>
55+
<ProjectReference Include="..\..\Web\SourceCodeReader.Web.csproj">
56+
<Project>{b80f76f9-73b8-472a-8592-3dcc46cbc29d}</Project>
5757
<Name>SourceCodeReader.Web</Name>
5858
</ProjectReference>
5959
</ItemGroup>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

SourceCodeReader.Web/LanguageServices/DotNet/DotNetCodeEditorService.cs renamed to Web/LanguageServices/DotNet/DotNetCodeEditorService.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public DotNetCodeEditorService(
3131

3232
public string BuildNavigatableSourceCodeFromFile(string username, string project, string path)
3333
{
34-
var projectCodeDirectory = this.GetProjectCodeDirectory(username, project);
34+
var projectCodeDirectory = new DirectoryInfo(this.applicationConfigurationProvider.GetProjectSourceCodePath(username, project));
3535
string fullPath = Path.Combine(projectCodeDirectory.FullName, path.Replace(@"/", @"\"));
3636
string fileExtension = Path.GetExtension(fullPath);
3737
string sourceCode = File.ReadAllText(fullPath);
@@ -124,7 +124,7 @@ private void TraverseThroughAllTheProjectFiles(TokenParameter parameter, IFindRe
124124
{
125125
findReferenceProgressListener.OnFindReferenceStarted();
126126

127-
var projectCodeDirectory = this.GetProjectCodeDirectory(parameter.Username, parameter.Project);
127+
var projectCodeDirectory = new DirectoryInfo(this.applicationConfigurationProvider.GetProjectSourceCodePath(parameter.Username, parameter.Project));
128128

129129
var solutionPath = FindSolutionPath(parameter.Username, parameter.Project);
130130
if (solutionPath == null)
@@ -182,7 +182,7 @@ private void TraverseThroughAllTheProjectFiles(TokenParameter parameter, IFindRe
182182

183183
private string FindSolutionPath(string username, string project)
184184
{
185-
var projectCodeDirectory = this.GetProjectCodeDirectory(username, project);
185+
var projectCodeDirectory = new DirectoryInfo(this.applicationConfigurationProvider.GetProjectSourceCodePath(username, project));
186186
var solutions = projectCodeDirectory.GetFiles("*.sln", SearchOption.AllDirectories);
187187

188188
if (solutions.Length > 0)
@@ -200,16 +200,10 @@ private string FindSolutionPath(string username, string project)
200200
return null;
201201
}
202202

203-
private DirectoryInfo GetProjectCodeDirectory(string username, string project)
204-
{
205-
var projectSourceCodeDirectory = this.applicationConfigurationProvider.GetProjectSourceCodePath(username, project);
206-
return new DirectoryInfo(projectSourceCodeDirectory).GetDirectories()[0];
207-
}
208-
209203

210204
public void RewriteExternalDependencies(string username, string project)
211205
{
212-
var projectDirectory = this.GetProjectCodeDirectory(username, project);
206+
var projectDirectory = new DirectoryInfo(this.applicationConfigurationProvider.GetProjectSourceCodePath(username, project));
213207
var projectFiles = projectDirectory.GetFiles("*.csproj", SearchOption.AllDirectories);
214208
var msBuildExteionpath32 = Path.Combine(this.applicationConfigurationProvider.ApplicationRoot, "TargetFiles", "MSBuild-MS-VS");
215209
foreach (var projectFile in projectFiles)

SourceCodeReader.Web/LanguageServices/DotNet/DotNetSourceCodeSearchService.cs renamed to Web/LanguageServices/DotNet/DotNetSourceCodeSearchService.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private IndexSearcher Searcher
8787

8888
public void IndexProject(string username, string projectName, string projectDirectory)
8989
{
90-
var projectRoot = new DirectoryInfo(projectDirectory).GetDirectories()[0];
90+
var projectRoot = new DirectoryInfo(projectDirectory);
9191
string projectIdentifier = string.Format("{0}_{1}", username, projectName).ToLower();
9292
string indexCreatedStatusCheckFilePath = Path.Combine(this.applicationConfigurationProvider.ApplicationDataRoot, string.Format("{0}.status", projectIdentifier));
9393

@@ -216,7 +216,7 @@ public TokenResult FindExact(TokenParameter parameter)
216216
string filePath = document.Get(ItemPath);
217217
int location = Int32.Parse(document.Get(ItemLocation));
218218

219-
var projectCodeDirectory = this.GetProjectCodeDirectory(parameter.Username, parameter.Project);
219+
var projectCodeDirectory = this.applicationConfigurationProvider.GetProjectSourceCodePath(parameter.Username, parameter.Project);
220220
//string relativePath = filePath //projectCodeDirectory.MakeRelativePath(filePath);
221221
return new Models.TokenResult
222222
{
@@ -229,12 +229,6 @@ public TokenResult FindExact(TokenParameter parameter)
229229
return null;
230230
}
231231

232-
private DirectoryInfo GetProjectCodeDirectory(string username, string project)
233-
{
234-
var projectSourceCodeDirectory = this.applicationConfigurationProvider.GetProjectSourceCodePath(username, project);
235-
return new DirectoryInfo(projectSourceCodeDirectory).GetDirectories()[0];
236-
}
237-
238232
public Models.DocumentInfo GetFileDetails(string filePath)
239233
{
240234
var query = new TermQuery(new Term(ItemPath, filePath));
File renamed without changes.
File renamed without changes.

SourceCodeReader.Web/Services/GitHub/GitHubSourceCodeProviderService.cs renamed to Web/Services/GitHub/GitHubSourceCodeProviderService.cs

+102-17
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ public GitHubSourceCodeProviderService(
3737
public ProjectItem GetContent(string username, string project, string path, ISourceCodeOpeningProgress openingProgressListener)
3838
{
3939
var projectSourceCodePath = this.applicationConfigurationProvider.GetProjectSourceCodePath(username, project);
40+
string projectIdentifier = string.Format("{0}_{1}", username, project);
4041
try
4142
{
4243
if (!Directory.Exists(projectSourceCodePath))
4344
{
44-
string projectIdentifier = string.Format("{0}_{1}", username, project);
45-
object __projectSpecificLock = ProjectDownloadLock.GetOrAdd(projectIdentifier, new object());
46-
lock (__projectSpecificLock)
45+
bool projectExtracted = false;
46+
this.ExecuteWithInAProjectContext(projectIdentifier, () =>
4747
{
4848
if (!Directory.Exists(projectSourceCodePath))
4949
{
@@ -55,16 +55,17 @@ public ProjectItem GetContent(string username, string project, string path, ISou
5555
if (projectSelected == null)
5656
{
5757
openingProgressListener.OnProjectNotFound();
58-
return null;
58+
this.logger.Info("Couldn't find the specified the Github project {0}/{1}", username, project);
5959
}
60+
6061
openingProgressListener.OnProjectFound();
61-
62+
6263
openingProgressListener.OnProjectDownloadStarted();
6364
var downloadStatus = this.DownloadZipBall(packagePath, projectSelected.DownloadPackageUrl);
6465
if (!downloadStatus)
6566
{
6667
openingProgressListener.OnProjectDownloadFailed();
67-
return null;
68+
this.logger.Info("Failed to download Github project {0}/{1}", username, project);
6869
}
6970

7071
openingProgressListener.OnProjectDownloadCompleted();
@@ -74,33 +75,63 @@ public ProjectItem GetContent(string username, string project, string path, ISou
7475
this.ExtractZipBall(packagePath, projectSourceCodePath);
7576

7677
this.editorService.RewriteExternalDependencies(username, project);
78+
79+
projectExtracted = true;
7780
}
78-
}
81+
});
7982

80-
// Try to remove the created lock object
81-
ProjectDownloadLock.TryRemove(projectIdentifier, out __projectSpecificLock);
83+
if (!projectExtracted)
84+
{
85+
return null;
86+
}
8287
}
8388

84-
openingProgressListener.OnBuildingWorkspace();
89+
openingProgressListener.OnBuildingWorkspace();
8590
this.sourceCodeIndexingService.IndexProject(username, project, projectSourceCodePath);
8691
openingProgressListener.OnProjectLoaded();
8792

8893
var projectItem = GetContentFromPath(username, project, projectSourceCodePath, path);
8994
projectItem.DownloadedDate = Directory.GetCreationTimeUtc(projectSourceCodePath).ToString("dd MMM yyyy hh:mm:ss UTC");
9095
return projectItem;
9196
}
97+
catch (PathTooLongException ex)
98+
{
99+
this.ExecuteWithInAProjectContext(projectIdentifier, () =>
100+
{
101+
// Cleanup the extracted project directory
102+
if (Directory.Exists(projectSourceCodePath))
103+
{
104+
Directory.Delete(projectSourceCodePath, true);
105+
}
106+
});
107+
108+
openingProgressListener.OnProjectLoadingError();
109+
this.logger.Error(ex, "Path too long exception {0} from project {1}/{2} ", path, username, project);
110+
}
92111
catch (Exception ex)
93112
{
94113
openingProgressListener.OnProjectLoadingError();
95-
this.logger.Error(ex, "An error has occured while getting the content for path {0} from project {1}/{2}", path, username, project);
96-
return null;
114+
this.logger.Error(ex, "An error has occured while getting the content for path {0} from project {1}/{2} ", path, username, project);
115+
}
116+
117+
return null;
118+
}
119+
120+
private void ExecuteWithInAProjectContext(string projectIdentifier, Action codeToExecute)
121+
{
122+
object __projectSpecificLock = ProjectDownloadLock.GetOrAdd(projectIdentifier, new object());
123+
lock (__projectSpecificLock)
124+
{
125+
codeToExecute();
97126
}
127+
128+
// Try to remove the created lock object
129+
ProjectDownloadLock.TryRemove(projectIdentifier, out __projectSpecificLock);
98130
}
99131

100132
private ProjectItem GetContentFromPath(string username, string project, string projectSourceCodePath, string path)
101133
{
102-
var repositoryDirectory = new DirectoryInfo(projectSourceCodePath);
103-
DirectoryInfo applicationRoot = repositoryDirectory.GetDirectories()[0];
134+
DirectoryInfo applicationRoot = new DirectoryInfo(projectSourceCodePath);
104135
string currentDirectoryName = "root";
105136

106137
if (!string.IsNullOrEmpty(path))
@@ -208,12 +239,66 @@ private void ExtractZipBall(string zipFileToExtract, string destinationDirectory
208239
//Clean up the existing repository
209240
if (Directory.Exists(destinationDirectory))
210241
{
211-
Directory.Delete(destinationDirectory, true);
242+
DirectoryInfo directory = new DirectoryInfo(destinationDirectory);
243+
foreach (FileInfo file in directory.GetFiles()) file.Delete();
244+
foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
245+
}
246+
else
247+
{
248+
Directory.CreateDirectory(destinationDirectory);
212249
}
213250

214-
using (var zipFile = ZipFile.Read(zipFileToExtract))
251+
//using (var zipFile = ZipFile.Read(zipFileToExtract))
252+
//{
253+
// zipFile.ExtractAll(destinationDirectory, ExtractExistingFileAction.OverwriteSilently);
254+
//}
255+
256+
using (ZipInputStream zipStream = new ZipInputStream(zipFileToExtract))
257+
{
258+
string zipRootDirectoryName = string.Empty;
259+
ZipEntry zipEntry = zipStream.GetNextEntry();
260+
if (zipEntry == null)
261+
{
262+
return;
263+
}
264+
else
265+
{
266+
if (zipEntry.IsDirectory)
267+
{
268+
zipRootDirectoryName = zipEntry.FileName;
269+
}
270+
}
271+
272+
while ((zipEntry = zipStream.GetNextEntry()) != null)
273+
{
274+
string zipEntryPath = zipEntry.FileName;
275+
if (!string.IsNullOrEmpty(zipRootDirectoryName))
276+
{
277+
zipEntryPath = zipEntryPath.Replace(zipRootDirectoryName, string.Empty);
278+
}
279+
280+
if (zipEntry.IsDirectory)
281+
{
282+
Directory.CreateDirectory(Path.Combine(destinationDirectory, zipEntryPath));
283+
continue;
284+
}
285+
286+
string filename = Path.Combine(destinationDirectory, zipEntryPath);
287+
using (var fileStream = File.OpenWrite(filename))
288+
{
289+
CopyStream(zipStream, fileStream);
290+
}
291+
}
292+
}
293+
}
294+
295+
private static void CopyStream(Stream input, Stream output)
296+
{
297+
byte[] buffer = new byte[8 * 1024];
298+
int len;
299+
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
215300
{
216-
zipFile.ExtractAll(destinationDirectory, ExtractExistingFileAction.OverwriteSilently);
301+
output.Write(buffer, 0, len);
217302
}
218303
}
219304

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)