yellowfeather / DbfDataReader

DbfDataReader is a small fast .Net Core library for reading dBase, xBase, Clipper and FoxPro database files
MIT License
134 stars 61 forks source link

Do not mess with user files #215

Closed Balkoth closed 1 year ago

Balkoth commented 1 year ago

Describe the bug In the construcotr of DbfTable which takes a path to the table you change the attributes of the file in question to open:

public DbfTable(string path, Encoding encoding = null)
{
    if (!File.Exists(path)) throw new FileNotFoundException();

    Path = path;
    CurrentEncoding = encoding;

    // https://stackoverflow.com/questions/23559452/stream-reader-process-cannot-access-file-because-its-in-use-by-another-process
    File.SetAttributes(path, FileAttributes.Normal); // <-- Do not do this!
    Stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

    Init();

    var memoPath = MemoPath();
    if (!string.IsNullOrEmpty(memoPath)) Memo = CreateMemo(memoPath);
}

This for example clears the hidden state of the file. It is generally a bad idea to mess with files you have no control over. If the file can not be opened, because it is used by another process the code should just fail here. The developer using the library is responsible for taking the right actions if that happens.

chrisrichards commented 1 year ago

Thanks for pointing this out, fixed!