Libgit2sharp: Dispose doesn't free all files

Created on 19 Jun 2014  路  4Comments  路  Source: libgit2/libgit2sharp

Disposing repository after fetch operation doesn't seem to free all files. Tested on windows using latest package from nuget.

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testrepo");

using (var repo = new Repository(Repository.Init(path, isBare: true)))
{
    var remote = repo.Network.Remotes.Add("origin", "https://github.com/libgit2/TestGitRepository.git", "+refs/*:refs/*");
    repo.Network.Fetch(remote);
    repo.Network.Remotes.Remove("origin");
}

Directory.Delete(path, true);
System.UnauthorizedAccessException was unhandled
  HResult=-2147024891
  Message=Access to the path 'pack-773b425dab536d28aaeaf2b8f310c9c25f256087.idx' is denied.
  Source=mscorlib
  StackTrace:
       at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound)
       at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost)
       at System.IO.Directory.Delete(String path, Boolean recursive)
       at ConsoleApplication1.Program.Main(String[] args) in c:\Users\Luk谩拧\Documents\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 24
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Most helpful comment

for the next person who finds this

public static void DeleteDirectory(string directoryPath)
{
    if (!Directory.Exists(directoryPath))
    {
        return;
    }

    var files = Directory.GetFiles(directoryPath);
    var directories = Directory.GetDirectories(directoryPath);

    foreach (var file in files)
    {
        File.SetAttributes(file, FileAttributes.Normal);
        File.Delete(file);
    }

    foreach (var dir in directories)
    {
        DeleteDirectory(dir);
    }

    File.SetAttributes(directoryPath, FileAttributes.Normal);

    Directory.Delete(directoryPath, false);
}

All 4 comments

Are you sure this is because the file is open? The message looks like the one you'd get when the reason is that you're trying to remove a read-only file, which Windows does not like, and the objects git's object database (of which that index file is one) are read-only.

You are right, I was not aware of that behavior. Removing readonly flag allows me to delete the whole directory. Thank you for the swift support :)

for the next person who finds this

public static void DeleteDirectory(string directoryPath)
{
    if (!Directory.Exists(directoryPath))
    {
        return;
    }

    var files = Directory.GetFiles(directoryPath);
    var directories = Directory.GetDirectories(directoryPath);

    foreach (var file in files)
    {
        File.SetAttributes(file, FileAttributes.Normal);
        File.Delete(file);
    }

    foreach (var dir in directories)
    {
        DeleteDirectory(dir);
    }

    File.SetAttributes(directoryPath, FileAttributes.Normal);

    Directory.Delete(directoryPath, false);
}

You don't have to reset the attributes on the directory itself on Windows. The read-only attribute is not applied as 'read-only' there

Was this page helpful?
0 / 5 - 0 ratings