"du" in C

Jan-Benedict Glaw jbglaw at lug-owl.de
Fri Jul 12 17:25:02 CEST 2002


On Fri, 2002-07-12 16:33:24 +0200, Andre Landwehr <andre.landwehr at gmx.net>
wrote in message <20020712143324.GI1916 at twintower>:
> On Fri, Jul 12, 2002 at 02:55:39PM +0200, Florian Lohoff wrote:
> > Festplatte ist dabei falsch - CDs und MOs haben teilweise 2048
> > byte bloecke. Wie funktioniert das dann ? Man bekommt die blockgroesse
> > des filesystems auch raus via ioctl(FIGETBSZ) auf den filedescriptor.
> > ggfs. ist das allerdings root only.
> 
> Mehr oder weniger - man braucht Leserechte so wie ich das sehe.
> 
> Es bleibt die Frage, wie man da jetzt eine verläßliche Angabe
> machen kann. Es gibt zwei Möglichkeiten:
> a) entry->dusize = 512 * st_blocks; // (aus struct stat)
> 	 Wobei es keine Möglichkeit (zumindest für Normaluser) zu geben
>    scheint, die tatsächliche Blockgröße auf dem Datenträger
>    rauszufinden. Zumindest schlug ein ioctl(BLKSSZGET) fehl, so daß

"Inappropriate ioctl for device" - Du mußt den mit /dev/sdXN machen,
nicht mit einem fd auf eine Datei.

> b) if (ioctl (fd, FIGETBSZ, blksize) == 0) {
>       blocks = (buf->st_size % *blksize > 0) ? 1 : 0;
>       blocks += buf->st_size / *blksize;
>       entry->dusize = blocks * *blksize;
>    }

Code sieht falsch aus. "*blksize" bei der Benutzung sieht aus, als
würdest Du das so stehen haben:

	int	*blksize;
	....
	ioctl (fd, FIGETBSZ, blksize);

Du mußt aber einen Pointer auf eine int-Variable, für die Du Speicher
hast, übergeben. Die libc besorgt Dir den nicht. Das muß also etwa so
aussehen:

	int	blksize;
	...
	ioctl (fd, FIGETBSZ, &blksize);

...und das hier tut ganz gut:


#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
int
main(int argc, char *argv[])
{
        int     blksize;
        int     fd;

        /* fd = open("./testfile", O_RDONLY); */
        fd = open("/dev/hda1", O_RDONLY);
        if(fd < 0) {
                perror("2nd open()");
                return EXIT_FAILURE;
        }

        /* if(ioctl(fd, FIGETBSZ, &blksize)!= 0) { */
        if(ioctl(fd, BLKSSZGET, &blksize)!= 0) {
                perror("ioctl()");
                close(fd);
                return EXIT_FAILURE;
        }

        close(fd);

        printf("Blocksize = %d\n", blksize);

        return EXIT_SUCCESS;
}


...aber dafür mutß Du in Gruppe "disks" sein (zumindest bei Debian).

MfG, JBG

-- 
Jan-Benedict Glaw   .   jbglaw at lug-owl.de   .   +49-172-7608481
	 -- New APT-Proxy written in shell script --
	   http://lug-owl.de/~jbglaw/software/ap2/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lug-owl.de/pipermail/linux/attachments/20020712/3306aa4b/attachment.sig>


More information about the Linux mailing list