utils/stat.c
/*
 * open - search for an entry specified by pathname and open it
 *
 * @pathname - the path to a file/device
 * @flags    - open flags
 *
 *  Open a node and save the information in the current TCB.
 *
 */
search for an entry specified by pathname and open it
@pathname - the path to a file/device
@flags    - open flags

Open a node and save the information in the current TCB.
search for an entry specified by pathname and open it @pathname - the path to a file / device @flags - open flags Open a node and save the information in the current TCB .
int open(const char *pathname, int flags, ...)
{
  struct vfs_node_s *node = NULL;
  size_t name_len = strlen(pathname);
  if (name_len == 0)
    return -EINVAL;
  int ret = OK;
  char *path_without_name;
  int i = name_len - 1;
  if (pathname[name_len - 1] == '/') {
    while (i > 0 && pathname[i] == '/') { i--; }
  }
  while (i > 0 && pathname[i] != '/') { i--; }
  path_without_name = calloc(1, i + 2);
  if (path_without_name == NULL)
    return -ENOMEM;
  strncpy(path_without_name, pathname, i + 1);
  if (flags & O_CREATE) {
    node = create_vfs_node(path_without_name, pathname, name_len);
  } else {
    node = vfs_get_matching_node(pathname, name_len);
    if (node == NULL && (flags & O_APPEND)) {
      node = create_vfs_node(path_without_name, pathname, name_len);
    }
  }
  if (node == NULL) {
    ret = -ENOENT;
    goto free_with_path;
  }
  struct opened_resource_s *res =
    sched_allocate_resource(node, 0);
  if (res == NULL) {
    ret = -ENFILE;
    goto free_with_path;
  }
  sem_wait(&node->lock);
  if (node->ops != NULL && node->ops->open != NULL) {
    ret = node->ops->open(res, pathname, flags, 0);
  }
  if (ret < 0) {
    sched_free_resource(res->fd);
    sem_post(&node->lock);
    goto free_with_path;
  }
  ret = res->fd;
  node->open_count += 1;
  sem_post(&node->lock);
free_with_path:
  free(path_without_name);
  return ret;
}
int open ( const char * pathname , int flags , ... ) { struct vfs_node_s * node = NULL ; size_t name_len = strlen ( pathname ) ; if ( name_len == 0 ) return - EINVAL ; int ret = OK ; char * path_without_name ; int i = name_len - 1 ; if ( pathname [ name_len - 1 ] == ' ' ) { while ( i > 0 && pathname [ i ] == ' ' ) { i -- ; } } while ( i > 0 && pathname [ i ] != ' ' ) { i -- ; } path_without_name = calloc ( 1 , i + 2 ) ; if ( path_without_name == NULL ) return - ENOMEM ; strncpy ( path_without_name , pathname , i + 1 ) ; if ( flags & O_CREATE ) { node = create_vfs_node ( path_without_name , pathname , name_len ) ; } else { node = vfs_get_matching_node ( pathname , name_len ) ; if ( node == NULL && ( flags & O_APPEND ) ) { node = create_vfs_node ( path_without_name , pathname , name_len ) ; } } if ( node == NULL ) { ret = - ENOENT ; goto free_with_path ; } struct opened_resource_s * res = sched_allocate_resource ( node , 0 ) ; if ( res == NULL ) { ret = - ENFILE ; goto free_with_path ; } sem_wait ( & node -> lock ) ; if ( node -> ops != NULL && node -> ops -> open != NULL ) { ret = node -> ops -> open ( res , pathname , flags , 0 ) ; } if ( ret < 0 ) { sched_free_resource ( res -> fd ) ; sem_post ( & node -> lock ) ; goto free_with_path ; } ret = res -> fd ; node -> open_count += 1 ; sem_post ( & node -> lock ) ; free_with_path : free ( path_without_name ) ; return ret ; }
open - search for an entry specified by pathname and open it
 @pathname - the path to a file/device
 @flags    - open flags
open - search for an entry specified by pathname and open it @pathname - the path to a file / device @flags - open flags
/* If the name of the open path ends in '/' ex : /mnt/my_file/
   * move the index i to:                      --------|
   * to extract the parent.
   */ /* Allocate memory for the path without the filename */ /* Look through VFS and find the node identified by pathname */ /* If the user process requested O_APPEND bu there is no file
     * we should create a new file.
     */ /* Grab an entry from the tcb FILE structure. */ /* Call the vfs open method */