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