return lba_is_phy_aligned(cxt, lba);
}
-static unsigned long get_sector_size(int fd)
+static unsigned long get_sector_size(struct fdisk_context *cxt)
{
int sect_sz;
- if (!blkdev_get_sector_size(fd, §_sz))
+ if (!fdisk_is_regfile(cxt) &&
+ !blkdev_get_sector_size(cxt->dev_fd, §_sz))
return (unsigned long) sect_sz;
+
return DEFAULT_SECTOR_SIZE;
}
int fdisk_discover_geometry(struct fdisk_context *cxt)
{
fdisk_sector_t nsects;
+ unsigned int h = 0, s = 0;
assert(cxt);
assert(cxt->geom.heads == 0);
DBG(CXT, ul_debugobj(cxt, "%s: discovering geometry...", cxt->dev_path));
- /* get number of 512-byte sectors, and convert it the real sectors */
- if (!blkdev_get_sectors(cxt->dev_fd, (unsigned long long *) &nsects))
- cxt->total_sectors = (nsects / (cxt->sector_size >> 9));
+ if (fdisk_is_regfile(cxt))
+ cxt->total_sectors = cxt->dev_st.st_size / cxt->sector_size;
+ else {
+ /* get number of 512-byte sectors, and convert it the real sectors */
+ if (!blkdev_get_sectors(cxt->dev_fd, (unsigned long long *) &nsects))
+ cxt->total_sectors = (nsects / (cxt->sector_size >> 9));
+
+ /* what the kernel/bios thinks the geometry is */
+ blkdev_get_geometry(cxt->dev_fd, &h, &s);
+ }
DBG(CXT, ul_debugobj(cxt, "total sectors: %ju (ioctl=%ju)",
(uintmax_t) cxt->total_sectors,
(uintmax_t) nsects));
- /* what the kernel/bios thinks the geometry is */
- blkdev_get_geometry(cxt->dev_fd, &cxt->geom.heads, (unsigned int *) &cxt->geom.sectors);
+ cxt->geom.cylinders = 0;
+ cxt->geom.heads = h;
+ cxt->geom.sectors = s;
/* obtained heads and sectors */
recount_geometry(cxt);
blkid_free_probe(pr);
#endif
- cxt->sector_size = get_sector_size(cxt->dev_fd);
+ cxt->sector_size = get_sector_size(cxt);
if (!cxt->phy_sector_size) /* could not discover physical size */
cxt->phy_sector_size = cxt->sector_size;
free(cxt->collision);
cxt->collision = NULL;
+ memset(&cxt->dev_st, 0, sizeof(cxt->dev_st));
+
cxt->dev_fd = -1;
cxt->firstsector = NULL;
cxt->firstsector_bufsz = 0;
int rc, org = fdisk_is_listonly(cxt->parent);
/* assign_device() is sensitive to "listonly" mode, so let's
- * follow the current context setting for the parent to avoid
+ * follow the current context setting for the parent to avoid
* unwanted extra warnings. */
fdisk_enable_listonly(cxt->parent, fdisk_is_listonly(cxt));
if (fd < 0)
return -errno;
+ fstat(fd, &cxt->dev_st);
+
cxt->readonly = readonly;
cxt->dev_fd = fd;
cxt->dev_path = strdup(fname);
return cxt->readonly;
}
+/**
+ * fdisk_is_regfile:
+ * @cxt: context
+ *
+ * Returns: 1 if open file descriptor is regular file rather than a block device.
+ */
+int fdisk_is_regfile(struct fdisk_context *cxt)
+{
+ assert(cxt);
+ return S_ISREG(cxt->dev_st.st_mode);
+}
+
/**
* fdisk_unref_context:
* @cxt: fdisk context
struct fdisk_context {
int dev_fd; /* device descriptor */
char *dev_path; /* device path */
+ struct stat dev_st; /* stat(2) result */
+
int refcount;
unsigned char *firstsector; /* buffer with master boot record */
unsigned long firstsector_bufsz;
+
/* topology */
unsigned long io_size; /* I/O size used by fdisk */
unsigned long optimal_io_size; /* optional I/O returned by device */
struct fdisk_script *script; /* what we want to follow */
};
+
/* context.c */
extern int __fdisk_switch_label(struct fdisk_context *cxt,
struct fdisk_label *lb);