diff --git a/ide/dialogprocs.pas b/ide/dialogprocs.pas index 640ca3d480..b7b643dcd6 100644 --- a/ide/dialogprocs.pas +++ b/ide/dialogprocs.pas @@ -131,10 +131,12 @@ function LoadCodeBuffer(var ACodeBuffer: TCodeBuffer; const AFilename: string; Flags: TLoadBufferFlags): TModalResult; var ACaption, AText: string; + FileReadable: boolean; begin repeat + FileReadable:=true; if (lbfCheckIfText in Flags) - and FileExists(AFilename) and (not FileIsText(AFilename)) + and (not FileIsText(AFilename,FileReadable)) and FileReadable then begin if lbfQuiet in Flags then begin Result:=mrCancel; @@ -147,8 +149,12 @@ begin end; if Result<>mrOk then break; end; - ACodeBuffer:=CodeToolBoss.LoadFile(AFilename,lbfUpdateFromDisk in Flags, - lbfRevert in Flags); + if FileReadable then + ACodeBuffer:=CodeToolBoss.LoadFile(AFilename,lbfUpdateFromDisk in Flags, + lbfRevert in Flags) + else + ACodeBuffer:=nil; + if ACodeBuffer<>nil then begin Result:=mrOk; end else begin diff --git a/ide/main.pp b/ide/main.pp index c8dcd2e041..aea457cdd7 100644 --- a/ide/main.pp +++ b/ide/main.pp @@ -6544,6 +6544,7 @@ var Ext,AText,ACaption: string; NewBuf: TCodeBuffer; LastDesigner: TDesigner; AnUnitInfo: TUnitInfo; + FileReadable: Boolean; begin // close the old project if SomethingOfProjectIsModified then begin @@ -6576,7 +6577,7 @@ begin Result:=MessageDlg(ACaption, AText, mtError, [mbAbort], 0); exit; end; - + // if there is a project info file, load that instead if (Ext<>'.lpi') and (FileExists(ChangeFileExt(AFileName,'.lpi'))) then begin // load instead of program file the project info file @@ -6584,13 +6585,19 @@ begin Ext:='.lpi'; end; - if (not FileIsText(AFilename)) then begin + if (not FileIsText(AFilename,FileReadable)) and FileReadable then begin ACaption:=lisFileNotText; AText:=Format(lisFileDoesNotLookLikeATextFileOpenItAnyway, ['"', AFilename, '"', #13, #13]); Result:=MessageDlg(ACaption, AText, mtConfirmation, [mbYes, mbAbort], 0); if Result=mrAbort then exit; end; + if not FileReadable then begin + Result:=QuestionDlg('Unable to read file', + 'Unable to read file "'+AFilename+'".', + mtError,[mrCancel,'Skip file',mrAbort,'Abort all loading'],0); + exit; + end; if ofAddToRecent in Flags then AddRecentProjectFileToEnvironment(AFileName); diff --git a/lcl/fileutil.pas b/lcl/fileutil.pas index b7058b9ff1..835eb5c6f1 100644 --- a/lcl/fileutil.pas +++ b/lcl/fileutil.pas @@ -43,6 +43,7 @@ procedure CheckIfFileIsSymlink(const AFilename: string); function FileIsReadable(const AFilename: string): boolean; function FileIsWritable(const AFilename: string): boolean; function FileIsText(const AFilename: string): boolean; +function FileIsText(const AFilename: string; out FileReadable: boolean): boolean; function FileIsExecutable(const AFilename: string): boolean; function FileIsSymlink(const AFilename: string): boolean; function GetFileDescription(const AFilename: string): string; diff --git a/lcl/include/fileutil.inc b/lcl/include/fileutil.inc index e5e22dbd0a..f3f5f5c568 100644 --- a/lcl/include/fileutil.inc +++ b/lcl/include/fileutil.inc @@ -397,12 +397,22 @@ end; function FileIsText(const AFilename: string): boolean; ------------------------------------------------------------------------------} function FileIsText(const AFilename: string): boolean; +var + FileReadable: Boolean; +begin + Result:=FileIsText(AFilename,FileReadable); + if FileReadable then ; +end; + +function FileIsText(const AFilename: string; out FileReadable: boolean + ): boolean; var fs: TFileStream; Buf: string; Len, i: integer; NewLine: boolean; begin Result:=false; + FileReadable:=true; try fs:=TFileStream.Create(AFilename,fmOpenRead); try @@ -427,6 +437,9 @@ begin fs.Free; end; except + on E: Exception do begin + FileReadable:=false; + end; end; end;