device.c revision 7e7bd3dccbfe8f79e25e5c1554b5bc3a9aaca321
/*
libparted - a library for manipulating disk partitions
Copyright (C) 1999 - 2001, 2005, 2007 Free Software Foundation, Inc.
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file device.c */
/**
* \addtogroup PedDevice
*
* \brief Device access.
*
* When ped_device_probe_all() is called, libparted attempts to detect all
* devices. It constructs a list which can be accessed with
* ped_device_get_next().
*
* If you want to use a device that isn't on the list, use
* ped_device_get(). Also, there may be OS-specific constructors, for creating
* devices from file descriptors, stores, etc. For example,
* ped_device_new_from_store().
*
* @{
*/
#include <config.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
under section 6.7.8 part 10
#ifndef HAVE_CANONICALIZE_FILE_NAME
char *
canonicalize_file_name (const char *name)
{
char * buf;
int size;
char * result;
#ifdef PATH_MAX
#else
/* Bigger is better; realpath has no way todo bounds checking. */
size = 4096;
#endif
/* Just in case realpath does not NULL terminate the string
* or it just fits in SIZE without a NULL terminator. */
if (! buf) {
return NULL;
}
if (! result)
return result;
}
#else
extern char *canonicalize_file_name (const char *name);
#endif /* !HAVE_CANONICALIZE_FILE_NAME */
static void
{
if (walk)
else
}
static void
{
}
if (last)
else
}
/**
* Returns the next device that was detected by ped_device_probe_all(), or
* calls to ped_device_get_next().
* If dev is NULL, returns the first device.
*
* \return NULL if dev is the last device.
*/
{
if (dev)
else
return devices;
}
void
_ped_device_probe (const char* path)
{
if (!dev)
}
/**
* Attempts to detect all devices.
*/
void
{
}
/**
* Called by ped_done(), so you do not need to worry about it.
*/
void
{
while (devices)
}
/**
* Gets the device "name", where name is usually the block device, e.g.
* an attempt will be made to detect it again. If it is found, it will
* be added to the list.
*/
ped_device_get (const char* path)
{
char* normal_path;
if (!normal_path)
/* Well, maybe it is just that the file does not exist.
* Try it anyway. */
if (!normal_path)
return NULL;
return walk;
}
}
if (!walk)
return NULL;
return walk;
}
/**
* Destroys a device and removes it from the device list, and frees
* all resources associated with the device (all resources allocated
* when the device was created).
*/
void
{
while (dev->open_count) {
if (!ped_device_close (dev))
break;
}
}
void
{
}
int
{
}
/**
* Attempt to open a device to allow use of read, write and sync functions.
*
* The meaning of "open" is architecture-dependent. Apart from requesting
* access to the device from the operating system, it does things like flushing
* caches.
* \note May allocate resources. Any resources allocated here will
* be freed by a final ped_device_close(). (ped_device_open() may be
* called multiple times -- it's a ref-count-like mechanism)
*
* \return zero on failure
*/
int
{
int status;
if (dev->open_count)
else
if (status)
dev->open_count++;
return status;
}
/**
* Close dev.
* If this is the final close, then resources allocated by
* ped_device_open() are freed.
*
* \return zero on failure
*/
int
{
if (--dev->open_count)
else
}
/**
* Begins external access mode. External access mode allows you to
* safely do IO on the device. If a PedDevice is open, then you should
* not do any IO on that device, e.g. by calling an external program
* like e2fsck, unless you put it in external access mode. You should
* not use any libparted commands that do IO to a device, e.g.
* ped_file_system_{open|resize|copy}, ped_disk_{read|write}), while
* a device is in external access mode.
* Also, you should not ped_device_close() a device, while it is
* in external access mode.
* Note: ped_device_begin_external_access_mode() does things like
* tell the kernel to flush its caches.
*
* Close a device while pretending it is still open.
* This is useful for temporarily suspending libparted access to the device
* in order for an external program to access it.
* (Running external programs while the device is open can cause cache
* coherency problems.)
*
* In particular, this function keeps track of dev->open_count, so that
* reference counting isn't screwed up.
*
* \return zero on failure.
*/
int
{
if (dev->open_count)
else
return 1;
}
/**
* \brief Complementary function to ped_device_begin_external_access.
*
* \note does things like tell the kernel to flush the device's cache.
*
* \return zero on failure.
*/
int
{
dev->external_mode = 0;
if (dev->open_count)
else
return 1;
}
/**
* \internal Read count sectors from dev into buffer, beginning with sector
* start.
*
* \return zero on failure.
*/
int
{
}
/**
* \internal Write count sectors from buffer to dev, starting at sector
* start.
*
* \return zero on failure.
*
* \sa PedDevice::sector_size
* \sa PedDevice::phys_sector_size
*/
int
{
}
{
}
/**
* \internal Flushes all write-behind caches that might be holding up
* writes.
* It is slow because it guarantees cache coherency among all relevant caches.
*
* \return zero on failure
*/
int
{
}
/**
* \internal Flushes all write-behind caches that might be holding writes.
* \warning Does NOT ensure cache coherency with other caches.
* If you need cache coherency, use ped_device_sync() instead.
*
* \return zero on failure
*/
int
{
}
/**
* Get a constraint that represents hardware requirements on alignment and
* geometry.
* This is, for example, important for media that have a physical sector
* size that is a multiple of the logical sector size.
*
* \warning This function is experimental for physical sector sizes not equal to
* 2^9.
*/
{
PedConstraint* c = ped_constraint_new (
return c;
}
/** @} */