Merge tag 'u-boot-atmel-fixes-2021.01-b' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / fs / squashfs / sqfs.c
index 4350816..5de69ac 100644 (file)
@@ -49,6 +49,7 @@ static int sqfs_read_sblk(struct squashfs_super_block **sblk)
 
        if (sqfs_disk_read(0, 1, *sblk) != 1) {
                free(*sblk);
+               sblk = NULL;
                return -EINVAL;
        }
 
@@ -1326,6 +1327,14 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
 
        *actread = 0;
 
+       if (offset) {
+               /*
+                * TODO: implement reading at an offset in file
+                */
+               printf("Error: reading at a specific offset in a squashfs file is not supported yet.\n");
+               return -EINVAL;
+       }
+
        /*
         * sqfs_opendir will uncompress inode and directory tables, and will
         * return a pointer to the directory that contains the requested file.
@@ -1415,6 +1424,8 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
                }
 
                finfo.size = len;
+       } else {
+               len = finfo.size;
        }
 
        if (datablk_count) {
@@ -1461,16 +1472,22 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
                        if (ret)
                                goto out;
 
-                       memcpy(buf + offset + *actread, datablock, dest_len);
+                       if ((*actread + dest_len) > len)
+                               dest_len = len - *actread;
+                       memcpy(buf + *actread, datablock, dest_len);
                        *actread += dest_len;
                } else {
-                       memcpy(buf + offset + *actread, data, table_size);
+                       if ((*actread + table_size) > len)
+                               table_size = len - *actread;
+                       memcpy(buf + *actread, data, table_size);
                        *actread += table_size;
                }
 
                data_offset += table_size;
                free(data_buffer);
                data_buffer = NULL;
+               if (*actread >= len)
+                       break;
        }
 
        /*
@@ -1514,7 +1531,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
                        goto out;
                }
 
-               for (j = offset + *actread; j < finfo.size; j++) {
+               for (j = *actread; j < finfo.size; j++) {
                        memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
                        (*actread)++;
                }
@@ -1524,7 +1541,7 @@ int sqfs_read(const char *filename, void *buf, loff_t offset, loff_t len,
        } else if (finfo.frag && !finfo.comp) {
                fragment_block = (void *)fragment + table_offset;
 
-               for (j = offset + *actread; j < finfo.size; j++) {
+               for (j = *actread; j < finfo.size; j++) {
                        memcpy(buf + j, &fragment_block[finfo.offset + j], 1);
                        (*actread)++;
                }
@@ -1633,11 +1650,50 @@ free_strings:
        return ret;
 }
 
+int sqfs_exists(const char *filename)
+{
+       struct fs_dir_stream *dirsp = NULL;
+       struct squashfs_dir_stream *dirs;
+       char *dir, *file;
+       struct fs_dirent *dent;
+       int ret;
+
+       sqfs_split_path(&file, &dir, filename);
+       /*
+        * sqfs_opendir will uncompress inode and directory tables, and will
+        * return a pointer to the directory that contains the requested file.
+        */
+       ret = sqfs_opendir(dir, &dirsp);
+       if (ret) {
+               ret = -EINVAL;
+               goto free_strings;
+       }
+
+       dirs = (struct squashfs_dir_stream *)dirsp;
+
+       while (!sqfs_readdir(dirsp, &dent)) {
+               ret = strcmp(dent->name, file);
+               if (!ret)
+                       break;
+               free(dirs->entry);
+               dirs->entry = NULL;
+       }
+
+       sqfs_closedir(dirsp);
+
+free_strings:
+       free(dir);
+       free(file);
+
+       return ret == 0;
+}
+
 void sqfs_close(void)
 {
+       sqfs_decompressor_cleanup(&ctxt);
        free(ctxt.sblk);
+       ctxt.sblk = NULL;
        ctxt.cur_dev = NULL;
-       sqfs_decompressor_cleanup(&ctxt);
 }
 
 void sqfs_closedir(struct fs_dir_stream *dirs)