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

📄 main.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 4 页
字号:

    fid2 = open(fname2, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);
    if (fid2 == -1)
    {
      perror("Error opening the file");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Exercise fcntl() with F_GETFL and F_SETFL.                      */
    /*-----------------------------------------------------------------*/
    i = fcntl(fid, F_GETFL);
    printf("flags = 0x%02X\n", i);
    fcntl(fid, F_SETFL, O_ASYNC | O_NONBLOCK);
    i = fcntl(fid, F_GETFL);
    printf("flags = 0x%02X\n", i);

    /*-----------------------------------------------------------------*/
    /* Write contents of hello[] to files.                             */
    /*-----------------------------------------------------------------*/
    if (write(fid, hello, sizeof(hello)) != sizeof(hello) )
    {
      perror("Error writing to file");
      return -1;
    }

    for (i = 0; i < 1000; ++i)
    {
      if (write(fid2, hello, sizeof(hello)) != sizeof(hello))
      {
        perror("Error writing to file");
        return -1;
      }
    }

    /*-----------------------------------------------------------------*/
    /* Retrieve stats on the two files.                                */
    /*-----------------------------------------------------------------*/
    if (fstat(fid, &stbuf))
      perror("Error reading file stats");
    else
    {
      printf("File access time:   %s", ctime(&stbuf.st_atime));
      printf("File modified time: %s", ctime(&stbuf.st_mtime));
      printf("File mode:          %d\n", stbuf.st_mode);
      printf("File serial number: %d\n", stbuf.st_ino);
      printf("File num links:     %d\n", stbuf.st_nlink);
      printf("File size (byte):   %u\n", stbuf.st_size);
      printf("File user ID:       %u\n", stbuf.st_uid);
      printf("File group ID:      %u\n", stbuf.st_gid);
    }

    if (fstat(fid2, &stbuf))
      perror("Error reading file stats");
    else
    {
      printf("File access time:   %s", ctime(&stbuf.st_atime));
      printf("File modified time: %s", ctime(&stbuf.st_mtime));
      printf("File mode:          %d\n", stbuf.st_mode);
      printf("File serial number: %d\n", stbuf.st_ino);
      printf("File num links:     %d\n", stbuf.st_nlink);
      printf("File size (byte):   %u\n", stbuf.st_size);
      printf("File user ID:       %u\n", stbuf.st_uid);
      printf("File group ID:      %u\n", stbuf.st_gid);
    }

    /*-----------------------------------------------------------------*/
    /* Use ftruncate to shorten the file to half its length.           */
    /*-----------------------------------------------------------------*/
    offset = lseek(fid2, 0, SEEK_END);
    if (ftruncate(fid2, offset / 2))
    {
      perror("Error truncating file");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Display and verify the file's size.                             */
    /*-----------------------------------------------------------------*/
    stat(fname2, &stbuf);
    printf("\"%s\" size after truncate(., %d) = %d\n",
           fname2, offset / 2, stbuf.st_size);
    if (stbuf.st_size != (offset / 2))
    {
      printf("File length is unexpected\n");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Move file position to the new end of file.                      */
    /*-----------------------------------------------------------------*/
    if (lseek(fid2, (off_t)0, SEEK_END) < 0)
    {
      perror("lseek() failed");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Write four bytes and verify the file's size.                    */
    /*-----------------------------------------------------------------*/
    if (write(fid2, "\0\1\2\3", 4) != 4)
    {
      perror("Error writing to file");
      return -1;
    }
    stat(fname2, &stbuf);
    printf("\"%s\" size after write() = %d\n", fname2, stbuf.st_size);
    if (stbuf.st_size != (offset / 2 + 4))
    {
      printf("File length is unexpected\n");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Use truncate() to extend the file to its original length.       */
    /*-----------------------------------------------------------------*/
    if (truncate(fname2, offset))
    {
      perror("Error truncating file");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Display and verify the file's length.                           */
    /*-----------------------------------------------------------------*/
    stat(fname2, &stbuf);
    printf("\"%s\" size after truncate(., %d) = %d\n",
           fname2, offset, stbuf.st_size);
    if (stbuf.st_size != offset)
    {
      printf("File length is unexpected\n");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* truncate() should not affect the file offset. So write four     */
    /* bytes and verify the length is unchanged.                       */
    /*-----------------------------------------------------------------*/
    if (write(fid2, "\0\1\2\3", 4) != 4)
    {
      perror("Error writing to file");
      return -1;
    }
    stat(fname2, &stbuf);
    printf("\"%s\" size after write() = %d\n", fname2, stbuf.st_size);
    if (stbuf.st_size != offset)
    {
      printf("File length is unexpected\n");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Ensure file holds zero from current position to original end.   */
    /*-----------------------------------------------------------------*/
    for (i = offset / 2 + 8; i < offset; ++i)
    {
      ui8 ch;

      if (read(fid2, &ch, 1) != 1)
      {
        perror("Error reading from file");
        return -1;
      }
      if (ch)
      {
        printf("File not zero-filled correctly\n");
        return -1;
      }
    }

    /*-----------------------------------------------------------------*/
    /* Truncate the file to a multiple of the sector size.             */
    /*-----------------------------------------------------------------*/
    if (ftruncate(fid2, stats.ffs.sect_size * 3))
    {
      perror("Error truncating file");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Ensure the truncated file has the correct length.               */
    /*-----------------------------------------------------------------*/
    if (fstat(fid2, &stbuf))
    {
      perror("Error reading file's statistics");
      return -1;
    }
    if (stbuf.st_size != stats.ffs.sect_size * 3)
    {
      perror("file has wrong size after truncation");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Close the working files.                                        */
    /*-----------------------------------------------------------------*/
    if (close(fid))
    {
      perror("Error closing file");
      return -1;
    }
    if (close(fid2))
    {
      perror("Error closing file");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Delete the working files.                                       */
    /*-----------------------------------------------------------------*/
    if (unlink(fname))
    {
      perror("Error removing file");
      return -1;
    }
    if (unlink(fname2))
    {
      perror("Error removing file");
      return -1;
    }
  }

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

/***********************************************************************/
/* creatn_test: Test the creatn() function                             */
/*                                                                     */
/*       Input: vol_name = name of file system volume                  */
/*                                                                     */
/***********************************************************************/
static int creatn_test(char *vol_name)
{
  int fid, i, rc, sect_size;
  char *fname = "log_file";
  struct stat stbuf;
  union vstat stats;
  void *buf;

  /*-------------------------------------------------------------------*/
  /* Determine the volume's sector size.                               */
  /*-------------------------------------------------------------------*/
  if (vstat(vol_name, &stats))
  {
    perror("Error reading volume statistics");
    return -1;
  }
  sect_size = stats.ffs.sect_size;

  /*-------------------------------------------------------------------*/
  /* Allocate a sector-sized buffer for writing to the file.           */
  /*-------------------------------------------------------------------*/
  buf = malloc(sect_size);
  if (buf == NULL)
  {
    perror("Error allocating creatn() buffer");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Create a special file with the length of 10 sectors.              */
  /*-------------------------------------------------------------------*/
  fid = creatn(fname, 0666, 10 * sect_size);
  if (fid == -1)
  {
    perror("Error creating file");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Many writes that should all succeed and not trigger a recycle.    */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < 10; ++i)
  {
    /*-----------------------------------------------------------------*/
    /* Write a full sector of data.                                    */
    /*-----------------------------------------------------------------*/
    if (write(fid, buf, sect_size) != sect_size)
    {
      perror("Error writing to file");
      return -1;
    }

    /*-----------------------------------------------------------------*/
    /* Read file's statistics and check that its size hasn't changed.  */
    /*-----------------------------------------------------------------*/
    if (fstat(fid, &stbuf))
    {
      perror("Error reading file's statistics");
      return -1;
    }
    if (stbuf.st_size != 10 * sect_size)
    {
      perror("creatn() file size changed");
      return -1;
    }
  }

  /*-------------------------------------------------------------------*/
  /* Try to write past the end.                                        */
  /*-------------------------------------------------------------------*/
  rc = lseek(fid, 0, SEEK_END);
  if (rc < 0)
  {
    perror("Error seeking to end");
    return -1;
  }
  if (write(fid, buf, sect_size) != -1)
  {
    perror("Able to write past creatn() end");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Try to seek past the end.                                         */
  /*-------------------------------------------------------------------*/
  rc = lseek(fid, sect_size, SEEK_END);
  if (rc > 0)
  {
    perror("Able to seek past creatn() end");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Close and delete the file.                                        */
  /*-------------------------------------------------------------------*/
  if (close(fid))
  {
    perror("Error closing file");
    return -1;
  }
  if (unlink(fname))
  {
    perror("Error removing file");
    return -1;
  }

  /*-------------------------------------------------------------------*/
  /* Free sector write buffer and return success.                      */
  /*-------------------------------------------------------------------*/
  free(buf);
  return 0;
}

/***********************************************************************/
/*    cmp_name: Compare directory entries by name                      */
/*                                                                     */
/***********************************************************************/
static int cmp_name(const DirEntry *e1, const DirEntry *e2)
{
  return strcmp(e1->st_name, e2->st_name);
}

/***********************************************************************/

⌨️ 快捷键说明

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