]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/drivetype.c
Merge in all the low-risk changes from the Windows branch.
[bacula/bacula] / bacula / src / findlib / drivetype.c
1 /*
2  *  Implement routines to determine drive type (Windows specific).
3  *
4  *   Written by Robert Nelson, June 2006
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2006 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
20    the file LICENSE for additional details.
21
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