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