Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active January 28, 2017 02:23
Show Gist options
  • Save thomasjslone/0168c9a7b7b6b2857c300af0c5b1b1ad to your computer and use it in GitHub Desktop.
Save thomasjslone/0168c9a7b7b6b2857c300af0c5b1b1ad to your computer and use it in GitHub Desktop.
vdr with storrage/access modes seperate, multidisk virtual file system w/ encryption and importing/exporting to real file system
class VDrive
## V1.2.2
## Methods:
## - file_exist?(STRING) path returns boolean
## - folder_exist?(STRING) path returns boolean
## - ftype?(STRING) path returns "FILE" , "FOLDER" , "CORRUPTED OBJ"
## - read_file(STRING) path returns string contained in file
## - write_file(STRING,STRING) path, content returns string written, if file does not exist it will be created, otherwise it will be overwritten
## - copy_file(STRING,STRING) path, newpath returns true
## - create_folder(STRING) path returns true
## - copy_folder(STRING,STRING) path, newpatj returns true
## - delete_file(STRING) path returns true
## - delete_folder(STRING) path returns true
## - rename_file(STRING,STRING) path, newname returns true
## - rename_folder(STRING,STRING) path, newname returns true
## - import_file(STRING,STRING) vdrive_path, os_filepath return location of imported file
## - export_file(STRING,STRING) vdrive_path, os_filepath returns os path file exported to
## - import_folder(STRING,STRING) vdrive_path, os_folderpath returns location of folder imported
## - export_folder(STRING,STRING) vdrive_path, os_filepath
## - export_disk(path,password) os_filepath returns true
## - import_disk(path,password) os_filepath returns true
## - save_disk writes the file system disk down into storage in this source code
## - load_disk loads the file system disk from this source code
## - format_disk formats disk to brandnew then saves it
## - disk_size counts the amount of integers it takes to represent your disk
## - disk_label returns the disk label name
## - rename_disk(newname) change the disk label
## - mode? returns the disk access mode
## - set_mode(mode) sets the disk access mode
## - auto_save save any itme a change is made
## - dont_auto_save turn off auto save
## - generate_image(files,folders) for use by methods when in direct mode returns STRING
## - process_image(img) for use by methods in direct mode returns [@files,@folders]
def initialize
@default_disks = ["v"]
@mounted_volumes = []
@mounted_disks = []
@storage_mode = "internal" ## internal stores the code below in @disks/@volumes, which get loaded raw by default when the porgram starts up
@fsys_external_path = nil
@access_mode = "normal" # normal / encoded / encrypted ( loading/saving takes longer )
@auto_save = true # this tells vdrive to save after any change to a file or folder, not nessecary in direct mode obviously
###DISK DATA BELOW###
@disks = ['6767672284']
@volumes = ['v']
###DISK DATA ABOVE###
@default_disks.each do |d|
mount(d)
end
return @mounted_disks[0].to_s
end
def file_exist?(loc)
if @mounted_volumes.include?(loc[0].to_s)
found = false ; i = 0
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0].each do |f|
if f.to_s.split("@")[0].to_s + "/" + f.to_s.split("@")[1].to_s == loc.to_s
found = i ; break
end
i += 1
end
found
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def folder_exist?(loc)
if @mounted_volumes.include?(loc[0].to_s)
if @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].include?(loc)
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].index(loc)
else
false
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def ftype?(loc)
if @mounted_volumes.include?(loc[0].to_s)
if folder_exist?(loc).ia_a? Integer
"FOLDER"
elsif file_exist?(loc).is_a? Integer
"FILE"
else
"FALSE"
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def read_file(loc)
if @mounted_volumes.include?(loc[0].to_s)
if file_exist?(loc).is_a? Integer
denumerize(@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][i].to_s.split("@")[2].to_s).to_s
else
"FILE NO EXIST ERROR"
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def write_file(loc,dat) ## enter valid folder path and append a file name, if it doesnt exist it will be created
if @mounted_volumes.include?(loc[0].to_s)
if folder_exist?(loc.to_s.split("/")[0..-2].join("/")).is_a? Integer
if file_exist?(loc).is_a? Integer ## overwrite existing file
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][i] = @mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][i].to_s.split("@")[0..1].join("@").to_s + "@" + numerize(dat.to_s).to_s
else ## write to new file
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0] << loc.to_s.split("/")[0..-2].join("/").to_s + "@" + loc.to_s.split("/")[-1].to_s + "@" + numerize(dat.to_s).to_s
end
else
"ERROR ; DIR NO-EXIST: " + loc.to_s.split("/")[0..-2].join("/").to_s
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def delete_file(loc)
if @mounted_volumes.include(loc[0].to_s)
if file_exist?(loc).is_a? Integer
@mounted_disks[@mounted_volumes.index(vol)][0].delete_at(i) ; if @auto_save ; save_disk ; end ; true
else
"FILE NON-EXISTENT"
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def rename_file(loc,newname)
if @mounted_volumes.include?(loc[0].to_s)
if file_exist?(loc).is_a? Integer
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][found] = @mounted_disks[@mounted_volumes.index(loc[0].to_s)][0][found].to_s.split("@")[0].to_s + "@" + newname.to_s + "@" + files[found].to_s.split("@")[2].to_s ; if @auto_save ; save_disk ; end ; true
else
"FILE NON-EXISTENT"
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def copy_file(loc,nloc)
end
def move_file(loc,nloc)
end
def create_folder(loc)
if @mounted_volumes.include?(loc[0].to_s)
if folder_exist?(loc.to_s.split("/")[0..-2].join("/")).is_a? Integer
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1] << loc.to_s ; if @auto_save ; save_disk ; end ; true
else
"PARENT FOLDER DOES NOT EXIST: " + loc.to_s.split("/")[0..-2].join("/").to_s
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def delete_folder(loc)
if @mounted_volumes.include?(loc[0].to_s)
cfiles = [] ; cfolders = [] ; remaining_folders = [loc]
until remaining_folders.length == 0
cont = read_folder(remaining_folders[0]) ; cfolders << remaining_folders[0] ; remaining_folders.delete_at(0)
unless cont.to_s == "empty"
unless cont[0].to_s == "empty"
cont[0].each { |c| cfiles << c.to_s }
end
unless cont[1].to_s == "empty"
cont[1].each { |c| remaining_folders << c.to_s }
end
end
end
unless cfiles.length < 1
cfiles.each { |f| @mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0].delete(f.to_s) }
end
unless cfolders.length < 1
cfolders.each { |f| @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].delete(f.to_s) }
end
if @auto_save ; save_disk ; end
true
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def rename_folder()
end
def copy_folder()
end
def move_folder()
end
def read_folder(loc) ## returns list of paths of files and folders in given folder
vol = loc[0].to_s
i = instance_folder_exist?(loc)
if i.is_a? Integer
@mounted_disks[@mounted_volumes.index(vol)][0].each do |f|
if f.to_s.split("@")[0].to_s == loc.to_s
contents << f.to_s.split("@")[0].to_s + "/" + f.to_s.split("@")[1].to_s
end
end
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].each do |f|
if f.to_s.split("/")[0..-2].join("/").to_s == loc.to_s
contents << f.to_s
end
end
if contents.length == 0
return "empty"
else
return contents
end
else
"FOLDER NON-EXISTENT"
end
end
def map_folder(loc) ## returns a list of all paths contained in the given folder and all its subfolders
end
def import_file(loc,os_file_path)
if ftype?(loc.to_s).to_s == "FOLDER"
if File.exist?(os_file_path.to_s)
begin
f = File.open(os_file_path.to_s,"r") ; cont = f.read.to_s ; f.close
write_file(loc.to_s + "/" + os_file_path.to_s.split("/")[-1].to_s,cont.to_s)
if @auto_save ; save_disk ; end
file_exist?(loc.to_s + "/" + os_file_path.to_s.split("/")[-1].to_s).is_a? Integer
rescue
"Could not access file: " + os_file_path.to_s
end
else
"Invalid File Path: " + os_file_path.to_s
end
else
"INVALID IMPORT LOCATION: " + loc.to_s
end
end
def export_file(loc,os_directory)
if File.directory?(os_directory)
if ftype(loc.to_s).to_s == "FILE"
cont = read_file(loc.to_s).to_s
begin
f = File.open(os_directory.to_s + "/" + loc.to_ss,"w") ; f.write(cont.to_s) ; f.close
File.exist?(os_directory.to_s + "/" + loc.to_s.split("/")[-1].to_s)
rescue
"Could not write to directory: " + os_directory.to_s
end
else
"Invalid Export File: " + loc.to_s
end
else
"Invalid Export Directory: " + os_directory.to_s
end
end
def import_folder(loc,os_file_path)
end
def export_folder(loc,os_directory)
end
def import_disk(vol,diskfilepath,password)
if password == nil or password == '' or password == 0
pv = 0
else
pv = numerize(password.to_s).to_i
end
file = File.open(diskfilepath,"r")
img = file.read.to_s ; file.close
img = img.to_i / pv.to_i + diskfilepath.length.to_i
@mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0] = denumerize(img.to_s).to_s.split("###")[0].to_s.split(",")
@mounted_disks[@mounted_volumes.index(loc[0].to_s)][1] = denumerize(img.to_s).to_s.split("###")[1].to_s.split(",")
end
def export_disk(vol,diskfilepath,password)
if password == nil or password == "" or password == "0" or password == 0
pv = 0
else
pv = numerize(password.to_s).to_i
end
img = numerize(numerize(@mounted_disks[@mounted_volumes.index(loc.to_s.split(":")[0])][0].join(",") + "###" + @mounted_disks[@mounted_volumes.index(loc[0].to_s)][1].join(","))).to_i * pv - diskfilepath.to_s.length.to_i
file = File.new(diskfilepath,"w")
file.write(img.to_s) ; file.close
diskfilepath.to_s
end
def format_disk(vol)
@mounted_disks[@mounted_volumes.index(vol)][0] = [] ; @mounted_disks[@mounted_volumes.index(vol)][1] = [vol.to_s + ':'] ; save_disk(vol) ; true
end
def disk_size(vol)
@mounted_disks[@mounted_volumes.index(vol)].to_s.length.to_i
end
def mountable?
@volumes
end
def mounted?
return @mounted_volumes
end
def mount(vol)
if @mounted_disks.include?(vol)
"Disk '" + vol.to_s + "' is already mounted."
else
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = (icont.to_s.split("\n")[1].split(" = ")[1].to_s)
if volumes.include?(vol)
@mounted_volumes << vol.to_s ; @mounted_disks << [[],[]] ; load_disk(vol)
true
else
"Invalid Volume Label: " + vol.to_s + ", No such disk attached."
end
end
end
def unmount(vol)
if @mounted_volumes.include?(vol)
i = @mounted_volumes.index(vol)
@mounted_volumes.delete_at(i) ; @mounted_disks.delete_at(i)
true
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def load_disk(vol)
if @mounted_volumes.include?(vol)
if @storage_mode == "internal"
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = eval(icont.to_s.split("\n")[1].split(" = ")[1].to_s)
img = disks[volumes.index(vol)]
files = denumerize(img.to_s).to_s.split("###")[0].to_s.split(",")
folders = denumerize(img.to_s).to_s.split("###")[1].to_s.split(",")
@mounted_disks[@mounted_volumes.index(vol)][0] = files
@mounted_disks[@mounted_volumes.index(vol)][1] = folders
true
elsif @storage_mode == "external"
file = File.open(@fsysimg_external_path,"r") ; icont = file.read.to_s ; file.close
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = eval(icont.to_s.split("\n")[1].split(" = ")[1].to_s)
img = disks[volumes.index(vol)]
files = denumerize(img.to_s).to_s.split("###")[0].to_s.split(",")
folders = denumerize(img.to_s).to_s.split("###")[1].to_s.split(",")
@mounted_disks[@mounted_volumes.index(vol)][0] = files
@mounted_disks[@mounted_volumes.index(vol)][1] = folders
true
end
else
"Disk '" + vol.to_s + ":/' is not mounted or is an invalid volume label."
end
end
def save_disk(vol)
if @mounted_volumes.include?(vol)
if @storage_mode.to_s == "internal"
img = numerize(@mounted_disks[@mounted_volumes.index(vol)][0].join(",") + "###" + @mounted_disks[@mounted_volumes.index(vol)][1].join(","))
file = File.open(__FILE__,"r") ; fcont = file.read.to_s ; file.close
icont = fcont.to_s.split("###DISK " + "DATA BELOW###")[-1].to_s.split("###DISK " + "DATA ABOVE###")[0]
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = (icont.to_s.split("\n")[1].split(" = ")[1].to_s)
disks[volumes.index(vol)] = img.to_s
first = fcont.to_s.split("###DISK " + "DATA BELOW###")[0].to_s + "###DISK " + "DATA BELOW###"
middle = "@disks = " + disks.to_s + "\n" + "@volumes = " + volumes.to_s + "\n"
last = "###DISK " + "DATA ABOVE###" + fcont.to_s.split("###DISK " + "DATA ABOVE###")[-1].to_s
file = File.open(__FILE__,"w") ; file.write(first.to_s + "\n" + middle.to_s + "\n" + last.to_s) ; file.close
true
elsif @storage_mode.to_s == "external"
img = numerize(@mounted_disks[@mounted_volumes.index(vol)][0].join(",") + "###" + @mounted_disks[@mounted_volumes.index(vol)][1].join(","))
file = File.open(@fsys_external_path,"r") ; icont = file.read.to_s ; file.close
volumes = eval("[" + icont.to_s.split("\n")[2].to_s.split("@volumes = [")[1].to_s[0..-3].to_s + "]")
disks = (icont.to_s.split("\n")[1].split(" = ")[1].to_s)
disks[volumes.index(vol)] = img.to_s
img = "@disks = " + disks.to_s + "\n" + "@volumes = " + volumes.to_s + "\n"
file = File.open(__FILE__,"w") ; file.write(img.to_s) ; file.close
true
end
else
"Disk '" + loc[0].to_s + ":/' is not mounted or is an invalid volume label."
end
end
def numerize(str) ## turn any string into positive only numbers that never start with zero
if str == "" ; return 0 ; else ; chbytes = ["97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "126", "96", "33", "64", "35", "36", "37", "94", "38", "42", "40", "41", "45", "95", "61", "43", "91", "93", "123", "125", "59", "58", "34", "39", "92", "47", "124", "63", "60", "62", "44", "46", "32", "9", "10"] ; ch_inds = [] ; str.to_s.split("").each { |ch| ch_inds << chbytes.index(ch.to_s.ord.to_s) } ; enc = [] ; ch_inds.each do |ch| ; code = ch.to_i + 1 ; if code.to_s.length == 1 ; code = "0" + code.to_s ; else ; code = code.to_s ; end ; enc << code.to_s ; end ; enc = enc.join("").to_s ; if enc.to_s[0].to_s == "0" ; enc = enc[1..-1].to_s ; end ; return enc.to_s ; end
end
def denumerize(str) ## get your old string back from the numbers you generated
kbchars = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","~","`","!","@","#","$","%","^","&","*","(",")","-","_","=","+","[","]","{","}",";",":","\"","'","\\","/","|","?","<",">",",","."," ","\t","\n"] ; if str.to_s.length.odd? ; str = "0" + str.to_s ; end ; str_codes = [] ; i = 0 ; hold = "" ; str.to_s.split("").each do |num| ; if i.to_i == 0 ; hold << num.to_s ; i += 1 ; elsif i.to_i == 1 ; hold << num.to_s ; i = 0 ; str_codes << hold.to_s ; hold = "" ; end ; end ; str = str_codes ; str_codes = [] ; str.each do |c| ; str_codes << c.to_i - 1 ; end ; dec_str = "" ; str_codes.each { |c| dec_str << kbchars[c.to_i].to_s } ; dec_str.to_s
end
end
$vdr = VDrive.new()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment