0

I have an application where I'm writing data on the removable media (SD card/ pendrive). Now, I want to add a functionality where I can format the SD card in case it's not mounted properly or got corrupted for any reason.

I know I can use system() method to call an underlying utility like mkfs.ext4 for formatting the removable partition as we do via terminal, or a bash script which will unmount, format and remount the removable media. But I would like to avoid system() call because:

It spawns a new shell over which I will have no control. In this particular case, I cannot check the status of unmounting/ formatting/ remounting, which prevents me from proper error handling in the application. Is there a system library I can use to do the same task in C++ way (using APIs/ method calls) rather than having to call system() and then a bash script within.

Please correct if I misunderstood something.

Please suggest.

Thanks

Rhythm Chopra
  • 103
  • 3
  • 9
  • There is no such think in linux afaik. [This question](https://stackoverflow.com/questions/10166750/writing-an-ext4-file-system-in-c) might help. – Michael Chourdakis Feb 09 '20 at 09:52

1 Answers1

0

I would start from the libext2fs-dev and e2fslibs-dev packages (see function ext2fs_initialize in ext2fs/ext2fs.h).

This is what linux distributions are using to handle such filesystems, but the documentation is a bit scarce:

2.1.3 Initializing a filesystem

An ext2 filesystem is initializing by the 'mke2fs' program. The two functions described here, 'ext2fs_initialize' and 'ext2fs_allocate_tables' do much of the initial work for setting up a filesystem. However, they don't do the whole job. 'mke2fs' calls 'ext2fs_initialize' to set up the filesystem superblock, and calls 'ext2fs_allocate_tables' to allocate space for the inode table, and the inode and block bitmaps. In addition, 'mke2fs' must also initialize the inode tables by clearing them with zeros, create the root and lost+found directories, and reserve the reserved inodes.

The quote is from the generated doc of a snapshot of the project (git repo https://git.kernel.org/pub/scm/fs/ext2/e2fsprogs.git)

I also found this online pdf file if you want to have a quick look...

zmb
  • 61
  • 5