Skip to content

Commit

Permalink
fat: add ioctl to get current sector/cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
rasky committed Oct 12, 2024
1 parent 2cd5afc commit 7387ccd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions include/fat.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define LIBDRAGON_FAT_H

#include <stdint.h>
#include "ioctl.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -97,6 +98,33 @@ typedef struct {
} fat_disk_t;


/**
* @brief Return the current cluster number
*
* The cluster number is a 32-bit integer value.
*
* \code{.c}
* FILE *f = fopen("sd:/myfile.dat", "rb");
* int32_t cluster = 0;
* ioctl(fileno(f), IOFAT_GET_CLUSTER, &cluster);
* \endcode
*/
#define IOFAT_GET_CLUSTER _IO('F', 1)

/**
* @brief Return the current sector number
*
* The sector number is a 64-bit integer value.
*
* \code{.c}
* FILE *f = fopen("sd:/myfile.dat", "rb");
* int64_t sector = 0;
* ioctl(fileno(f), IOFAT_GET_SECTOR, &sector);
* \endcode
*/
#define IOFAT_GET_SECTOR _IO('F', 2)


/**
* @brief Mount the volume only when it is accessed for the first time.
*
Expand Down
22 changes: 22 additions & 0 deletions src/fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,27 @@ static int __fat_lseek(void *file, int offset, int whence)
return f_tell(f);
}

static int __fat_ioctl(void *file, unsigned long request, void *arg)
{
FIL *f = file;
switch (request) {
case IOFAT_GET_CLUSTER: {
int32_t *cluster = arg;
*cluster = f->clust;
return 0;
}
case IOFAT_GET_SECTOR: {
int64_t *sector = arg;
*sector = f->sect;
return 0;
}
default: {
errno = ENOTTY;
return -1;
}
}
}

static int __fat_unlink(char *name, int volid)
{
FRESULT res = f_unlink(MAKE_FAT_NAME(volid, name));
Expand Down Expand Up @@ -325,6 +346,7 @@ static const filesystem_t fat_newlib_fs = {
.read = __fat_read,
.write = __fat_write,
.close = __fat_close,
.ioctl = __fat_ioctl,
.unlink = NULL, // per-volume function
.findfirst = NULL, // per-volume function
.findnext = __fat_findnext,
Expand Down

0 comments on commit 7387ccd

Please sign in to comment.