<commit_before>/* Copyright 2013-2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>

#include <ccan/container_of/container_of.h>

#include <mtd/mtd-abi.h>

#include "libflash.h"
#include "blocklevel.h"

struct file_data {
	int fd;
	char *name;
	char *path;
	struct blocklevel_device bl;
};

static int file_release(struct blocklevel_device *bl)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	close(file_data->fd);
	file_data->fd = -1;
	return 0;
}

static int file_reacquire(struct blocklevel_device *bl)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	int fd;

	fd = open(file_data->path, O_RDWR);
	if (fd == -1)
		return FLASH_ERR_PARM_ERROR;
	file_data->fd = fd;
	return 0;
}

static int file_read(struct blocklevel_device *bl, uint32_t pos, void *buf, uint32_t len)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	int rc, count = 0;

	rc = lseek(file_data->fd, pos, SEEK_SET);
	/* errno should remain set */
	if (rc != pos)
		return FLASH_ERR_PARM_ERROR;

	while (count < len) {
		rc = read(file_data->fd, buf, len);
		/* errno should remain set */
		if (rc == -1)
			return FLASH_ERR_BAD_READ;

		count += rc;
	}<commit_msg>libflash: fix bug on reading truncated flash file<commit_after>/* Copyright 2013-2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>

#include <ccan/container_of/container_of.h>

#include <mtd/mtd-abi.h>

#include "libflash.h"
#include "blocklevel.h"

struct file_data {
	int fd;
	char *name;
	char *path;
	struct blocklevel_device bl;
};

static int file_release(struct blocklevel_device *bl)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	close(file_data->fd);
	file_data->fd = -1;
	return 0;
}

static int file_reacquire(struct blocklevel_device *bl)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	int fd;

	fd = open(file_data->path, O_RDWR);
	if (fd == -1)
		return FLASH_ERR_PARM_ERROR;
	file_data->fd = fd;
	return 0;
}

static int file_read(struct blocklevel_device *bl, uint32_t pos, void *buf, uint32_t len)
{
	struct file_data *file_data = container_of(bl, struct file_data, bl);
	int rc, count = 0;

	rc = lseek(file_data->fd, pos, SEEK_SET);
	/* errno should remain set */
	if (rc != pos)
		return FLASH_ERR_PARM_ERROR;

	while (count < len) {
		rc = read(file_data->fd, buf, len);
		/* errno should remain set */
		if (rc == -1 || rc == 0)
			return FLASH_ERR_BAD_READ;

		count += rc;
	}
{'fraction_non_alphanumeric': 0.11929382093316519, 'fraction_numerical': 0.011349306431273645, 'mean_word_length': 4.246031746031746, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5125920', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1394, 'n_words': 449}