VirtualFiles gets mixed up

Discuss any general programming issues here
Post Reply
amyren
Posts: 352
Joined: Thu May 02, 2019 11:53 am

VirtualFiles gets mixed up

Post by amyren »

I have been using virtualfiles in my program like shown in the code below.
This seemed to work fine until I noticed that the content in two of my files got mixed.
It was the content inside the pris.dat and premiering.dat that got merged together, and content of both files became messed up and identical.
All other files was acting normally.

Probably have been reading myself blind here, because I've been going over this code many many times now without being able to see what cause the mixup. To troubleshoot I did try to change those two mentioned files into using temporary physical files instead of virtual. And that solved the issue for those files, but then the two next virtualfiles started to get mixed up.

Secondly, I did have an issue with that last UndefineVirtualStringFile command somtimes gave me an error, hence the added ExitOnError(False).

I have avoided the problem now by making all these files temporary files. But still I would like to know what caused the problem in the first case.

Code: Select all

Function p_loadfromserver()
	If IsOnline()
		premieringfil$  = DefineVirtualFileFromString("", "premiering.dat", True)
		prisfil$  = DefineVirtualFileFromString("", "pris.dat", True)
		notatfil$  = DefineVirtualFileFromString("", "notat.dat", True)
		renninfofil$ = DefineVirtualFileFromString("", "renninfo_"..year$..".dat", True)		
		deltakerfil2$ = DefineVirtualFileFromString("", "rennliste_"..year$..".dat", True)
		deltakerfil$ = DefineVirtualFileFromString("", "deltakerliste.dat", True)
		ExitOnError(False)
		fil_dl$ = server_dl_config$.."deltakerliste.dat"
		fil_dl2$ = server_dl_config$.."rennliste_"..year$..".dat"
		fil_dlprem$ = server_dl_config$.."/premiering.dat"
		fil_dlpris$ = server_dl_config$.."/pris.dat"
		fil_dlnotat$ = server_dl_config$.."/notat.dat"
		fil_dlinfo$ = server_dl_config$.."/renninfo_"..year$..".dat"

		DownloadFile(fil_dlprem$, {File = premieringfil$, Fail404 = True, Adapter = "hurl"})
		DownloadFile(fil_dlpris$, {File = prisfil$, Fail404 = True, Adapter = "hurl"})
		DownloadFile(fil_dlnotat$, {File = notatfil$, Fail404 = True, Adapter = "hurl"})
		DownloadFile(fil_dlinfo$, {File = renninfofil$, Fail404 = True, Adapter = "hurl"})
		DownloadFile(fil_dl2$, {File = deltakerfil2$, Fail404 = True, Adapter = "hurl"})
		DownloadFile(fil_dl$, {File = deltakerfil$, Fail404 = True, Adapter = "hurl"})

		code=GetLastError()
		ExitOnError(True)
		If code<>0
			SystemRequest("Nedlasting feilet", "Server nede eller annen\nnettverksfeil.","OK")
		Else
			If Exists(deltakerfil$)
				moai.DoMethod("lv", "clear")
				OpenFile(1, deltakerfil$, #MODE_READ)
					deltakerliste = ReadTable(1, {Adapter = "default"})
				CloseFile(1)
				For i = 0 To ListItems(deltakerliste)-1
					etternavn$ = deltakerliste[i][0]
					fornavn$ = deltakerliste[i][1]
					telefon$ = deltakerliste[i][2]
					epost$ = deltakerliste[i][3]
					fdato$ = deltakerliste[i][4]
					kjonn$ = deltakerliste[i][5]
					deltatt$ = deltakerliste[i][6]
					antall$ = deltakerliste[i][7]
					kommentar$ = deltakerliste[i][8]
					regnr$ = deltakerliste[i][9]
				moai.DoMethod("lv", "insert", "bottom", etternavn$, fornavn$, telefon$, epost$, fdato$, kjonn$, deltatt$, antall$, kommentar$, regnr$)
				Next
			EndIf			
			If Exists(deltakerfil2$)
				If FileSize(deltakerfil2$)>1
					DebugPrint(FileSize(deltakerfil2$))
					moai.DoMethod("lv2", "clear")
					OpenFile(1, deltakerfil2$, #MODE_READ)
						If FileSize(deltakerfil2$) > 1
							deltakerliste2 = ReadTable(1, {Adapter = "default"})
							For i = 0 To ListItems(deltakerliste2)-1
								startnr$ = deltakerliste2[i][0]
								betalt$ = deltakerliste2[i][1]
								betmetode$ = deltakerliste2[i][2]
								tid$ = deltakerliste2[i][3]
								klasse$ =deltakerliste2[i][4]
								etternavn2$ = deltakerliste2[i][5]
								fornavn2$ = deltakerliste2[i][6]
								telefon2$ = deltakerliste2[i][7]
								epost2$ = deltakerliste2[i][8]
								fdato2$ = deltakerliste2[i][9]
								kjonn2$ = deltakerliste2[i][10]
								deltatt2$ = deltakerliste2[i][11]
								antall2$ = deltakerliste2[i][12]
								kommentar2$ = deltakerliste2[i][13]
								regnr2$ = deltakerliste2[i][14]
								moai.DoMethod("lv2", "insert", "bottom", startnr$, betalt$, betmetode$, tid$, klasse$, etternavn2$, fornavn2$, telefon2$, epost2$, fdato2$, kjonn2$, deltatt2$, antall2$, kommentar2$, regnr2$)
							Next
						EndIf
					CloseFile(1)
				EndIf
			EndIf	
			If Exists(premieringfil$)
				premiering$, len = FileToString(premieringfil$)
				DebugPrint("PREMIE:"..premiering$)
			EndIf
			If Exists(prisfil$)
				pris$, len = FileToString(prisfil$)
				DebugPrint("PRIS:"..pris$)
			EndIf
			If Exists(notatfil$)
				notat$, len = FileToString(notatfil$)
			EndIf
			If Exists(renninfofil$)
				informasjon$, len = FileToString(renninfofil$)
			EndIf	
		EndIf
		UndefineVirtualStringFile(premieringfil$)
		UndefineVirtualStringFile(prisfil$)
		UndefineVirtualStringFile(notatfil$)
		UndefineVirtualStringFile(renninfofil$)
		UndefineVirtualStringFile(deltakerfil2$)
		UndefineVirtualStringFile(deltakerfil$)
	Else
		SystemRequest("Nettverksfeil", "Sjekk internettforbindelse", "OK")
	EndIf
EndFunction

Function p_save2server()
	sel = moai.Request("Last opp data til server?", "Bekreft at du vil overskrive data\npå serveren. Last opp?", "Yes|No")
	Switch sel
	Case 1:
		If IsOnline()
			deltakerliste = CreateList()
			For i = 0 To moai.Get("lv", "Entries")-1
				Local etternavn$, fornavn$, telefon$, epost$, fdato$, kjonn$, deltatt$, antall$, kommentar$, regnr$ = moai.DoMethod("lv", "getentry", i)
				InsertItem(deltakerliste, {etternavn$, fornavn$, telefon$, epost$, fdato$, kjonn$, deltatt$, antall$, kommentar$, regnr$})
			Next
			premieringfil$  = DefineVirtualFileFromString("", "premiering.dat", True)
			prisfil$  = DefineVirtualFileFromString("", "pris.dat", True)
			notatfil$  = DefineVirtualFileFromString("", "notat.dat", True)
			renninfofil$ = DefineVirtualFileFromString("", "renninfo_"..year$..".dat", True)
			deltakerfil2$ = DefineVirtualFileFromString("", "rennliste_"..year$..".dat", True)
			deltakerfil$ = DefineVirtualFileFromString("", "deltakerliste.dat", True)
			If moai.Get("lv2", "Entries") > 0 
				deltakerliste2 = CreateList()
				For i = 0 To moai.Get("lv2", "Entries")-1
					Local startnr$, betalt$, betmetode$, tid$, klasse$, etternavn2$, fornavn2$, telefon2$, epost2$, fdato2$, kjonn2$, deltatt2$, antall2$, kommentar2$, regnr2$ = moai.DoMethod("lv2", "getentry", i)
					InsertItem(deltakerliste2, {startnr$, betalt$, betmetode$, tid$, klasse$, etternavn2$, fornavn2$, telefon2$, epost2$, fdato2$, kjonn2$, deltatt2$, antall2$, kommentar2$, regnr2$})
				Next
				OpenFile(1, deltakerfil2$, #MODE_WRITE)
					WriteTable(1, deltakerliste2, {Adapter = "Default"})
				CloseFile(1)
			Else
				StringToFile("", deltakerfil2$)
			EndIf
			StringToFile(premiering$, premieringfil$)
		OpenFile(1, premieringfil$)
			While Not Eof(1)
				DebugPrint(ReadLine(1))
			Wend
		CloseFile(1)
			StringToFile(pris$, prisfil$)
			StringToFile(notat$, notatfil$)
			StringToFile(informasjon$, renninfofil$)
			OpenFile(1, deltakerfil$, #MODE_WRITE)
				WriteTable(1, deltakerliste, {Adapter = "Default"})
			CloseFile(1)
			uploadfileprem$ = server_ul_config$.."/premiering.dat"
			uploadfilepris$ = server_ul_config$.."/pris.dat"
			uploadfilenotat$ = server_ul_config$.."/notat.dat"
			uploadfileinfo$ = server_ul_config$.."/renninfo_"..year$..".dat"
			uploadfile2$ = server_ul_config$.."/rennliste_"..year$..".dat"
			uploadfile$ = server_ul_config$.."/deltakerliste.dat"

			UploadFile(uploadfile$, {File = deltakerfil$, Adapter = "hurl"})
			UploadFile(uploadfile2$, {File = deltakerfil2$, Adapter = "hurl"})
			UploadFile(uploadfileprem$, {File = premieringfil$, Adapter = "hurl"})
			UploadFile(uploadfilepris$, {File = prisfil$, Adapter = "hurl"})
			UploadFile(uploadfilenotat$, {File = notatfil$, Adapter = "hurl"})
			UploadFile(uploadfileinfo$, {File = renninfofil$, Adapter = "hurl"})
	
			UndefineVirtualStringFile(premieringfil$)
			UndefineVirtualStringFile(prisfil$)
			UndefineVirtualStringFile(notatfil$)
			UndefineVirtualStringFile(renninfofil$)
			UndefineVirtualStringFile(deltakerfil2$)
			ExitOnError(False)
			UndefineVirtualStringFile(deltakerfil$)
		Else
			SystemRequest("Nettverksfeil", "Sjekk internettforbindelse", "OK")
		EndIf
	EndSwitch
EndFunction
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: VirtualFiles gets mixed up

Post by airsoftsoftwair »

That is way too much code. You need to cut this down into an MCVE :)
amyren
Posts: 352
Joined: Thu May 02, 2019 11:53 am

Re: VirtualFiles gets mixed up

Post by amyren »

Perhaps I will try to dig deeper and create a MCVE when I get the time.
I did try to make one, but I think there must be something else in my code somewhere that interferes with the p_loadfromsever function. Because when trying to isolate it and create a smaller script with only that function, the problem did not apear.

What I did now is splitting the loading into smaller parts, so that one virtual file is defined, loaded and undefined before defining the next virtual file. When having only one virtual file active at the time then everything work without problems.
amyren
Posts: 352
Joined: Thu May 02, 2019 11:53 am

Re: VirtualFiles gets mixed up

Post by amyren »

Still some code I'm afraid, but this is the smallest MCVE I was able to make of it.

To test, just run the script as is, and press the Load button once the splash window has gone.
Two text files will be downloaded and debugprinted.
file1 have 4 lines, starting with "File 1, line1"
file 2 is similar, but only one line.

Notice the debug output from those two files is merged together, and both files will be printed with 4 lines.
Next, delete the section in the script containing the splash window, and run the script again.
This time the output will look like it is supposed to, first 4 lines from file 1, and then one line from file 2.

Now, why does the existence of the splash window have this effect on the script?

Code: Select all

@VERSION 6,1
@APPTITLE "Test"
@REQUIRE "RapaGUI"
@REQUIRE "hurl"

Function p_EventFunc(msg)
	Switch msg.action
	Case "RapaGUI":
		Switch msg.attribute
		Case "CloseRequest":
			End
		Case "Pressed":
			Switch msg.id						
			Case "Exit"
				End	
			Case "Load"
				p_load
			EndSwitch
		EndSwitch
	EndSwitch
EndFunction

Function p_load()
	listfile1$  = DefineVirtualFileFromString("", "list1.dat", True)
	listfile2$  = DefineVirtualFileFromString("", "list2.dat", True)
	DownloadFile("https://amyren.info/testarea//list1.dat", {File = listfile1$, Fail404 = True, Adapter = "hurl"})
	DownloadFile("https://amyren.info/testarea//list2.dat", {File = listfile2$, Fail404 = True, Adapter = "hurl"})
	list1$, len = FileToString(listfile1$)
	DebugPrint(list1$)
	list2$, len = FileToString(listfile2$)
	DebugPrint(list2$)
	UndefineVirtualStringFile(listfile1$)
	UndefineVirtualStringFile(listfile2$)
EndFunction

moai.CreateApp([[<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
	<window id="win" title="TEST" notify="CloseRequest">
		<vgroup>
			<hgroup>
				<button id="Load">Load</button>
				<button id="Exit">Exit</button>
			</hgroup>				
		</vgroup>
	</window>		
</application>]])

InstallEventHandler({RapaGUI = p_EventFunc})

;open Splash window
moai.CreateObject([[
<window id="splash" title="Splash">
<vgroup>
<textview>SPLASH!</textview>
</vgroup>
</window>
]])
moai.DoMethod("app", "addwindow", "splash")
moai.Set("splash", "open", True)
SetTimeout(Nil, Function() moai.Set("splash", "open", False) EndFunction, 1000) ; closes the window after 1 second
	
Repeat
	WaitEvent
Forever

Also by restructuring the p_load funtion like this, it will work with even the splash window

Code: Select all

Function p_load()
	listfile1$  = DefineVirtualFileFromString("", "list1.dat", True)
	DownloadFile("https://amyren.info/testarea//list1.dat", {File = listfile1$, Fail404 = True, Adapter = "hurl"})
	list1$, len = FileToString(listfile1$)
	DebugPrint(list1$)
	UndefineVirtualStringFile(listfile1$)	
	listfile2$  = DefineVirtualFileFromString("", "list2.dat", True)
	DownloadFile("https://amyren.info/testarea//list2.dat", {File = listfile2$, Fail404 = True, Adapter = "hurl"})
	list2$, len = FileToString(listfile2$)
	DebugPrint(list2$)
	UndefineVirtualStringFile(listfile2$)
EndFunction
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: VirtualFiles gets mixed up

Post by airsoftsoftwair »

Which platforms have you tested this on? Does the problem occur only on Amiga or also on Windows?
amyren
Posts: 352
Joined: Thu May 02, 2019 11:53 am

Re: VirtualFiles gets mixed up

Post by amyren »

Sorry, should have specified that.
Windows 10 x64
Hollywood 9.0
RapaGUI 2.1
hURL 1.1 (also tested with 1.2)

I just tried MorphOS, and I could not replicate the problem there.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: VirtualFiles gets mixed up

Post by airsoftsoftwair »

Actually, this is the same issues as this so it's fixed already.
Post Reply