Unable to create file __temp__.tmp lazarus / 49-FPSpreadsheet ---------------------------------------------------------------------- krzynio · 2015-12-02 19:43 I moved my application with excel export capability to another network location and made the location read only for some users. Since that time I noticed that read only users who want to export data to excel worksheet (I use fpspreadsheet component for this) see such an error 'Unable to create file __temp__.tmp. I found the file name '__temp__.tmp in one of fpspreadsheet modules and I think that this file is used by zipper during export. The file is probably created on the drive where the program is started from. If I am right I suggest to modify the source and find users temporary directory and create the file in users temp folder. As an alternative to temporary folder which may be not easy to locate on different platforms the zip file may be created in path where original file will be stored. You can also provide a variable where the programmer will be able to store the path to his temporary folder. ---------------------------------------------------------------------- wp · 2015-12-02 21:29 To be honest, I don't remember why I added this temp filename when implementing the stream writers for xlsx and ods in last year's summer. At least now I see that writing to these formats seems to work if the line "FZip.FileName := '__temp__.tmp'" is removed from fpsOpendocument.pas and xlsxooxml.pas. This should resolve your read- only issue. I would appreciate if you could comment this line in fpsopendocument.pas and xlsxooxml.pas - there's only one occurance in each file - and test the modified version. ---------------------------------------------------------------------- rvk · 2015-12-02 22:38 Quote from: wp on December 02, 2015, 10:29:40 pmTo be honest, I don't remember why I added this temp filename when implementing the stream writers for xlsx and ods in last year's summer. At least now I see that writing to these formats seems to work if the line "FZip.FileName := '__temp__.tmp'" is removed from fpsOpendocument.pas and xlsxooxml.pas. This should resolve your read-only issue.I do This is related to my post, from last year. http://forum.lazarus.freepascal.org/index.php/topic,25142.msg152271.ht ml#msg152271 Back then I had problems creating a file larger than 256Kb (the internal maximum of a memory-zipper file). So zipper needs a temp- filename when you're working with larger files, otherwise it'll crash. Quote from: rvk on July 11, 2014, 02:20:32 pmBut in xlsxooxml.pas there isn't a Filename set for TZip in WriteToStream. So creating large spreadsheet will always crash. (I added FZip.FileName := 'temp___'; at line 618 and it was solved) Actually (fps)zipper should check if FileName is set and if it's not it should assign something to it before continuing (or give a proper exception). So be careful removing that line, and if you do, make sure you test it with very large xlsx and ods files. ---------------------------------------------------------------------- wp · 2015-12-02 23:34 Thanks for brushing up my brain... I ran the fpsSpeedTest for xlsx and ods with 40k rows which creates 20MB files, and did not see the crash that you mention. Strange... There have not been any changes with fpszipper, and only a few ones with zipper (but this is used only with fp >=2.7 and above). If you remember what you did last year could you please repeat this? (The current trunk version of fpspreadsheet has the filename assignment already removed). ---------------------------------------------------------------------- rvk · 2015-12-02 23:40 I remember my speedtest example crashed without the filename present. I'll try to reproduce it tomorrow with latest trunk. I'll also trace into the zip routine to see the condition when a filename is needed. As I remember, if the zipped file is above a certain point the filename is used and if it isn't assigned you'll get the crash. ---------------------------------------------------------------------- rvk · 2015-12-03 10:31 I kinda remember what the problem was. In zipper.pp (which trunk is using because  FPC_FULLVERSION >= 20701) there is this piece of code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure TZipper.ZipOneFile(Item : TZipFileEntry);...Begin  OpenInput(Item);  Try    StartZipFile(Item);    If (FInfile.Size<=FInMemSize) then      ZipStream:=TMemoryStream.Create    else      begin      TmpFileName:=ChangeFileExt(FFileName,'.tmp');      if TmpFileName=FFileName then        TmpFileName:=TmpFileName+'.tmp';(Try setting a breakpoint at the highlighted line and see what FFileName is) When FFileName is empty '', the TmpFileName will be ".tmp". This will normally work fine under Windows (but do we really want a filename which is empty with only .tmp as complete filename ???) Last year, when I tried this with some stress-testing, Windows came back with the message << Unable to create file ".tmp". >>. I just tried it with fpsspeedtest and although the .tmp is still used, it didn't crash for me anymore. But I still have a very bad feeling about using just .tmp as temporary filename. (I still feel that TZipper should check if the filename is empty and assign something arbitrary if it is.) ---------------------------------------------------------------------- wp · 2015-12-03 11:16 You are right - using ".tmp" as a filename will crash if the exe directory is not writable as required by the OP. I restored the removed lines in the xlsx and ods writers, but now the FileName is assigned to the result of "GetTempFilename" - the temp folder should always be writable. (But - as a recent discussion in the mailing list shows - this is not thread-safe, let's wait if this case ever happens -- (of course, it will...) ). ---------------------------------------------------------------------- engkin · 2015-12-03 14:35 Quote from: wp on December 03, 2015, 12:16:52 pm... now the FileName is assigned to the result of "GetTempFilename" ... this is not thread-safe ... Do you have any reason that prohibits using CreateGUID instead? ---------------------------------------------------------------------- krzynio · 2015-12-03 15:01 Quote from: wp on December 02, 2015, 10:29:40 pmTo be honest, I don't remember why I added this temp filename when implementing the stream writers for xlsx and ods in last year's summer. At least now I see that writing to these formats seems to work if the line "FZip.FileName := '__temp__.tmp'" is removed from fpsOpendocument.pas and xlsxooxml.pas. This should resolve your read-only issue. I would appreciate if you could comment this line in fpsopendocument.pas and xlsxooxml.pas - there's only one occurance in each file - and test the modified version. I commented the two lines and compiled the program then tested but not myself. I just uploaded it to application server and asked the user for generating report. She said that now XLS file was created. I will try to test it myself once again tomorrow evening when I will have access to to server but now it seems that removal solves the problem. Thank you. ---------------------------------------------------------------------- rvk · 2015-12-03 15:18 Quote from: krzynio on December 03, 2015, 04:01:47 pmI will try to test it myself once again tomorrow evening when I will have access to to server but now it seems that removal solves the problem. Thank you. As you can see from the above posts the FileName-lines are added again. Now with GetTempFilename, which should result in a filename in a temporary directory where the user always has write-access. So, when you test this for yourself, you might want to checkout the last revision 4400. QuoteRevision: 4400 fpspreadsheet: Restore usage of a temp file name for the zipper needed by xlsx and ods writers, now defined by GetTempfilename, instead of the constant '___TEMP.TMP___'. This should work even if the current folder is not writable. ---------------------------------------------------------------------- wp · 2015-12-03 15:19 You are saying that the xls file was created, but you were talking of xlsx before. The xls format does not need the zipper... Anyway: If you read the preceeding discussion with rvk, it is better to replace "FZip.Filename := '__temp__.tmp" by "FZip.FileName := GetTempFilename" instead of removing the line altogether because this creates the file in the temp folder in which any user is permitted to write. Quote from: engkin on December 03, 2015, 03:35:40 pmDo you have any reason that prohibits using CreateGUID instead? No, except for my laziness because GetTempFileName is "ready", and with CreateGUID I'd have to add the temp path and remove the braces - ok, I know, not too much of an effort... And I believe that this would not be the only part of fpspreadsheet which is not thread-safe. (But I never checked, I have to confess.) ---------------------------------------------------------------------- rvk · 2015-12-03 15:27 Quote from: wp on December 03, 2015, 04:19:45 pmQuote from: engkin on December 03, 2015, 03:35:40 pmDo you have any reason that prohibits using CreateGUID instead? No, except for my laziness because GetTempFileName is "ready", and with CreateGUID I'd have to add the temp path and remove the braces - ok, I know, not too much effort... The removal of the brackets is not necessary because every filesystem I know accepts them as part of the filename. But the question is, is it really necessary. Although GetTempFileName might not strictly be thread-safe, this will only be a problem when calling multiple write- processes at exactly the same time. I don't see that happening in case of fpspreadsheet. The GetTempFileName is already a lot better than just .tmp (which will certainly be a problem with two simultaneous write-processes). ---------------------------------------------------------------------- krzynio · 2015-12-03 16:55 Quote from: krzynio on December 03, 2015, 04:01:47 pmQuote from: wp on December 02, 2015, 10:29:40 pmTo be honest, I don't remember why I added this temp filename when implementing the stream writers for xlsx and ods in last year's summer. At least now I see that writing to these formats seems to work if the line "FZip.FileName := '__temp__.tmp'" is removed from fpsOpendocument.pas and xlsxooxml.pas. This should resolve your read-only issue. I would appreciate if you could comment this line in fpsopendocument.pas and xlsxooxml.pas - there's only one occurance in each file - and test the modified version. I commented the two lines and compiled the program then tested but not myself. I just uploaded it to application server and asked the user for generating report. She said that now XLS file was created. I will try to test it myself once again tomorrow evening when I will have access to to server but now it seems that removal solves the problem. Thank you. I have access to Excel now, in my case when I run  program with commented zip filename I also cannot create the file, just file name in error message has been changed from __temp___.tmp to .tmp. Replacing by GetTempFileName solved the problem. Thanks! ---------------------------------------------------------------------- rvk · 2015-12-03 16:57 Quote from: krzynio on December 03, 2015, 05:55:03 pmI have access to Excel now, in my case when I run  program with commented zip filename I also cannot create the file, just file name in error message has been changed from __temp___.tmp to .tmp That was what I already expected. The Filename is not the problem. Try the suggestion from wp: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FZip.FileName := GetTempFilename; ---------------------------------------------------------------------- wp · 2015-12-03 17:18 Ok then I resolve the bug tracker issue.