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

📄 main.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/*    cmp_size: Compare directory entries by size                      */
/*                                                                     */
/***********************************************************************/
static int cmp_size(const DirEntry *e1, const DirEntry *e2)
{
  return e1->st_size - e2->st_size;
}

/***********************************************************************/
/*   sort_test: Test directory entry sorting                           */
/*                                                                     */
/*       Input: vol_name = name of file system volume                  */
/*                                                                     */
/***********************************************************************/
static int sort_test(char *vol_name)
{
  int fid, i;
  DIR *dir;
  struct stat pbuf, ebuf;
  struct dirent *entry, *prev;
  char fname[2], nbuf[FILENAME_MAX];

  /*-------------------------------------------------------------------*/
  /* Create test directory and change CWD to it.                       */
  /*-------------------------------------------------------------------*/
  if (mkdir("sort", S_IRUSR | S_IWUSR | S_IXUSR) || chdir("sort"))
  {
    perror("Error making or cd'ing into directory");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Create the test files.                                            */
  /*-------------------------------------------------------------------*/
  fname[1] = 0;
  for (i = 0; i < 20; ++i)
  {
    fname[0] = 'A' + i;
    fid = creat(fname, 0666);
    write(fid, "heap", 200 - i);
    close(fid);
  }

  /*-------------------------------------------------------------------*/
  /* List the directory contents.                                      */
  /*-------------------------------------------------------------------*/
  dir = opendir("../sort");
  while ((entry = readdir(dir)) != NULL)
    printf("  %s", entry->d_name);
  putchar('\n');
  closedir(dir);

  /*-------------------------------------------------------------------*/
  /* Sort directory entries by alphanumeric name order.                */
  /*-------------------------------------------------------------------*/
  if (sortdir("../sort", cmp_name))
    return -1;

  /*-------------------------------------------------------------------*/
  /* List the directory contents.                                      */
  /*-------------------------------------------------------------------*/
  dir = opendir("../sort");
  while ((entry = readdir(dir)) != NULL)
    printf("  %s", entry->d_name);
  putchar('\n');
  closedir(dir);

  /*-------------------------------------------------------------------*/
  /* Test the directory order.                                         */
  /*-------------------------------------------------------------------*/
  dir = opendir("../sort");
  for (prev = readdir(dir);; prev = entry)
  {
    strncpy(nbuf, prev->d_name, FILENAME_MAX);
    entry = readdir(dir);
    if (entry == NULL)
      break;
    if (strcmp(nbuf, entry->d_name) > 0)
      return -1;
  }
  closedir(dir);

  /*-------------------------------------------------------------------*/
  /* Sort directory entries by file size order.                        */
  /*-------------------------------------------------------------------*/
  if (sortdir("../sort", cmp_size))
    return -1;

  /*-------------------------------------------------------------------*/
  /* List the directory contents.                                      */
  /*-------------------------------------------------------------------*/
  dir = opendir("../sort");
  while ((entry = readdir(dir)) != NULL)
    printf("  %s", entry->d_name);
  putchar('\n');
  closedir(dir);

  /*-------------------------------------------------------------------*/
  /* Test the directory order.                                         */
  /*-------------------------------------------------------------------*/
  dir = opendir("../sort");
  entry = readdir(dir);
  stat(entry->d_name, &pbuf);
  while ((entry = readdir(dir)) != NULL)
  {
    stat(entry->d_name, &ebuf);
    if (pbuf.st_size > ebuf.st_size)
      return -1;
    pbuf.st_size = ebuf.st_size;
  }
  closedir(dir);

  /*-------------------------------------------------------------------*/
  /* Delete the test files.                                            */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < 20; ++i)
  {
    fname[0] = 'A' + i;
    unlink(fname);
  }

  /*-------------------------------------------------------------------*/
  /* Change CWD to volume root and delete test directory.              */
  /*-------------------------------------------------------------------*/
  if (chdir("..") || rmdir("sort"))
    return -1;

  /*-------------------------------------------------------------------*/
  /* Return success.                                                   */
  /*-------------------------------------------------------------------*/
  return 0;
}

/***********************************************************************/
/*   seek_test: Test file postion after reads and seeks                */
/*                                                                     */
/***********************************************************************/
static int seek_test(void)
{
  int fid, res, rc = -1;
  struct stat st;
  ui8 buf[600];
  off_t cur, end;

  /*-------------------------------------------------------------------*/
  /* Create file "test.txt" and write 11 bytes to it.                  */
  /*-------------------------------------------------------------------*/
  fid = open("test.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);
  if (fid == -1)
  {
    perror("open() failed");
    return -1;
  }
  if (write(fid, "01234567890", 11) != 11)
  {
    perror("write() failed");
    goto seek_err;
  }

  /*-------------------------------------------------------------------*/
  /* Close file and then check its size.                               */
  /*-------------------------------------------------------------------*/
  close(fid);
  stat("test.txt", &st);
  if (st.st_size != 11)
  {
    unlink("test.txt");
    perror("stat() reported wrong size");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Open file "test.txt" as read-only file.                           */
  /*-------------------------------------------------------------------*/
  fid = open("test.txt", O_RDONLY);
  if (fid == -1)
  {
    unlink("test.txt");
    perror("open() failed");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Read some bytes and check read()'s return value.                  */
  /*-------------------------------------------------------------------*/
  res = read(fid, buf, 1);
  if (res != 1)
    goto seek_err;
  res = read(fid, buf, 1);
  if (res != 1)
    goto seek_err;
  res = read(fid, buf, 4);
  if (res != 4)
    goto seek_err;

  /*-------------------------------------------------------------------*/
  /* Read remaining bytes with buf size bigger than the sector size.   */
  /*-------------------------------------------------------------------*/
  res = read(fid, buf, 600);
  if (res != 5)
    goto seek_err;

  /*-------------------------------------------------------------------*/
  /* java.io.FileInputStream.available() behaves like this.            */
  /*-------------------------------------------------------------------*/
  cur = lseek(fid, 0, SEEK_CUR);
  if (cur != 11)
    goto seek_err;
  end = lseek(fid, 0, SEEK_END);
  if (end != 11)
    goto seek_err;
  lseek(fid, cur, SEEK_SET);

  /*-------------------------------------------------------------------*/
  /* Read again. We should get 0 as the result.                        */
  /*-------------------------------------------------------------------*/
  res = read(fid, buf, 600);
  if (res)
    goto seek_err;
  res = read(fid, buf, 600);
  if (res)
    goto seek_err;

  /*-------------------------------------------------------------------*/
  /* Either clear return value or print error message. Clean up.       */
  /*-------------------------------------------------------------------*/
  rc = 0;
seek_err:
  if (rc)
    perror("Wrong read() return value");
  close(fid);
  unlink("test.txt");
  return rc;
}

/***********************************************************************/
/*  seek_test2: Test seeks that hit and cross sector boundaries        */
/*                                                                     */
/***********************************************************************/
static int seek_test2(void)
{
  int fid, i, rc = -1;
  ui8 bv;

  /*-------------------------------------------------------------------*/
  /* Create file "test.txt" and write repeating pattern to it.         */
  /*-------------------------------------------------------------------*/
  fid = open("seek.tst", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);
  if (fid == -1)
  {
    perror("open() failed");
    return -1;
  }
  for (i = 0; i < 1234; ++i)
  {
    bv = (ui8)(1234 - i);
    write(fid, &bv, 1);
  }

  /*-------------------------------------------------------------------*/
  /* Repeatedly seek, check position, read, and check value.           */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < 1234; ++i)
  {
    lseek(fid, 0, SEEK_SET);
    lseek(fid, i, SEEK_SET);
    if (ftellFFS(fdopenFFS(fid, NULL)) != i)
    {
      perror("Seek test position error");
      goto seek_err;
    }
    read(fid, &bv, 1);
    if (bv != (ui8)(1234 - i))
    {
      perror("Seek test data error");
      goto seek_err;
    }
  }

  /*-------------------------------------------------------------------*/
  /* Clear return value if successful. Clean up.                       */
  /*-------------------------------------------------------------------*/
  rc = 0;
seek_err:
  close(fid);
  unlink("seek.tst");
  return rc;
}

/***********************************************************************/
/*  utime_test: Test utime()                                           */
/*                                                                     */
/*       Input: vol_name = name of file system volume                  */
/*                                                                     */
/***********************************************************************/
static int utime_test(char *vol_name)
{
  int fid;
  uid_t uid;
  gid_t gid;
  union vstat stats;

  /*-------------------------------------------------------------------*/
  /* Check detection of ENOENT errors.                                 */
  /*-------------------------------------------------------------------*/
  if (mkdir("diru", S_IRUSR | S_IWUSR | S_IXUSR))
  {
    perror("Error making directory");
    return -1;
  }
  if (utime("dira/file", NULL) == 0)
  {
    perror("Missed need for execute permission");
    return -1;
  }
  if (utime("", NULL) == 0)
  {
    perror("Missed need for non-empty path");
    return -1;
  }
  if (errno != ENOENT)
  {
    perror("Set wrong errno value");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Check detection of EFAULT errors.                                 */
  /*-------------------------------------------------------------------*/
  if (utime(NULL, NULL) == 0)
  {
    perror("Missed need for non-NULL path");
    return -1;
  }
  if (errno != EFAULT)
  {
    perror("Set wrong errno value");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Check detection of ENOTDIR errors.                                */
  /*-------------------------------------------------------------------*/
  fid = creat("notdir", 0666);
  if (fid == -1)
  {
    perror("Error creating file");
    return -1;
  }
  if (utime("notdir/file", NULL) == 0)
  {
    perror("Missed need for non-NULL path");
    return -1;
  }
  if (errno != ENOTDIR)
  {
    perror("Set wrong errno value");

⌨️ 快捷键说明

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