-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Description
Problem
The os.File Openfunctions always return the name of the file as it was presented to the Open function. I.e. opening a file with f, err := os.Open("huGO.txt") returns "huGO.txt" when f.Name() is called. This is all fine as long as one uses file names on the same operating system. However, it creates a problem, when one uses data from one operating system on another.
Suppose there is an application that calculates some properties for some files and stores the result in a file with key-value pairs where the key is the file name and the value is some property. The file names are supplied by the user. The program has a property generator and a property verifier command.
Now, suppose the program property generator is run on a Windows machine and there is a file called huGO.txt on the disk. The user supplies hugo.txt as the file name. Since the Windows file system is case-insensitive by default the function os.Open("hugo.txt") will open the file huGO.txt. The property generator can only save the file name as presented to the Open function which is the name entered by the user and therefore generates the following output when run on Windows:
"hugo.txt": "...property..."
Now the file and the property generator output are transferred to a Linux machine. There the verifier is run. The verifier reads the file names from the property file. So it will try to open a file called hugo.txt. However, the file name is huGO.txt. The Linux file system is case-sensitive by default and so the verifier fails to find the file on Linux and will output an error message. The verifier would work on Windows, but not on Linux.
This is an unsolvable problem in the current situation. When one stores file names that the user provided on an OS with a case-insensitive file system and wants to work with these file names on an OS with a case-sensitive file system this will inevitably fail if the user did not specify the file name exactly as it is stored on the file system. On Windows users rely on the fact that Windows is not case-sensitive, so this happens very often.
Solution
This could be solved by a function that returns the "real" file name as stored by the file system. If the file is opened with f, err := os.Open("hugo.txt") on Windows a function fn := f.FsName() would return huGO.txt in the above example.
This proposed os.File function FsName() would solve the above problem quite easily while still maintaining compatibility with older programs. The name of the function could be something else, like e.g. RealName() or whatever. There just should be a function with the specified functionality.