Is. #681 - read-only files, and dirs (#926)

Tested in 3 majors OS'es... no problems... closes #681
This commit is contained in:
Geoff McLane 2021-04-14 18:02:09 +02:00 committed by GitHub
parent 8e70d4dfa2
commit 957ee4a47e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1125,19 +1125,26 @@ int TIDY_CALL tidyParseSource( TidyDoc tdoc, TidyInputSource* source )
return tidyDocParseSource( doc, source ); return tidyDocParseSource( doc, source );
} }
#ifdef WIN32
#define M_IS_DIR _S_IFDIR
#else // !WIN32
#define M_IS_DIR S_IFDIR
#endif
int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam ) int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam )
{ {
int status = -ENOENT; int status = -ENOENT;
FILE* fin = fopen( filnam, "r+" ); FILE* fin = 0;
struct stat sbuf = { 0 }; /* Is. #681 - read-only files */
if ( !fin ) if ( stat(filnam,&sbuf) != 0 )
{ {
TY_(ReportFileError)( doc, filnam, FILE_NOT_FILE ); TY_(ReportFileError)( doc, filnam, FILE_NOT_FILE );
return status; return status;
} }
if (sbuf.st_mode & M_IS_DIR) /* and /NOT/ if a DIRECTORY */
fclose( fin ); {
TY_(ReportFileError)(doc, filnam, FILE_NOT_FILE);
return status;
}
#ifdef _WIN32 #ifdef _WIN32
return TY_(DocParseFileWithMappedFile)( doc, filnam ); return TY_(DocParseFileWithMappedFile)( doc, filnam );
@ -1147,7 +1154,6 @@ int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam )
#if PRESERVE_FILE_TIMES #if PRESERVE_FILE_TIMES
{ {
struct stat sbuf = { 0 };
/* get last modified time */ /* get last modified time */
TidyClearMemory(&doc->filetimes, sizeof(doc->filetimes)); TidyClearMemory(&doc->filetimes, sizeof(doc->filetimes));
if (fin && cfgBool(doc, TidyKeepFileTimes) && if (fin && cfgBool(doc, TidyKeepFileTimes) &&