Stream write: Invalid File

I have a function that writes a file, a Stream.

noError() can usually catch bad filenames but not in this edge case where the filename contains a colon:

bool WriteToFile(string sFullLocalFileName, string sText)
{
    bool bDone = true
        noError()
        Stream oStream = write(sFullLocalFileName, CP_UTF8)
        string sError = lastError()

        if(null(sError)) {
                if(!null(oStream)) {
                        print("Writing to: " sFullLocalFileName "\n")
                        oStream << sText
                        close(oStream)
                        print("... but did it really???\n")
                } else {
                        bDone = false
                        print("null oStream\n")
                }
        } else {
                bDone = false
                print("sError: " sError "\n")
        }
        return bDone
}

// Try to write to an invalid file - FileName contains ':'
bool bDone = WriteToFile("C:\\Hello:world.txt", "Some Text")
print("bDone: " bDone "\n")

I end up with an empty file "C:\Hello", and a success return result.

If I pass "C:\\ Hello :world.txt" it creates a file called " Hello " (with leading and trailing spaces).

This can only be deleted with the dos command "del "\\?\c:\ Hello "

Other than looping the pre existing files in the folder and comparing post write, I can't think of a way to detect this and return a FALSE.

I guess I could manually parse the path for a 2nd colon, but then I need to know all valid\invalid path strings which I am having a tough time finding.

Any ideas?

-Adam


Adamarla - Tue Feb 11 13:35:10 EST 2014

Re: Stream write: Invalid File
Mathias Mamsch - Wed Feb 12 03:14:07 EST 2014

Hi Adam,

I remember the ":" syntax under windows. It has a special meaning. It refers to Streams ... It is something like meta data that you can attach to a file. So indeed "Hello:word.txt" is a valid filename. But due to the windows API Windows will create a file Hello and attach a Stream word.txt to it. It is a nice way of hiding stuff in Windows, since no native functions (explorer, etc) will show these streams.

Read more about it here:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364404%28v=vs.85%29.aspx

Regards, Mathias

Re: Stream write: Invalid File
Adamarla - Wed Feb 12 04:56:23 EST 2014

Mathias Mamsch - Wed Feb 12 03:14:07 EST 2014

Hi Adam,

I remember the ":" syntax under windows. It has a special meaning. It refers to Streams ... It is something like meta data that you can attach to a file. So indeed "Hello:word.txt" is a valid filename. But due to the windows API Windows will create a file Hello and attach a Stream word.txt to it. It is a nice way of hiding stuff in Windows, since no native functions (explorer, etc) will show these streams.

Read more about it here:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364404%28v=vs.85%29.aspx

Regards, Mathias

Thanks Mathias,

I have learnt something new today.

So I now force the default data stream which throws the error for bad filenames:

string WriteToFile(string sFullLocalFileName, string sText)
{
    noError()
    Stream oStream = write(sFullLocalFileName "::$DATA", CP_UTF8)
    string sError = lastError()
    if(!null(sError)) { return sError }
    oStream << sText
    close(oStream)
    return ""
}
bool WithToFile(string sFullLocalFileName, string sText)
{
    return null(WithToFile(sFullLocalFileName, sText))
}

// Try to write to an invalid file - FileName contains ':'
string sError = WriteToFile("C:\\Hello:world.txt", "Some Text")
print("sError: " sError "\n")

-Adam