@@ -676,32 +676,48 @@ bool status_known(const basic_file_status &s);
676676// / platform-specific error_code.
677677std::error_code status_known (const Twine &path, bool &result);
678678
679- enum OpenFlags : unsigned {
680- F_None = 0 ,
681-
682- // / F_Excl - When opening a file, this flag makes raw_fd_ostream
683- // / report an error if the file already exists.
684- F_Excl = 1 ,
679+ enum CreationDisposition : unsigned {
680+ // / CD_CreateAlways - When opening a file:
681+ // / * If it already exists, truncate it.
682+ // / * If it does not already exist, create a new file.
683+ CD_CreateAlways = 0 ,
684+
685+ // / CD_CreateNew - When opening a file:
686+ // / * If it already exists, fail.
687+ // / * If it does not already exist, create a new file.
688+ CD_CreateNew = 1 ,
689+
690+ // / CD_OpenAlways - When opening a file:
691+ // / * If it already exists, open the file with the offset set to 0.
692+ // / * If it does not already exist, fail.
693+ CD_OpenExisting = 2 ,
694+
695+ // / CD_OpenAlways - When opening a file:
696+ // / * If it already exists, open the file with the offset set to 0.
697+ // / * If it does not already exist, create a new file.
698+ CD_OpenAlways = 3 ,
699+ };
685700
686- // / F_Append - When opening a file, if it already exists append to the
687- // / existing file instead of returning an error. This may not be specified
688- // / with F_Excl.
689- F_Append = 2 ,
701+ enum FileAccess : unsigned {
702+ FA_Read = 1 ,
703+ FA_Write = 2 ,
704+ };
690705
691- // / F_NoTrunc - When opening a file, if it already exists don't truncate
692- // / the file contents. F_Append implies F_NoTrunc, but F_Append seeks to
693- // / the end of the file, which F_NoTrunc doesn't.
694- F_NoTrunc = 4 ,
706+ enum OpenFlags : unsigned {
707+ OF_None = 0 ,
708+ F_None = 0 , // For compatibility
695709
696710 // / The file should be opened in text mode on platforms that make this
697711 // / distinction.
698- F_Text = 8 ,
712+ OF_Text = 1 ,
713+ F_Text = 1 , // For compatibility
699714
700- // / Open the file for read and write.
701- F_RW = 16 ,
715+ // / The file should be opened in append mode.
716+ OF_Append = 2 ,
717+ F_Append = 2 , // For compatibility
702718
703719 // / Delete the file on close. Only makes a difference on windows.
704- F_Delete = 32
720+ OF_Delete = 4
705721};
706722
707723// / Create a uniquely named file.
@@ -824,6 +840,15 @@ inline OpenFlags &operator|=(OpenFlags &A, OpenFlags B) {
824840 return A;
825841}
826842
843+ inline FileAccess operator |(FileAccess A, FileAccess B) {
844+ return FileAccess (unsigned (A) | unsigned (B));
845+ }
846+
847+ inline FileAccess &operator |=(FileAccess &A, FileAccess B) {
848+ A = A | B;
849+ return A;
850+ }
851+
827852// / @brief Opens the file with the given name in a write-only or read-write
828853// / mode, returning its open file descriptor. If the file does not exist, it
829854// / is created.
@@ -840,7 +865,45 @@ inline OpenFlags &operator|=(OpenFlags &A, OpenFlags B) {
840865// / @returns errc::success if \a Name has been opened, otherwise a
841866// / platform-specific error_code.
842867std::error_code openFileForWrite (const Twine &Name, int &ResultFD,
843- OpenFlags Flags, unsigned Mode = 0666 );
868+ CreationDisposition Disp = CD_CreateAlways,
869+ OpenFlags Flags = OF_None,
870+ unsigned Mode = 0666 );
871+
872+ // / @brief Opens the file with the given name in a write-only or read-write
873+ // / mode, returning its open file descriptor. If the file does not exist, it
874+ // / is created.
875+ // /
876+ // / The caller is responsible for closing the freeing the file once they are
877+ // / finished with it.
878+ // /
879+ // / @param Name The path of the file to open, relative or absolute.
880+ // / @param Flags Additional flags used to determine whether the file should be
881+ // / opened in, for example, read-write or in write-only mode.
882+ // / @param Mode The access permissions of the file, represented in octal.
883+ // / @returns a platform-specific file descriptor if \a Name has been opened,
884+ // / otherwise an error object.
885+ Expected<file_t > openNativeFileForWrite (const Twine &Name,
886+ CreationDisposition Disp,
887+ OpenFlags Flags, unsigned Mode = 0666 );
888+
889+ // / @brief Opens the file with the given name in a write-only or read-write
890+ // / mode, returning its open file descriptor. If the file does not exist, it
891+ // / is created.
892+ // /
893+ // / The caller is responsible for closing the file descriptor once they are
894+ // / finished with it.
895+ // /
896+ // / @param Name The path of the file to open, relative or absolute.
897+ // / @param ResultFD If the file could be opened successfully, its descriptor
898+ // / is stored in this location. Otherwise, this is set to -1.
899+ // / @param Flags Additional flags used to determine whether the file should be
900+ // / opened in, for example, read-write or in write-only mode.
901+ // / @param Mode The access permissions of the file, represented in octal.
902+ // / @returns errc::success if \a Name has been opened, otherwise a
903+ // / platform-specific error_code.
904+ std::error_code openFileForReadWrite (const Twine &Name, int &ResultFD,
905+ CreationDisposition Disp, OpenFlags Flags,
906+ unsigned Mode = 0666 );
844907
845908// / @brief Opens the file with the given name in a write-only or read-write
846909// / mode, returning its open file descriptor. If the file does not exist, it
@@ -855,8 +918,10 @@ std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
855918// / @param Mode The access permissions of the file, represented in octal.
856919// / @returns a platform-specific file descriptor if \a Name has been opened,
857920// / otherwise an error object.
858- Expected<file_t > openNativeFileForWrite (const Twine &Name, OpenFlags Flags,
859- unsigned Mode = 0666 );
921+ Expected<file_t > openNativeFileForReadWrite (const Twine &Name,
922+ CreationDisposition Disp,
923+ OpenFlags Flags,
924+ unsigned Mode = 0666 );
860925
861926// / @brief Opens the file with the given name in a read-only mode, returning
862927// / its open file descriptor.
@@ -873,6 +938,7 @@ Expected<file_t> openNativeFileForWrite(const Twine &Name, OpenFlags Flags,
873938// / @returns errc::success if \a Name has been opened, otherwise a
874939// / platform-specific error_code.
875940std::error_code openFileForRead (const Twine &Name, int &ResultFD,
941+ OpenFlags Flags = OF_None,
876942 SmallVectorImpl<char > *RealPath = nullptr );
877943
878944// / @brief Opens the file with the given name in a read-only mode, returning
@@ -888,7 +954,7 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
888954// / @returns a platform-specific file descriptor if \a Name has been opened,
889955// / otherwise an error object.
890956Expected<file_t >
891- openNativeFileForRead (const Twine &Name,
957+ openNativeFileForRead (const Twine &Name, OpenFlags Flags = OF_None,
892958 SmallVectorImpl<char > *RealPath = nullptr );
893959
894960// / @brief Close the file object. This should be used instead of ::close for
0 commit comments