View of xos/fs/fs.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
/* Note importante : Les modules fs et fd gerent des verrous sur la
 * representation en memoire des fichiers (hold_file(...), release_file(...)),
 * mais pas sur leur contenu physique. Il appartient aux systemes de fichiers,
 * s'ils le souhaitent, de synchroniser l'acces a leur peripherique pour eviter
 * les lectures et ecritures concurrentes. */
 
#include "file_systems.h"
#include "resolve.h"
#include "node.h"
#include "file_table.h"
 
#include <kmalloc.h>
#include <segment.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>
 
/* Identification a partir d'une chaine dans l'espace utilisateur */
 
/* peripherique */
static int get_device(const char *fs_filename, char **device_path, struct device_struct **device)
{
  int retval;
  struct file_struct *nd;
 
  if (!fs_filename) {
    *device = NULL;
    *device_path = NULL;
    return 0;
  }
 
  /* resolution du nom de fichier */
  if ((retval = resolve(fs_filename, &nd)) < 0)
    goto end;
 
  /* obtention du chemin du peripherique */
  if ((retval = get_path(nd, device_path)) < 0)
    goto release;
 
  /* identification du peripherique */
  if ((retval = node_get_device(nd, device)) < 0)
    goto error_free;
 
  retval = 0;
 release:
  node_release(nd);
 end:
  return retval;
 
 error_free:
  if (*device_path)
    kfree(*device_path, strlen(*device_path) + 1);
  goto release;
}
 
/* Fonctions exportees */
 
/* Point d'entree pour l'initialisation du systeme de fichier. */
void fs_init()
{
  root_init();
  file_table_init();
}
 
void fs_print_info()
{
  file_table_print_info();
}
 
int fs_create(const char *fs_filename)
{
  int retval;
  struct file_struct *d;
  const char *fs_basename;
 
  /* verification que le nouveau nom n'existe pas (cela exclut . et ..) */
  if ((retval = file_exists(fs_filename)) < 0)
    goto end;
  if (retval) {
    retval = -EEXIST;
    goto end;
  }
 
  /* resolution du nom de repertoire */
  if ((retval = resolve_dir(fs_filename, &d, &fs_basename)) < 0)
    goto end;
  if (!get_fs_byte(fs_basename)) { /* on veut creer la racine ! */
    retval = -EEXIST;
    goto release;
  }
 
  /* creation du fichier */
  if ((retval = node_create(d, fs_basename)) < 0)
    goto release;
 
  retval = 0;
 release:
  node_release(d);
 end:
  return retval;
}
 
int fs_remove(const char *fs_filename)
{
  int retval;
  struct file_struct *nd;
 
  /* resolution du nom de fichier */
  if ((retval = resolve(fs_filename, &nd)) < 0)
    goto end;
 
  /* suppression du fichier */
  if ((retval = node_remove(nd)) < 0)
    goto release;
 
  retval = 0;
 release:
  node_release(nd);
 end:
  return retval;
}
 
int fs_mkdir(const char *fs_filename)
{
  int retval;
  struct file_struct *d;
  const char *fs_basename;
 
  /* verification que le nouveau nom n'existe pas (cela exclut . et ..) */
  if ((retval = file_exists(fs_filename)) < 0)
    goto end;
  if (retval) {
    retval = -EEXIST;
    goto end;
  }
 
  /* resolution du nom de repertoire */
  if ((retval = resolve_dir(fs_filename, &d, &fs_basename)) < 0)
    goto end;
  if (!get_fs_byte(fs_basename)) { /* on veut creer la racine ! */
    retval = -EEXIST;
    goto release;
  }
 
  /* creation du repertoire */
  if ((retval = node_mkdir(d, fs_basename)) < 0)
    goto release;
 
  retval = 0;
 release:
  node_release(d);
 end:
  return retval;
}
 
int fs_rmdir(const char *fs_filename)
{
  int retval;
  struct file_struct *d;
 
  /* resolution du nom de fichier */
  if ((retval = resolve(fs_filename, &d)) < 0)
    goto end;
 
  /* suppression */
  if ((retval = node_rmdir(d)) < 0)
    goto release;
 
  retval = 0;
 release:
  node_release(d);
 end:
  return retval;
}
 
int fs_rename(const char *fs_oldname, const char *fs_newname)
{
  int retval;
  struct file_struct *nd, *nd1, *d;
  const char *fs_basename;
 
  /* resolution du nom de fichier */
  if ((retval = resolve(fs_oldname, &nd)) < 0)
    goto end;
 
  /* verification que le nouveau nom n'existe pas (cela exclut . et ..) */
  if ((retval = file_exists(fs_newname)) < 0)
    goto release_nd;
  if (retval) {
    if ((retval = resolve(fs_newname, &nd1)) < 0)
      goto release_nd;
    node_release(nd1);
    if (nd1 == nd) { /* si le nouveau nom designe le fichier a renommer, on ne fait rien et on renvoie un code de succes */
      retval = 0;
      goto release_nd;
    }
    else {
      retval = -EEXIST;
      goto release_nd;
    }
  }
 
  /* resolution du nom de repertoire du nouveau nom */
  if ((retval = resolve_dir(fs_newname, &d, &fs_basename)) < 0)
    goto release_nd;
  if (!get_fs_byte(fs_basename)) { /* on veut nommer la racine ! */
    retval = -EEXIST;
    goto release_d;
  }
 
  /* renommage */
  if ((retval = node_rename(nd, d, fs_basename)) < 0)
    goto release_d;
 
  retval = 0;
 release_d:
  node_release(d);
 release_nd:
  node_release(nd);
 end:
  return retval;
}
 
int fs_mount(const char *fs_device, const char *fs_dir, const char *fs_type)
{
  int retval;
  struct device_struct *dev;
  char *dev_path;
  struct file_struct *nd;
  const struct file_system_struct *fs;
 
  /* identification du peripherique */
  if ((retval = get_device(fs_device, &dev_path, &dev)) < 0)
    goto error;
 
  /* resolution du point de montage */
  if ((retval = resolve(fs_dir, &nd)) < 0)
    goto error_free;
 
  /* identification du systeme de fichier */
  if ((retval = get_file_system(fs_type, &fs)) < 0)
    goto error_release;
 
  /* montage */
  if ((retval = node_mount(nd, dev_path, dev, fs)) < 0)
    goto error_release;
 
  node_release(nd);
  return 0;
 
 error_release:
  node_release(nd);
 error_free:
  if (dev_path)
    kfree(dev_path, strlen(dev_path) + 1);
 error:
  return retval;
}
 
int fs_umount(const char *fs_dir)
{
  int retval;
  struct file_struct *nd;
 
  /* resolution du point de montage */
  if ((retval = resolve(fs_dir, &nd)) < 0)
    goto end;
 
  /* demontage */
  if ((retval = node_umount(nd)) < 0)
    goto release;
 
  retval = 0;
 release:
  node_release(nd);
 end:
  return retval;
}