FileStore.CreateFile returns ntStatus = 3221226071

162 views Asked by At

I am using this SMBLibrary. Related to this closed issue, I sometimes get ntStatus = 3221226071 when I attempt to FileStore.CreateFile(). What does "DFS pathname not on local server" mean? If I keep trying, eventually it will work. Leads me to believe some resources are being held or not released/disconnected. Any ideas here?

"SMBLibrary" Version="1.4.8"

Here is my code:

[Fact]
public void Unit_Test_To_Test_SMBLibrary()
{
  var server = "myRemoteServer";
  var shareName = "shareddir";
  var windowsPath = "Data\\Folder1\\unittest";

  var random = new System.Random();
  var filename = "createdBySmbclient" + random.Next(1, 10).ToString() + ".txt";

  var domain = "my.domain";
  var username = "myUser";
  var password = "--put secret password here--";

  var client = new SMB2Client();
  bool isConnected = client.Connect(server, SMBTransportType.DirectTCPTransport);

  if(isConnected)
  {
      try
      {
          NTStatus ntStatus = client.Login(domain, username, password);
          if (ntStatus == NTStatus.STATUS_SUCCESS)
          {
              ISMBFileStore fileStore = client.TreeConnect(shareName, out ntStatus);

              object fileHandle;
              FileStatus fileStatus;

              var windowsPathWithFile = Path.Combine(windowsPath, filename);

              // 1st, create empty file.
              ntStatus = fileStore.CreateFile(
                  out fileHandle,
                  out fileStatus,
                  windowsPathWithFile,
                  AccessMask.GENERIC_READ | AccessMask.GENERIC_WRITE,
                  0,
                  ShareAccess.None,
                  CreateDisposition.FILE_OPEN_IF,
                  CreateOptions.FILE_NON_DIRECTORY_FILE,
                  null
              );

              // create file contents and get the bytes
              byte[] filebytes = Encoding.ASCII.GetBytes("hello world");

              // 2nd, write data to the newly created file
              if (ntStatus == NTStatus.STATUS_SUCCESS && fileStatus == FileStatus.FILE_CREATED)
              {
                  int numberOfBytesWritten;
                  ntStatus = fileStore.WriteFile(out numberOfBytesWritten, fileHandle, 0, filebytes);
                  fileStore.FlushFileBuffers(fileHandle);
                  fileStore.CloseFile(fileHandle);
                  fileStore.Disconnect();
                  _logger.LogDebug(string.Format("Export successful: {0}", windowsPathWithFile));
              }
              else
              {
                  throw new Exception(string.Format("ERROR: ntStatus = {0}, fileStatus = {1}", ntStatus, fileStatus));
              }
          }
      }
      finally
      {
          client.Logoff();
          client.Disconnect();
      }
   }
}
0

There are 0 answers