Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / lib / ext2fs / unix_io.c
index 0baae38..e0cc4db 100644 (file)
  * %End-Header%
  */
 
+#define _LARGEFILE_SOURCE
+#define _LARGEFILE64_SOURCE
+
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#include <stdlib.h>
+#if HAVE_ERRNO_H
+#include <errno.h>
+#endif
 #include <fcntl.h>
 #include <time.h>
+#if HAVE_SYS_STAT_H
 #include <sys/stat.h>
+#endif
+#if HAVE_SYS_TYPES_H
 #include <sys/types.h>
-#if HAVE_ERRNO_H
-#include <errno.h>
 #endif
 
-#include "et/com_err.h"
-#include "ext2fs/ext2_err.h"
-#include "io.h"
+#if EXT2_FLAT_INCLUDES
+#include "ext2_fs.h"
+#else
+#include <linux/ext2_fs.h>
+#endif
+
+#include "ext2fs.h"
 
 /*
  * For checking structure magic numbers...
@@ -71,26 +81,26 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)
        io_channel      io = NULL;
        struct unix_private_data *data = NULL;
        errcode_t       retval;
+       int             open_flags;
 
        if (name == 0)
                return EXT2_ET_BAD_DEVICE_NAME;
-       io = (io_channel) malloc(sizeof(struct struct_io_channel));
-       if (!io)
-               return ENOMEM;
+       retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
+                               (void **) &io);
+       if (retval)
+               return retval;
        memset(io, 0, sizeof(struct struct_io_channel));
        io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
-       data = (struct unix_private_data *)
-               malloc(sizeof(struct unix_private_data));
-       if (!data) {
-               retval = ENOMEM;
+       retval = ext2fs_get_mem(sizeof(struct unix_private_data),
+                               (void **) &data);
+       if (retval)
                goto cleanup;
-       }
+
        io->manager = unix_io_manager;
-       io->name = malloc(strlen(name)+1);
-       if (!io->name) {
-               retval = ENOMEM;
+       retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
+       if (retval)
                goto cleanup;
-       }
+
        strcpy(io->name, name);
        io->private_data = data;
        io->block_size = 1024;
@@ -100,13 +110,17 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)
 
        memset(data, 0, sizeof(struct unix_private_data));
        data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
-       data->buf = malloc(io->block_size);
+       retval = ext2fs_get_mem(io->block_size, (void **) &data->buf);
        data->buf_block_nr = -1;
-       if (!data->buf) {
-               retval = ENOMEM;
+       if (retval)
                goto cleanup;
-       }
-       data->dev = open(name, (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY);
+
+       open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
+#ifdef HAVE_OPEN64
+       data->dev = open64(name, open_flags);
+#else
+       data->dev = open(name, open_flags);
+#endif
        if (data->dev < 0) {
                retval = errno;
                goto cleanup;
@@ -116,11 +130,11 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)
 
 cleanup:
        if (io)
-               free(io);
+               ext2fs_free_mem((void **) &io);
        if (data) {
                if (data->buf)
-                       free(data->buf);
-               free(data);
+                       ext2fs_free_mem((void **) &data->buf);
+               ext2fs_free_mem((void **) &data);
        }
        return retval;
 }
@@ -140,18 +154,19 @@ static errcode_t unix_close(io_channel channel)
        if (close(data->dev) < 0)
                retval = errno;
        if (data->buf)
-               free(data->buf);
+               ext2fs_free_mem((void **) &data->buf);
        if (channel->private_data)
-               free(channel->private_data);
+               ext2fs_free_mem((void **) &channel->private_data);
        if (channel->name)
-               free(channel->name);
-       free(channel);
+               ext2fs_free_mem((void **) &channel->name);
+       ext2fs_free_mem((void **) &channel);
        return retval;
 }
 
 static errcode_t unix_set_blksize(io_channel channel, int blksize)
 {
        struct unix_private_data *data;
+       errcode_t               retval;
 
        EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
        data = (struct unix_private_data *) channel->private_data;
@@ -159,10 +174,10 @@ static errcode_t unix_set_blksize(io_channel channel, int blksize)
 
        if (channel->block_size != blksize) {
                channel->block_size = blksize;
-               free(data->buf);
-               data->buf = malloc(blksize);
-               if (!data->buf)
-                       return ENOMEM;
+               ext2fs_free_mem((void **) &data->buf);
+               retval = ext2fs_get_mem(blksize, (void **) &data->buf);
+               if (retval)
+                       return retval;
                data->buf_block_nr = -1;
        }
        return 0;
@@ -195,7 +210,7 @@ static errcode_t unix_read_blk(io_channel channel, unsigned long block,
        size = (count < 0) ? -count : count * channel->block_size;
        location = (ext2_loff_t) block * channel->block_size;
        if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
-               retval = errno ? errno : EXT2_IO_LLSEEK_FAILED;
+               retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
                goto error_out;
        }
        actual = read(data->dev, buf, size);
@@ -244,7 +259,7 @@ static errcode_t unix_write_blk(io_channel channel, unsigned long block,
 
        location = (ext2_loff_t) block * channel->block_size;
        if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
-               retval = errno ? errno : EXT2_IO_LLSEEK_FAILED;
+               retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
                goto error_out;
        }