Temporary Insanity

BlitzMax Forums/BlitzMax Beginners Area/Temporary Insanity

dw817(Posted 2016) [#1]
I think everyone wants to find a nice place to put their data files without prying eyes getting all over them.

Well, I know that %temp% is a short-cut to the temporary directory. And while this might be the case, this CODE is certainly going somewhere else !

You can prove this by bringing up EXPLORER and going into the address field and typing out %temp% and [ENTER] to see the temporary directory.

What you WON'T see is the file that is created in this program - so my question is, where is it going ? It obviously exists or it wouldn't be able to load later.




Henri(Posted 2016) [#2]
Hi,

you can add 'Debugstop' command in your code to halt program execution and from MaxIDE use the Step button to execute one statement at the time. Your variables are seen in the right panel under debug tab.

In Windows enviroment TempDir can be found like this:
Local tempDir:String = getenv_("TEMP") + "\"


-Henri


xlsior(Posted 2016) [#3]
You can't actually use environment variables in blitz path names like DOS/windows supports, you have to actually translate it yourself. Trying to change folders to %temp% doesn't do anything, and you remain in the current folder. You can still use the temp folder environment variable to figure out where to go, but you have to retrieve its contents yourself first, it won't automatically replace it with the variable.

Note that you can actually retrieve the results (true, false) of your changedir command, so you can see if it actually did anything. CurrentDir() will print the actual folder you're in.

Here's a working example:

If ChangeDir("%temp%")=True Then Print "It worked!" Else Print "False!"
Print CurrentDir()

If ChangeDir(GetEnv_("temp"))=True Then Print "It worked!" Else Print "False!"
Print CurrentDir()



(And just an FYI: in your case where you didn't see the output file: You are still in the current folder where the .exe itself resides, since your changedir didn't actually do anything. If you don't see anything written in the current folder: If you are using Windows 8 or 10 with UAC enabled and your current folder is inside c:\program files, then windows may automatically virtualize file operations to a location somewhere inside the c:\users\<username> path)


dw817(Posted 2016) [#4]
Why the sneaky little cheat ! Didn't even change home directory, is that right, Xlsior ? The dirty whatsit.

Yep, there's the test file, toasting its pommel by the fire. Awright, Henri, I like what you wrote. That definitely does find the main temporary directory. Good enough for me !

Thanks guys !


grable(Posted 2016) [#5]
If you want to hide stuff from prying eyes and use windows, you could also use alternate data streams.

untitled1.bmx
SuperStrict

Local stream:TStream = WriteStream("untitled1.bmx:hidden")
stream.WriteLine("hidden stuff")
stream.Close()

Print LoadString("untitled1.bmx:hidden")

Though this is more security through obscurity, as the data can still be found if one know to look for it.
Also the data will be lost if copied to non NTFS drivers, or transmitted over a network.


dw817(Posted 2016) [#6]
Oh my. What ARE you doing here, Grable ?? Looking ... Is "hidden" a reserved word or can you use any word you want in this situation ?

This is astounding. I can see so many uses for this as I often work with throwaway files on the HD constantly in my bigger programs.

You mentioned the data could still be found. I'm most impressed by what I'm seeing - could you please give me the LONG story of how someone would find this file - "untitled1.bmx" ?

And are you possibly hinting that it is possible to run source code as a text file inside a BltzMAX compiled executable ? I know some other languages that can do this.


grable(Posted 2016) [#7]
On windows, most file system apis support Alternate Data Streams by appending :anything you want, mostly to the filename.
It is an NTFS feature.

I could have chosen any other filename before the :, it just had to be a file i knew would be there ;)
Though it might create an empty file if it doesnt exist, havent tried that.
So no code execution from source at runtime im afraid.

The file "untitled.bmx" is just a normal file, but it has extra data attached to it.
Which is not visible to explorer or other programs unless they also check for ADS data.

Have you ever wondered where browsers stores the data about executables it downloads from the internet, and thus nagging you about it before running them?
Well now you know :p

There are some new apis to specifically enumerate ADS data attached to files in Vista and up, before that one had to contort some backup apis to enumerate them.

Heres an old code archive entry NTFS Alternate Data Streams.

And a much simpler version using Vista+ apis:



dw817(Posted 2016) [#8]
Okay, let me ask the important questions now, Grable. Is this 'ghost' directory lost when execution of the program ends - or is it possible to have the contents maintain even after rebooting your computer ?

What code could you use to look at the contents, all files and directories saved in this passage of, ":hidden" ?


grable(Posted 2016) [#9]
As i said, they are attached to the files themselves. the filesystem (NTFS) takes care of it.
Think of it as file level meta-data, it will follow the file wherever it goes.
That is unless the file is copied to a non-NTFS drive like FAT32 etc, or transmitted over a network.

What code could you use to look at the contents, all files and directories saved in this passage of, ":hidden"
Your asking how to enumerate ALL files that have Alternate Data Streams?
If so, using ReadDir/NextFile/CloseDir to recurse from the root and then FindFirstStream/FindNextStream on each file.

The examples i posted are pretty self explanatory (for alternate data streams anyway).


dw817(Posted 2016) [#10]
This is really very helpful, Grable. Definitely new stuff to me.

Okay, the other question ? Is it possible to have text of source code and run it inside BlitzMAX ? For instance.
z=3
x=50+z
y=75
a$="cls ; drawrect 0,0,"+x+","+y
Exec a$
Now I know there is Framework and Import, no, this is something different, a real string you can make changes to and still be able to execute it as if it were code - and not just at the top of your code but you can do it anywhere inside with a command like Exec() or something like it.

I can think of 2 programming languages that do this. One of which is quite current.


dw817(Posted 2016) [#11]
Okay, I'm experimenting now, Grable. It DOES save a file to your hard-drive with easy access. HOWEVER, that file appears to have zero bytes in it.

I tried 7zip on it, it too thought it was zero bytes.

I copied the EXE to read it in a different directory. I made a dummy file with the same name. It did not like that, it crashes.

So then I copied over the correct newly made zero-byte file from the program that saved it to hidden. Then it worked - it could read it.

This is certainly some rough magic going on. It hides the contents and the filesize but maintains the visibility of the filename itself.

I am also understanding that if I copied that zero-byte file to, say a thumb drive, and brought it upstairs, that it would not work to try and read it there. That's an experiment I'll try the morrow.

Also, you cannot store files in "hidden" directories, a la
CreateDir "stuff:lala"
Trying to do so creates no visible directory and trying to save anything there just saves it to the current directory normally - not hidden at all.

Hmm ... I was hoping to save mayhaps a hundred or more files in a hidden directory during operation of a program. Perhaps not.


grable(Posted 2016) [#12]
Is it possible to have text of source code and run it inside BlitzMAX ? For instance.
If you create a parser and interpreter for it yes.
Which is to say duplicating the workings of BCC (the blitzmax compiler) albeit much simpler.

Take a look at yet another old code archive entry of mine Expression Evaluator which does some of it.
Its the simplest form of interpretation. ie no intermediate storage, just evaluating each piece of a string as it reads it.

HOWEVER, that file appears to have zero bytes in it.
That is because it is only attached to the file via the file system. The file itself it none the wiser.

I copied the EXE to read it in a different directory. I made a dummy file with the same name. It did not like that, it crashes.
As it should when trying to read from a non existant file.


I am also understanding that if I copied that zero-byte file to, say a thumb drive, and brought it upstairs, that it would not work to try and read it there, yes ?
Yes. Unless it was formatted with NTFS, in which case it would work.

Trying to do so creates no visible directory and trying to save anything there just saves it to the current directory normally - not hidden at all.
As i said, the streams are pure data only. Like any other file.

Hmm ... I was hoping to save mayhaps a hundred or more files in a hidden directory during operation of a program. Perhaps not.
I am not aware of any limit on the amount of streams, so that would certainly be possible.


dw817(Posted 2016) [#13]
Good Morning. Just getting ready to copy the zero-byte file, "hello.txt" to my thumb drive to test upstairs, Grable.

Ah ! An interesting message appeared as I tried to copy to it:

1 Interrupted Action

Are you sure you want to copy this file without its properties?

The file hello.txt has properties that can't be copied to the new location.


Tricky little bugger. :)

As for your expression evaluator, that's quite a bit ! I know if I work on my game making language thingie that I'll need something to handle + - * / and parentheses. But that's as far as I go.

Is there an expression evaluator at least in BlitzMAX ? I know there was one in TRS 80. (adjust integer input)
INPUT A
PRINT A
? 1+2*3
7



grable(Posted 2016) [#14]
Is there an expression evaluator at least in BlitzMAX?
No, otherwise i would not have to make my own ;)


dw817(Posted 2016) [#15]
Hmm ... disappointing. Well I'm still working on that 750k thingie. Once I finish that I think a fancy calculator with changeable variables and parenthesis will be in order. :)


Henri(Posted 2016) [#16]
Expressions can be evaluated to true or false like:
Local six:Int = 6, seven:Int = 7 

Print seven = seven	'True
Print seven = six	'False


-Henri


dw817(Posted 2016) [#17]
Hi Henri. No, what we are referring to is the ability to calculate the numeric value of a math problem inside a single string.

a$="2+3*4"

print superval(a$)

14