fs.py

Created by tlake

Created on August 07, 2024

3.6 KB


#virtual filesystem module
#version 2.0

def path(path):
  i=len(path)-1
  while i!=0 and path[i] !="/":
    i-=1
  return ("/"+path[0:i],path[i+1:len(path)]) if i!=0 else (path[0:i],path[i:len(path)])






fstree=["folder",{},"rw-rw-rw-"]
dosSyst=("bin","dev","etc","home","lib","media","mnt","opt","proc","root","run","sbin","srv","sys","tmp","usr","var")
for i in dosSyst:
  fstree[1][i]=["folder",{},"rw-rw-rw-"]
fstree[1]["usr"][1]["users"]=["data","admin bobb\npassword Bobb_56"]
fswd=["root"]
fsdir=fstree[1]["root"]
del dosSyst


def cd(name="/root"):
  #lister les dossiers successifs
  global fswd
  global fsdir
  if name[0]=="/":#chemin absolu
    fswd=[]
    name=name[1:len(name)]
  
  l=[]
  deb=0
  for i in range(len(name)):
    if name[i]=="/":
      l.append(name[deb:i])
      deb=i+1
    if i==len(name)-1:
      l.append(name[deb:len(name)])
  for name in l:
    if name=="..":
      del fswd[-1]
    else:
      fswd.append(name)
  #aller dans le dossier
  fsdir=fstree
  for i in fswd:
    if fsdir[1][i][0]=="folder":
      fsdir=fsdir[1][i]
    else:
      print(i,"is not a folder.")
      del fswd[-1]
  
  return "/"+"/".join(fswd)

def ls(l=""):
  if l=="":
    for i in fsdir[1]:
      print(i,end="   ")
  elif l=="-l":
    for i in fsdir[1]:
      print(i, fsdir[1][i][2])
  print("\n",end="")


def pwd():
  wd="/"+"/".join(fswd)
  print(wd)
  return wd

def mkdir(*name):
  wdDeb="/"+"/".join(fswd)
  for i in name:
    i=path(i)
    cd(wdDeb+i[0])
    if not(i[1] in fsdir[1]):
      fsdir[1][i[1]]=["folder",{},"rw-rw-rw-"]
    else:
      print(i[1],"already exists")
  cd(wdDeb)

def su():
  l=fstree[1]["usr"][1]["users"][1].split()
  if input("Username: ")==l[l.index("admin")+1] and input("Password: ")==l[l.index("admin")+3]:
    print("Welcome")
  else:
    print("Authentification failed")



def touch(*name):
  wdDeb="/"+"/".join(fswd)
  for i in name:
    i=path(i)
    cd(wdDeb+i[0])
    if not(i[1] in fsdir[1]):
      fsdir[1][i[1]]=["file","","rw-rw-rw-"]
    else:
      print(i[1],"already exists")
  cd(wdDeb)

def rm(*name,t="file"):
  wdDeb="/"+"/".join(fswd)
  for i in name:
    i=path(i)
    cd(wdDeb+i[0])
    if fsdir[1][i[1]][0]==t:
      del fsdir[1][i[1]]
    else:
      print(i[1],"is not a",t)
  cd(wdDeb)

def mv(*args):
  to="/"+args[-1]
  name=list(args);name.pop()
  del args
  wdDeb="/"+"/".join(fswd)
  for i in name:
    i=path(i)
    cd(wdDeb+i[0])
    if to in fsdir[1]:
      mvData=fsdir[1][i[1]]
      del fsdir[1][i[1]]
      cd(wdDeb+to)
      fsdir[1][i[1]]=mvData
    else:
      mvData=fsdir[1][i[1]]
      del fsdir[1][i[1]]
      fsdir[1][to[1:len(to)]]=mvData
      
  cd(wdDeb)

def rmdir(*name):
  rm(*name,t="folder")

def cat(w="",file=""):
  if file=="":
    file=w;w=""
  
  wdDeb="/"+"/".join(fswd)
  file=path(file)
  cd(wdDeb+file[0])
  if fsdir[1][file[1]][0]=="file":
    if w=="":#aff
      print(fsdir[1][file[1]][1])
    elif w==">":#write
      fsdir[1][file[1]][1]=""
      vartest=True
      while vartest:
        try:
          line=input()
          fsdir[1][file[1]][1]+=line+"\n"
        except:
          vartest=False
  else:
    print(file[1],"is not a file")
  cd(wdDeb)


#creation des repertoires
mkdir("Desktop","Documents","Musique","Videos","Images","Documents/Ma_mission")
touch("Documents/Ma_mission/Mission.txt")
fsdir[1]["Documents"][1]["Ma_mission"][1]["Mission.txt"][1]="Vous avez une mission:\nVous devez parvenir a afficher dans le terminal, de la ou ils sont enregistres, le nom utilisateur de l administrateur ainsi que son mot de passe.\nPour ce faire, vous n avez droit qu a 6 instructions maximum et vous pouvez bien sur aller voir dans le code."

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.