void check_plausibility(const char *device) { int val; ext2fs_struct_stat s; val = ext2fs_stat(device, &s); if(val == -1) { fprintf(stderr, _("Could not stat %s --- %s\n"), device, error_message(errno)); if (errno == ENOENT) fputs(_("\nThe device apparently does not exist; " "did you specify it correctly?\n"), stderr); exit(1); } #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) /* On FreeBSD, all disk devices are character specials */ if (!S_ISBLK(s.st_mode) && !S_ISCHR(s.st_mode)) #else if (!S_ISBLK(s.st_mode)) #endif { printf(_("%s is not a block special device.\n"), device); proceed_question(); return; } #ifdef HAVE_LINUX_MAJOR_H #ifndef MAJOR #define MAJOR(dev) ((dev)>>8) #define MINOR(dev) ((dev) & 0xff) #endif #ifndef SCSI_BLK_MAJOR #ifdef SCSI_DISK0_MAJOR #ifdef SCSI_DISK8_MAJOR #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \ ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \ ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR)) #else #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \ ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR)) #endif /* defined(SCSI_DISK8_MAJOR) */ #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR) #else #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR) #endif /* defined(SCSI_DISK0_MAJOR) */ #endif /* defined(SCSI_BLK_MAJOR) */ if (((MAJOR(s.st_rdev) == HD_MAJOR && MINOR(s.st_rdev)%64 == 0) || (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) && MINOR(s.st_rdev)%16 == 0))) { printf(_("%s is entire device, not just one partition!\n"), device); proceed_question(); } #endif }
static errcode_t unix_open(const char *name, int flags, io_channel *channel) { io_channel io = NULL; struct unix_private_data *data = NULL; errcode_t retval; int open_flags, zeroes = 0; int f_nocache = 0; ext2fs_struct_stat st; #ifdef __linux__ struct utsname ut; #endif if (name == 0) return EXT2_ET_BAD_DEVICE_NAME; retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io); if (retval) goto cleanup; memset(io, 0, sizeof(struct struct_io_channel)); io->magic = EXT2_ET_MAGIC_IO_CHANNEL; retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data); if (retval) goto cleanup; io->manager = unix_io_manager; retval = ext2fs_get_mem(strlen(name)+1, &io->name); if (retval) goto cleanup; strcpy(io->name, name); io->private_data = data; io->block_size = 1024; io->read_error = 0; io->write_error = 0; io->refcount = 1; memset(data, 0, sizeof(struct unix_private_data)); data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL; data->io_stats.num_fields = 2; open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY; if (flags & IO_FLAG_EXCLUSIVE) open_flags |= O_EXCL; #if defined(O_DIRECT) if (flags & IO_FLAG_DIRECT_IO) { open_flags |= O_DIRECT; io->align = ext2fs_get_dio_alignment(data->dev); } #elif defined(F_NOCACHE) if (flags & IO_FLAG_DIRECT_IO) { f_nocache = F_NOCACHE; io->align = 4096; } #endif data->flags = flags; data->dev = ext2fs_open_file(io->name, open_flags, 0); if (data->dev < 0) { retval = errno; goto cleanup; } if (f_nocache) { if (fcntl(data->dev, f_nocache, 1) < 0) { retval = errno; goto cleanup; } } /* * If the device is really a block device, then set the * appropriate flag, otherwise we can set DISCARD_ZEROES flag * because we are going to use punch hole instead of discard * and if it succeed, subsequent read from sparse area returns * zero. */ if (ext2fs_stat(io->name, &st) == 0) { if (S_ISBLK(st.st_mode)) io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE; else io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES; } #ifdef BLKDISCARDZEROES ioctl(data->dev, BLKDISCARDZEROES, &zeroes); if (zeroes) io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES; #endif #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) /* * Some operating systems require that the buffers be aligned, * regardless of O_DIRECT */ if (!io->align) io->align = 512; #endif if ((retval = alloc_cache(io, data))) goto cleanup; #ifdef BLKROGET if (flags & IO_FLAG_RW) { int error; int readonly = 0; /* Is the block device actually writable? */ error = ioctl(data->dev, BLKROGET, &readonly); if (!error && readonly) { close(data->dev); retval = EPERM; goto cleanup; } } #endif #ifdef __linux__ #undef RLIM_INFINITY #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4))) #define RLIM_INFINITY ((unsigned long)(~0UL>>1)) #else #define RLIM_INFINITY (~0UL) #endif /* * Work around a bug in 2.4.10-2.4.18 kernels where writes to * block devices are wrongly getting hit by the filesize * limit. This workaround isn't perfect, since it won't work * if glibc wasn't built against 2.2 header files. (Sigh.) * */ if ((flags & IO_FLAG_RW) && (uname(&ut) == 0) && ((ut.release[0] == '2') && (ut.release[1] == '.') && (ut.release[2] == '4') && (ut.release[3] == '.') && (ut.release[4] == '1') && (ut.release[5] >= '0') && (ut.release[5] < '8')) && (ext2fs_stat(io->name, &st) == 0) && (S_ISBLK(st.st_mode))) { struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY; setrlimit(RLIMIT_FSIZE, &rlim); getrlimit(RLIMIT_FSIZE, &rlim); if (((unsigned long) rlim.rlim_cur) < ((unsigned long) rlim.rlim_max)) { rlim.rlim_cur = rlim.rlim_max; setrlimit(RLIMIT_FSIZE, &rlim); } } #endif *channel = io; return 0; cleanup: if (data) { free_cache(data); ext2fs_free_mem(&data); } if (io) ext2fs_free_mem(&io); return retval; }