summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2022-12-29 23:37:32 +0100
committerCarl Hetherington <cth@carlh.net>2023-01-30 20:52:20 +0100
commitb98f55b806f9dfe9e9374faceed99b689c29f28e (patch)
tree5c626761e0814ae7b9434a5fc66b1e2100743893
parent3dc752da8948564456ce2d721c266b4e1a50e7a1 (diff)
Return platform-specific errors via ext4_blockdev_errno2400-ext-errors
-rw-r--r--blockdev/blockdev.c3
-rw-r--r--blockdev/linux/file_dev.c5
-rw-r--r--blockdev/windows/file_windows.c12
-rw-r--r--include/ext4_blockdev.h2
-rw-r--r--include/ext4_errno.h4
-rw-r--r--src/ext4_errno.c30
6 files changed, 50 insertions, 6 deletions
diff --git a/blockdev/blockdev.c b/blockdev/blockdev.c
index a44a2e1..15ce00f 100644
--- a/blockdev/blockdev.c
+++ b/blockdev/blockdev.c
@@ -32,6 +32,9 @@
#include <ext4_errno.h>
+uint32_t ext4_blockdev_errno;
+
+
/**********************BLOCKDEV INTERFACE**************************************/
static int blockdev_open(struct ext4_blockdev *bdev);
static int blockdev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
diff --git a/blockdev/linux/file_dev.c b/blockdev/linux/file_dev.c
index 4e4cc67..77c6112 100644
--- a/blockdev/linux/file_dev.c
+++ b/blockdev/linux/file_dev.c
@@ -77,6 +77,7 @@ static int file_dev_open(struct ext4_blockdev *bdev)
if (dev_file < 0) {
printf("open of %s failed %d\n", fname, errno);
+ ext4_blockdev_errno = errno;
return EIO;
}
@@ -119,12 +120,14 @@ static int file_dev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id
uint32_t blk_cnt)
{
if (lseek(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET) < 0) {
+ ext4_blockdev_errno = errno;
printf("lseek failed %d\n", errno);
return EIO;
}
if (!blk_cnt)
return EOK;
if (read(dev_file, buf, bdev->bdif->ph_bsize * blk_cnt) < 0) {
+ ext4_blockdev_errno = errno;
printf("read failed %d\n", errno);
return EIO;
}
@@ -150,12 +153,14 @@ static int file_dev_bwrite(struct ext4_blockdev *bdev, const void *buf,
uint64_t blk_id, uint32_t blk_cnt)
{
if (lseek(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET) < 0) {
+ ext4_blockdev_errno = errno;
printf("fseeko failed %d\n", errno);
return EIO;
}
if (!blk_cnt)
return EOK;
if (write(dev_file, buf, bdev->bdif->ph_bsize * blk_cnt) < 0) {
+ ext4_blockdev_errno = errno;
printf("write failed %d\n", errno);
return EIO;
}
diff --git a/blockdev/windows/file_windows.c b/blockdev/windows/file_windows.c
index 45588e3..75a372f 100644
--- a/blockdev/windows/file_windows.c
+++ b/blockdev/windows/file_windows.c
@@ -80,6 +80,7 @@ static int file_open(struct ext4_blockdev *bdev)
FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, NULL);
if (dev_file == INVALID_HANDLE_VALUE) {
+ ext4_blockdev_errno = GetLastError();
return EIO;
}
@@ -88,6 +89,7 @@ static int file_open(struct ext4_blockdev *bdev)
&pdg, sizeof(pdg), &junk, (LPOVERLAPPED)NULL);
if (bResult == FALSE) {
+ ext4_blockdev_errno = GetLastError();
CloseHandle(dev_file);
return EIO;
}
@@ -116,19 +118,18 @@ static int file_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
{
long hipart = blk_id >> (32 - 9);
long lopart = blk_id << 9;
- long err;
SetLastError(0);
lopart = SetFilePointer(dev_file, lopart, &hipart, FILE_BEGIN);
- if (lopart == -1 && NO_ERROR != (err = GetLastError())) {
+ if (lopart == -1 && NO_ERROR != (ext4_blockdev_errno = GetLastError())) {
return EIO;
}
DWORD n;
if (!ReadFile(dev_file, buf, blk_cnt * 512, &n, NULL)) {
- err = GetLastError();
+ ext4_blockdev_errno = GetLastError();
return EIO;
}
return EOK;
@@ -140,19 +141,18 @@ static int file_bwrite(struct ext4_blockdev *bdev, const void *buf,
{
long hipart = blk_id >> (32 - 9);
long lopart = blk_id << 9;
- long err;
SetLastError(0);
lopart = SetFilePointer(dev_file, lopart, &hipart, FILE_BEGIN);
- if (lopart == -1 && NO_ERROR != (err = GetLastError())) {
+ if (lopart == -1 && NO_ERROR != (ext4_blockdev_errno = GetLastError())) {
return EIO;
}
DWORD n;
if (!WriteFile(dev_file, buf, blk_cnt * 512, &n, NULL)) {
- err = GetLastError();
+ ext4_blockdev_errno = GetLastError();
return EIO;
}
return EOK;
diff --git a/include/ext4_blockdev.h b/include/ext4_blockdev.h
index 4673479..19b3e0c 100644
--- a/include/ext4_blockdev.h
+++ b/include/ext4_blockdev.h
@@ -258,6 +258,8 @@ int ext4_block_cache_flush(struct ext4_blockdev *bdev);
* @return standard error code*/
int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
+extern uint32_t ext4_blockdev_errno;
+
#ifdef __cplusplus
}
#endif
diff --git a/include/ext4_errno.h b/include/ext4_errno.h
index edf89a9..6735eaf 100644
--- a/include/ext4_errno.h
+++ b/include/ext4_errno.h
@@ -41,6 +41,7 @@ extern "C" {
#endif
#include "ext4_config.h"
+#include <stdint.h>
#if !CONFIG_HAVE_OWN_ERRNO
#include <errno.h>
@@ -84,6 +85,9 @@ extern "C" {
#define EOK 0
#endif
+/* Platform-specific error code from the last OS API call that was made */
+extern uint32_t ext4_platform_errno;
+
#ifdef __cplusplus
}
#endif
diff --git a/src/ext4_errno.c b/src/ext4_errno.c
new file mode 100644
index 0000000..e5c57b9
--- /dev/null
+++ b/src/ext4_errno.c
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+int ext4_platform_errno;