chiark / gitweb /
switch tools and volume_id from LGPL to GPLv2
[elogind.git] / extras / volume_id / volume_id / util.c
index 868d67306cd594a769e26531f21ae204a3749ef7..361d7058d021e4c25978eff3850c9bdd983e0901 100644 (file)
@@ -3,19 +3,9 @@
  *
  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
  *
- *     This library is free software; you can redistribute it and/or
- *     modify it under the terms of the GNU Lesser General Public
- *     License as published by the Free Software Foundation; either
- *     version 2.1 of the License, or (at your option) any later version.
- *
- *     This library is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- *     Lesser General Public License for more details.
- *
- *     You should have received a copy of the GNU Lesser General Public
- *     License along with this library; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *     This program is free software; you can redistribute it and/or modify it
+ *     under the terms of the GNU General Public License as published by the
+ *     Free Software Foundation version 2 of the License.
  */
 
 #ifndef _GNU_SOURCE
@@ -197,26 +187,37 @@ set:
 
 uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
 {
-       size_t buf_len;
+       ssize_t buf_len;
 
        dbg("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len);
        /* check if requested area fits in superblock buffer */
        if (off + len <= SB_BUFFER_SIZE) {
                if (id->sbbuf == NULL) {
                        id->sbbuf = malloc(SB_BUFFER_SIZE);
-                       if (id->sbbuf == NULL)
+                       if (id->sbbuf == NULL) {
+                               dbg("error malloc");
                                return NULL;
+                       }
                }
 
                /* check if we need to read */
                if ((off + len) > id->sbbuf_len) {
                        dbg("read sbbuf len:0x%llx", (unsigned long long) (off + len));
-                       lseek(id->fd, 0, SEEK_SET);
+                       if (lseek(id->fd, 0, SEEK_SET) < 0) {
+                               dbg("lseek failed (%s)", strerror(errno));
+                               return NULL;
+                       }
                        buf_len = read(id->fd, id->sbbuf, off + len);
+                       if (buf_len < 0) {
+                               dbg("read failed (%s)", strerror(errno));
+                               return NULL;
+                       }
                        dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
                        id->sbbuf_len = buf_len;
-                       if (buf_len < off + len)
+                       if ((size_t)buf_len < off + len) {
+                               dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
                                return NULL;
+                       }
                }
 
                return &(id->sbbuf[off]);
@@ -229,20 +230,28 @@ uint8_t *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
                /* get seek buffer */
                if (id->seekbuf == NULL) {
                        id->seekbuf = malloc(SEEK_BUFFER_SIZE);
-                       if (id->seekbuf == NULL)
+                       if (id->seekbuf == NULL) {
+                               dbg("error malloc");
                                return NULL;
+                       }
                }
 
                /* check if we need to read */
                if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
                        dbg("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len);
-                       if (lseek(id->fd, off, SEEK_SET) == -1)
+                       if (lseek(id->fd, off, SEEK_SET) < 0) {
+                               dbg("lseek failed (%s)", strerror(errno));
                                return NULL;
+                       }
                        buf_len = read(id->fd, id->seekbuf, len);
+                       if (buf_len < 0) {
+                               dbg("read failed (%s)", strerror(errno));
+                               return NULL;
+                       }
                        dbg("got 0x%zx (%zi) bytes", buf_len, buf_len);
                        id->seekbuf_off = off;
                        id->seekbuf_len = buf_len;
-                       if (buf_len < len) {
+                       if ((size_t)buf_len < len) {
                                dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len);
                                return NULL;
                        }