Validation of USB mass storage write completion

During testing to the USB mass storage it appears it does lazy writing even when calling flush() on the FileStream and FileSystem is this correct?

If so how do we validate if writes have actually be written to the USB stick?

Both of them do same thing.

What do you mean lazy? If i remember correctly, auto flush if cache is full or every 60 seconds. You can call Flush() any time.

Lazy meaning that when writing continuously a small string to a new file and calling flush each time. It was 50 files behind writing after 340 started it had completed 290 when checked on the PC.

The files from 290 to 340 was created on the USB stick but had no content.

static void USBWriteTest(int writesCnt)
        {
            USBController sc = new USBController();
            IDriveProvider drive = sc.Driver;
            int i = 0;
            while (writesCnt > 0)
            {
                var directory = new DirectoryInfo(drive.Name);
                var files = directory.GetFiles();

                foreach (var f in files)
                {
                    System.Diagnostics.Debug.WriteLine(f.Name);
                }

                //JSON_Test();
                //Create a text file and save it to the SD card.
                string s = HttpsTest();
                var file = new FileStream($@"{drive.Name}Test{i}.txt", FileMode.OpenOrCreate);
                //JToken jt = JsonConverter.Serialize(inst);
                //string s = jt.ToString();
                var bytes = Encoding.UTF8.GetBytes(s);
                file.Write(bytes, 0, bytes.Length);

                file.Flush();
                file.Close();
                GC.Collect();
                FileSystem.Flush(sc.Controller.Hdc);
                writesCnt--;
                i++;
                Thread.Sleep(1000);
            }
        }

The Thread.Sleep(1000); might not have been included when during the write test.

Hi, there are 3 things:

  1. string s = HttpsTest() could return string.Empty, and this case the saved file is empty, that you think System lazy to Flush().
    Check ‘s’ is not empty or plus something else for sure.
  2. We recommend FAT32. For FAT, as I remember there is limited 512 files on single folder or 128 on root folder. Second reason is, 99% of the cases tested with FAT32.
  3. Make sure format by window tool, not third party tool.

image

Hey

You may indeed have to been right about case 1. I’am unable to reproduce my observations with a hardcode string.