]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/drivetype.c
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / findlib / drivetype.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2006-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *  Implement routines to determine drive type (Windows specific).
22  *
23  *   Written by Robert Nelson, June 2006
24  *
25  *   Version $Id$
26  */
27
28 #ifndef TEST_PROGRAM
29
30 #include "bacula.h"
31 #include "find.h"
32
33 #else /* Set up for testing a stand alone program */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #define SUPPORTEDOSES \
39    "HAVE_WIN32\n"
40 #define false              0
41 #define true               1
42 #define bstrncpy           strncpy
43 #define Dmsg0(n,s)         fprintf(stderr, s)
44 #define Dmsg1(n,s,a1)      fprintf(stderr, s, a1)
45 #define Dmsg2(n,s,a1,a2)   fprintf(stderr, s, a1, a2)
46 #endif
47
48 /*
49  * These functions should be implemented for each OS
50  *
51  *       bool drivetype(const char *fname, char *dt, int dtlen);
52  */
53
54 #if defined (HAVE_WIN32)
55 /* Windows */
56
57 bool drivetype(const char *fname, char *dt, int dtlen)
58 {
59    CHAR rootpath[4];
60    UINT type;
61
62    /* Copy Drive Letter, colon, and backslash to rootpath */
63    bstrncpy(rootpath, fname, 3);
64    rootpath[3] = '\0';
65
66    type = GetDriveType(rootpath);
67
68    switch (type) {
69    case DRIVE_REMOVABLE:   bstrncpy(dt, "removable", dtlen);   return true;
70    case DRIVE_FIXED:       bstrncpy(dt, "fixed", dtlen);       return true;
71    case DRIVE_REMOTE:      bstrncpy(dt, "remote", dtlen);      return true;
72    case DRIVE_CDROM:       bstrncpy(dt, "cdrom", dtlen);       return true;
73    case DRIVE_RAMDISK:     bstrncpy(dt, "ramdisk", dtlen);     return true;
74    case DRIVE_UNKNOWN:
75    case DRIVE_NO_ROOT_DIR:
76    default:
77       return false;
78    }
79 }
80 /* Windows */
81
82 #else    /* No recognised OS */
83
84 bool drivetype(const char *fname, char *dt, int dtlen)
85 {
86    Dmsg0(10, "!!! drivetype() not implemented for this OS. !!!\n");
87 #ifdef TEST_PROGRAM
88    Dmsg1(10, "Please define one of the following when compiling:\n\n%s\n",
89          SUPPORTEDOSES);
90    exit(EXIT_FAILURE);
91 #endif
92
93    return false;
94 }
95 #endif
96
97 #ifdef TEST_PROGRAM
98 int main(int argc, char **argv)
99 {
100    char *p;
101    char dt[1000];
102    int status = 0;
103
104    if (argc < 2) {
105       p = (argc < 1) ? "drivetype" : argv[0];
106       printf("usage:\t%s path ...\n"
107             "\t%s prints the drive type and pathname of the paths.\n",
108             p, p);
109       return EXIT_FAILURE;
110    }
111    while (*++argv) {
112       if (!drivetype(*argv, dt, sizeof(dt))) {
113          status = EXIT_FAILURE;
114       } else {
115          printf("%s\t%s\n", dt, *argv);
116       }
117    }
118    return status;
119 }
120 #endif