* Replaced one occurence of sprintf() with snprintf().

* Minor cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40224 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2011-01-12 18:42:48 +00:00
parent fd4998dce7
commit b16710b814
2 changed files with 70 additions and 65 deletions

View File

@ -1,9 +1,12 @@
/*
* Copyright 2006-2011, Axel Dörfler, axeld@pinc-software.de.
* Copyright 2003-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include "KDiskDevice.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@ -13,11 +16,11 @@
#include <Drivers.h>
#include "ddm_userland_interface.h"
#include "KDiskDevice.h"
#include "KDiskDeviceUtils.h"
#include "KPath.h"
#include "UserDataWriter.h"
// debugging
//#define DBG(x)
#define DBG(x) x
@ -46,7 +49,7 @@ KDiskDevice::~KDiskDevice()
status_t
KDiskDevice::SetTo(const char *path)
KDiskDevice::SetTo(const char* path)
{
// check initialization and parameter
status_t error = InitCheck();
@ -156,7 +159,7 @@ KDiskDevice::PublishDevice()
return B_OK;
}
// UnpublishDevice
status_t
KDiskDevice::UnpublishDevice()
{
@ -165,7 +168,7 @@ KDiskDevice::UnpublishDevice()
return B_OK;
}
// RepublishDevice
status_t
KDiskDevice::RepublishDevice()
{
@ -174,42 +177,42 @@ KDiskDevice::RepublishDevice()
return B_OK;
}
// SetDeviceFlags
void
KDiskDevice::SetDeviceFlags(uint32 flags)
{
fDeviceData.flags = flags;
}
// DeviceFlags
uint32
KDiskDevice::DeviceFlags() const
{
return fDeviceData.flags;
}
// IsReadOnlyMedia
bool
KDiskDevice::IsReadOnlyMedia() const
{
return fDeviceData.geometry.read_only;
}
// IsWriteOnce
bool
KDiskDevice::IsWriteOnce() const
{
return fDeviceData.geometry.write_once;
}
// IsRemovable
bool
KDiskDevice::IsRemovable() const
{
return fDeviceData.geometry.removable;
}
// HasMedia
bool
KDiskDevice::HasMedia() const
{
@ -254,15 +257,14 @@ KDiskDevice::UpdateGeometry()
}
// SetPath
status_t
KDiskDevice::SetPath(const char *path)
KDiskDevice::SetPath(const char* path)
{
return set_string(fDeviceData.path, path);
}
// Path
const char *
const char*
KDiskDevice::Path() const
{
return fDeviceData.path;
@ -270,7 +272,7 @@ KDiskDevice::Path() const
status_t
KDiskDevice::GetFileName(char *buffer, size_t size) const
KDiskDevice::GetFileName(char* buffer, size_t size) const
{
if (strlcpy(buffer, "raw", size) >= size)
return B_NAME_TOO_LONG;
@ -278,9 +280,8 @@ KDiskDevice::GetFileName(char *buffer, size_t size) const
}
// GetPath
status_t
KDiskDevice::GetPath(KPath *path) const
KDiskDevice::GetPath(KPath* path) const
{
if (!path || path->InitCheck() != B_OK)
return B_BAD_VALUE;
@ -289,51 +290,50 @@ KDiskDevice::GetPath(KPath *path) const
return path->SetPath(fDeviceData.path);
}
// SetFD
void
KDiskDevice::SetFD(int fd)
{
fFD = fd;
}
// FD
int
KDiskDevice::FD() const
{
return fFD;
}
// DeviceData
disk_device_data *
disk_device_data*
KDiskDevice::DeviceData()
{
return &fDeviceData;
}
// DeviceData
const disk_device_data *
const disk_device_data*
KDiskDevice::DeviceData() const
{
return &fDeviceData;
}
// WriteUserData
void
KDiskDevice::WriteUserData(UserDataWriter &writer, user_partition_data *data)
KDiskDevice::WriteUserData(UserDataWriter& writer, user_partition_data* data)
{
return KPartition::WriteUserData(writer, data);
}
// WriteUserData
void
KDiskDevice::WriteUserData(UserDataWriter &writer)
KDiskDevice::WriteUserData(UserDataWriter& writer)
{
KPartition *partition = this;
user_disk_device_data *data
KPartition* partition = this;
user_disk_device_data* data
= writer.AllocateDeviceData(partition->CountChildren());
char *path = writer.PlaceString(Path());
if (data) {
char* path = writer.PlaceString(Path());
if (data != NULL) {
data->device_flags = DeviceFlags();
data->path = path;
writer.AddRelocationEntry(&data->path);
@ -342,7 +342,7 @@ KDiskDevice::WriteUserData(UserDataWriter &writer)
partition->WriteUserData(writer, NULL);
}
// Dump
void
KDiskDevice::Dump(bool deep, int32 level)
{
@ -353,9 +353,9 @@ KDiskDevice::Dump(bool deep, int32 level)
KPartition::Dump(deep, 0);
}
// GetMediaStatus
status_t
KDiskDevice::GetMediaStatus(status_t *mediaStatus)
KDiskDevice::GetMediaStatus(status_t* mediaStatus)
{
status_t error = B_OK;
if (ioctl(fFD, B_GET_MEDIA_STATUS, mediaStatus) != 0)
@ -376,16 +376,16 @@ KDiskDevice::GetMediaStatus(status_t *mediaStatus)
return error;
}
// GetGeometry
status_t
KDiskDevice::GetGeometry(device_geometry *geometry)
KDiskDevice::GetGeometry(device_geometry* geometry)
{
if (ioctl(fFD, B_GET_GEOMETRY, geometry) != 0)
return errno;
return B_OK;
}
// _InitPartitionData
void
KDiskDevice::_InitPartitionData()
{
@ -429,4 +429,3 @@ KDiskDevice::_UpdateDeviceFlags()
if (fDeviceData.geometry.write_once)
SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_WRITE_ONCE);
}

View File

@ -1,16 +1,21 @@
// KFileDiskDevice.cpp
/*
* Copyright 2003-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include <KFileDiskDevice.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <KernelExport.h>
#include <Drivers.h>
#include <devfs.h>
#include <Drivers.h>
#include <KernelExport.h>
#include <KDiskDeviceUtils.h>
#include <KFileDiskDevice.h>
#include <KPath.h>
@ -19,26 +24,27 @@
#define DBG(x) x
#define OUT dprintf
static const char *kFileDevicesDir = "/dev/disk/virtual/files";
static const char* kFileDevicesDir = "/dev/disk/virtual/files";
// constructor
KFileDiskDevice::KFileDiskDevice(partition_id id)
: KDiskDevice(id),
fFilePath(NULL)
:
KDiskDevice(id),
fFilePath(NULL)
{
SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_IS_FILE);
}
// destructor
KFileDiskDevice::~KFileDiskDevice()
{
Unset();
}
// SetTo
status_t
KFileDiskDevice::SetTo(const char *filePath, const char *devicePath)
KFileDiskDevice::SetTo(const char* filePath, const char* devicePath)
{
// check params
if (!filePath || strlen(filePath) > B_PATH_NAME_LENGTH
@ -60,7 +66,7 @@ KFileDiskDevice::SetTo(const char *filePath, const char *devicePath)
return B_BAD_VALUE;
// create the device, if requested
KPath tmpDevicePath;
if (!devicePath) {
if (devicePath == NULL) {
// no device path: we shall create a new device entry
if (tmpDevicePath.InitCheck() != B_OK)
return tmpDevicePath.InitCheck();
@ -101,7 +107,7 @@ KFileDiskDevice::SetTo(const char *filePath, const char *devicePath)
return B_OK;
}
// Unset
void
KFileDiskDevice::Unset()
{
@ -118,23 +124,23 @@ KFileDiskDevice::Unset()
fFilePath = NULL;
}
// InitCheck
status_t
KFileDiskDevice::InitCheck() const
{
return KDiskDevice::InitCheck();
}
// FilePath
const char *
const char*
KFileDiskDevice::FilePath() const
{
return fFilePath;
}
// GetMediaStatus
status_t
KFileDiskDevice::GetMediaStatus(status_t *mediaStatus)
KFileDiskDevice::GetMediaStatus(status_t* mediaStatus)
{
// check the file
struct stat st;
@ -145,9 +151,9 @@ KFileDiskDevice::GetMediaStatus(status_t *mediaStatus)
return B_OK;
}
// GetGeometry
status_t
KFileDiskDevice::GetGeometry(device_geometry *geometry)
KFileDiskDevice::GetGeometry(device_geometry* geometry)
{
// check the file
struct stat st;
@ -177,25 +183,25 @@ KFileDiskDevice::GetGeometry(device_geometry *geometry)
return B_OK;
}
// _RegisterDevice
status_t
KFileDiskDevice::_RegisterDevice(const char *file, const char *device)
KFileDiskDevice::_RegisterDevice(const char* file, const char* device)
{
return devfs_publish_file_device(device + 5, file);
// we need to remove the "/dev/" part from the path
}
// _UnregisterDevice
status_t
KFileDiskDevice::_UnregisterDevice(const char *_device)
KFileDiskDevice::_UnregisterDevice(const char* _device)
{
return devfs_unpublish_file_device(_device + 5);
// we need to remove the "/dev/" part from the path
}
// _GetDirectoryPath
status_t
KFileDiskDevice::_GetDirectoryPath(partition_id id, KPath *path)
KFileDiskDevice::_GetDirectoryPath(partition_id id, KPath* path)
{
if (path == NULL)
return B_BAD_VALUE;
@ -206,7 +212,7 @@ KFileDiskDevice::_GetDirectoryPath(partition_id id, KPath *path)
status_t error = path->SetPath(kFileDevicesDir);
if (error == B_OK) {
char idBuffer[12];
sprintf(idBuffer, "%ld", id);
snprintf(idBuffer, sizeof(idBuffer), "%ld", id);
error = path->Append(idBuffer);
}
return error;