file.c

来自「linux下自动mount各种即插即用设备的一个小程序源码 文件包含内容: 」· C语言 代码 · 共 129 行

C
129
字号
/* * file.c * Data source for files and block devices. * * Copyright (c) 2003 Christoph Pfisterer * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.  */#include "file.h"#define USE_BINARY_SEARCH 0#define DEBUG_SIZE 0#ifdef USE_IOCTL_LINUX#include <sys/ioctl.h>#include <linux/fs.h>#endif#ifdef USE_IOCTL_FREEBSD#include <sys/disklabel.h>#endif#ifdef USE_IOCTL_DARWIN#include <sys/ioctl.h>#include <sys/disk.h>#endif/* * special handling hook: devices may have out-of-band structure */int analyze_file(SOURCE *s, DT_Info* info) {	if(s->source_open == 0) return 0;		if (analyze_cdaccess(s->fd, s, 0))		return 1;	return 0;}/* * raw read */U64 read_file(SOURCE *s, U64 pos, U64 len, void *buf) {	off_t result_seek;	ssize_t result_read;	char *p;	U64 got;	int fd = s->fd;		if(s->source_open == 0) return 0;	/* seek to the requested position */	result_seek = lseek(fd, pos, SEEK_SET);	if (result_seek != pos) {		errore("Seek to %llu failed", pos);		return 0;	}	/* read from there */	p = (char *)buf;	got = 0;	while (len > 0) {		result_read = read(fd, p, len);		if (result_read < 0) {			if (errno == EINTR || errno == EAGAIN)				continue;			errore("Data read failed at position %llu", pos + got);			break;		} else if (result_read == 0) {			/* simple EOF, no message */			break;		} else {			len -= result_read;			got += result_read;			p += result_read;		}	}	return got;}/* * dispose of everything */void close_file(SOURCE *s) {	DBG("closing source");	if(s->source_open != 0)		close(s->fd);}int open_file(SOURCE *s, char* filename) {	if(s->source_open != 0) {		error("Internal malfunction! SOURCE already opened!");		return 0;	} else {		s->fd = open(filename, O_RDONLY);		if(s->fd < 0) {			errore("Can't open %.300s for reading (detect)", filename);			return 0;		}		s->source_open = 1;	}	return 1;}/* EOF */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?