⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat_dir.c

📁 本资料包括建立在SD卡和NANDFLASH上的文件系统
💻 C
📖 第 1 页 / 共 2 页
字号:

  if (!pDir) {
    return 0;  /* No valid pointer to a FS_DIR structure */
  }
  /* Find path on the media and return file name part of the complete path */
  dsize = FS__fat_findpath(pDir->dev_index, pDirName, &filename, &unit, &dstart); 
  if (dsize == 0) {
    return 0;  /* Directory not found */
  }
  FS__lb_ioctl(FS__pDevInfo[pDir->dev_index].devdriver, unit, FS_CMD_INC_BUSYCNT, 0, (void*)0); /* Turn on busy signal */
  len = FS__CLIB_strlen(filename);
  if (len != 0) {
    /* There is a name in the complete path (it does not end with a '\') */
    FS__fat_make_realname(realname, filename);  /* Convert name to FAT real name */
    i =  FS__fat_find_dir(pDir->dev_index, unit, realname, dstart, dsize);  /* Search name in the directory */
    if (i == 0) {
      /* Directory not found */
      FS__lb_ioctl(FS__pDevInfo[pDir->dev_index].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn off busy signal */
      return 0;
    }
  }
  else {
    /* 
       There is no name in the complete path (it does end with a '\'). In that
       case, FS__fat_findpath returns already start of the directory.
    */
    i = dstart;  /* Use 'current' path */
  }
  if (i) {
    dsize  =  FS__fat_dir_size(pDir->dev_index, unit, i);  /* Get size of the directory */
  }
  if (dsize == 0) {
    /* Directory not found */
    FS__lb_ioctl(FS__pDevInfo[pDir->dev_index].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn off busy signal */
    return 0;
  }
  pDir->dirid_lo  = unit;
  pDir->dirid_hi  = i;
  pDir->dirid_ex  = dstart;
  pDir->error     = 0;
  pDir->size      = dsize;
  pDir->dirpos    = 0;
  pDir->inuse     = 1;
  return pDir;
}


/*********************************************************************
*
*             FS__fat_closedir
*
  Description:
  FS internal function. Close a directory referred by pDir.

  Parameters:
  pDir        - Pointer to a FS_DIR data structure. 
  
  Return value:
  ==0         - Directory has been closed.
  ==-1        - Unable to close directory.
*/

int FS__fat_closedir(FS_DIR *pDir) {
  if (!pDir) {
    return -1;  /* No valid pointer to a FS_DIR structure */
  }
  FS__lb_ioctl(FS__pDevInfo[pDir->dev_index].devdriver, pDir->dirid_lo, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn off busy signal */
  pDir->inuse = 0;
  return 0;
}


/*********************************************************************
*
*             FS__fat_readdir
*
  Description:
  FS internal function. Read next directory entry in directory 
  specified by pDir.

  Parameters:
  pDir        - Pointer to a FS_DIR data structure. 
  
  Return value:
  ==0         - No more directory entries or error.
  !=0         - Pointer to a directory entry.
*/

struct FS_DIRENT *FS__fat_readdir(FS_DIR *pDir) {
  FS__fat_dentry_type *s;
  FS_u32 dirindex;
  FS_u32 dsec;
  FS_u16 bytespersec;
  char *buffer;
  int err;

  if (!pDir) {
    return 0;  /* No valid pointer to a FS_DIR structure */
  }
  buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
  if (!buffer) {
    return 0;
  }
  bytespersec = FS__FAT_aBPBUnit[pDir->dev_index][pDir->dirid_lo].BytesPerSec;
  dirindex = pDir->dirpos / bytespersec;
  while (dirindex < (FS_u32)pDir->size) {
    dsec = FS__fat_dir_realsec(pDir->dev_index, pDir->dirid_lo, pDir->dirid_hi, dirindex);
    if (dsec == 0) {
      /* Cannot convert logical sector */
      FS__fat_free(buffer);
      return 0;
    }
    /* Read directory sector */
    err = FS__lb_read(FS__pDevInfo[pDir->dev_index].devdriver, pDir->dirid_lo, dsec, (void*)buffer);
    if (err < 0) {
      FS__fat_free(buffer);
      return 0;
    }
    /* Scan for valid directory entry */
    s = (FS__fat_dentry_type*)&buffer[pDir->dirpos % bytespersec];
    while (1) {
      if (s >= (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
        break;  /* End of sector reached */
      }
      if (s->data[11] != 0x00) { /* not an empty entry */
        if (s->data[0] != (unsigned char)0xe5) { /* not a deleted file */
          if (s->data[11] != (FS_FAT_ATTR_READ_ONLY | FS_FAT_ATTR_HIDDEN | FS_FAT_ATTR_SYSTEM | FS_FAT_VOLUME_ID)) {
            break;  /* Also not a long entry, so it is a valid entry */
          }
        }
      }
      s++;
      pDir->dirpos += 32;
    }
    if (s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
      /* Valid entry found, copy it.*/
      pDir->dirpos += 32;
      FS__CLIB_memcpy(pDir->dirent.d_name, s->data, 8);
      pDir->dirent.d_name[8] = '.';
      FS__CLIB_memcpy(&pDir->dirent.d_name[9], &s->data[8], 3);
      pDir->dirent.d_name[12] = 0;
      pDir->dirent.FAT_DirAttr = s->data[11];
      FS__fat_free(buffer);
      return &pDir->dirent;
    }
    dirindex++;
  }
  FS__fat_free(buffer);
  return 0;
}


/*********************************************************************
*
*             FS__fat_MkRmDir
*
  Description:
  FS internal function. Create or remove a directory. If you call this 
  function to remove a directory (MkDir==0), you must make sure, that 
  it is already empty.

  Parameters:
  pDirName    - Directory name. 
  Idx         - Index of device in the device information table 
                referred by FS__pDevInfo.
  MkDir       - ==0 => Remove directory.
                !=0 => Create directory.
  
  Return value:
  ==0         - Directory has been created.
  ==-1        - An error has occured.
*/

int  FS__fat_MkRmDir(const char *pDirName, int Idx, char MkDir) {
  FS_size_t len;
  FS_u32 dstart;
  FS_u32 dsize;
  FS_u32 unit;
  FS_i32 i;
  int lexp_a;
  int lexp_b;
  char realname[12];
  char *filename;

  if (Idx < 0) {
    return -1; /* Not a valid index */
  }
  dsize = FS__fat_findpath(Idx, pDirName, &filename, &unit, &dstart);
  if (dsize == 0) {
    return -1;  /* Path not found */
  }
  FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_INC_BUSYCNT, 0, (void*)0); /* Turn on busy signal */
  len = FS__CLIB_strlen(filename);
  if (len != 0) {
    FS__fat_make_realname(realname, filename);  /* Convert name to FAT real name */
    i =  FS__fat_find_dir(Idx, unit, realname, dstart, dsize);
    lexp_a = (i!=0) && (MkDir);  /* We want to create a direcory , but it does already exist */
    lexp_b = (i==0) && (!MkDir); /* We want to remove a direcory , but it does not exist */
    lexp_a = lexp_a || lexp_b;
    if (lexp_a) {
      /* We want to create, but dir does already exist or we want to remove, but dir is not there */
      /* turn off busy signal */
      FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);
      return -1;
    }
  }
  else {
    FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn off busy signal */
    return -1;
  }
  /* 
      When you get here, variables have following values:
       dstart="current"  
       dsize="size of current"  
       realname="real dir name to create" 
  */
  if (MkDir) {
    i = _FS_fat_create_directory(Idx, unit,realname, dstart, dsize);  /* Create the directory */
  }
  else {
    i = FS__fat_DeleteFileOrDir(Idx, unit, realname, dstart, dsize, 0);  /* Remove the directory */
  }
  if (i >= 0) {
    /* If the operation has been successfull, flush the cache.*/
    i = FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_FLUSH_CACHE, 2, (void*)0);
    FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn of busy signal */
    if (i < 0) {
      return -1;
    }
    return 0;
  }
  FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn of busy signal */
  return -1;
}


#endif /* FS_POSIX_DIR_SUPPORT */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -