http://linux-ntfs.sourceforge.net/snapshots/ntfsprogs-200309071734.tar.bz2
[ntfsprogs.git] / libntfs / device.c
1 /*
2  * device.c - Low level device io functions. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2003 Anton Altaparmakov
5  *
6  * This program/include file is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program/include file is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (in the main directory of the Linux-NTFS
18  * distribution in the file COPYING); if not, write to the Free Software
19  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "device.h"
29
30 /**
31  * ntfs_device_alloc - allocate an ntfs device structure and pre-initialize it
32  * name:        name of the device (must be present)
33  * state:       initial device state (usually zero)
34  * dops:        ntfs device operations to use with the device (must be present)
35  * private:     pointer to private data (optional)
36  *
37  * Allocate an ntfs device structure and pre-initialize it with the user-
38  * specified device operations @dops, device state @state, device name @name,
39  * and optional private data @private.
40  *
41  * Note, @name is copied and can hence be freed after this functions returns.
42  *
43  * On success return a pointer to the allocated ntfs device structure and on
44  * error return NULL with errno set to the error code returned by malloc().
45  */
46 struct ntfs_device *ntfs_device_alloc(const char *name, const long state,
47                 struct ntfs_device_operations *dops, void *private)
48 {
49         struct ntfs_device *dev;
50
51         if (!name) {
52                 errno = EINVAL;
53                 return NULL;
54         }
55
56         dev = (struct ntfs_device *)malloc(sizeof(struct ntfs_device));
57         if (dev) {
58                 if (!(dev->d_name = strdup(name))) {
59                         int eo = errno;
60                         free(dev);
61                         errno = eo;
62                         return NULL;
63                 }
64                 dev->d_ops = dops;
65                 dev->d_state = state;
66                 dev->d_private = private;
67         }
68         return dev;
69 }
70
71 /**
72  * ntfs_device_free - free an ntfs device structure
73  * @dev:        ntfs device structure to free
74  *
75  * Free the ntfs device structure @dev.
76  *
77  * Return 0 on success or -1 on error with errno set to the error code. The
78  * following error codes are defined:
79  *      EINVAL          Invalid pointer @dev.
80  *      EBUSY           Device is still open. Close it before freeing it!
81  */
82 int ntfs_device_free(struct ntfs_device *dev)
83 {
84         if (!dev) {
85                 errno = EINVAL;
86                 return -1;
87         }
88         if (NDevOpen(dev)) {
89                 errno = EBUSY;
90                 return -1;
91         }
92         if (dev->d_name)
93                 free(dev->d_name);
94         free(dev);
95         return 0;
96 }
97