~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to commands/search.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* search.c - search devices based on a file or a filesystem label */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2005  Free Software Foundation, Inc.
 
5
 *
 
6
 *  GRUB is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  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 GRUB; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
 
19
 */
 
20
 
 
21
#include <grub/types.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/err.h>
 
25
#include <grub/dl.h>
 
26
#include <grub/normal.h>
 
27
#include <grub/arg.h>
 
28
#include <grub/device.h>
 
29
#include <grub/file.h>
 
30
#include <grub/env.h>
 
31
 
 
32
static const struct grub_arg_option options[] =
 
33
  {
 
34
    {"file", 'f', 0, "search devices by a file (default)", 0, 0},
 
35
    {"label", 'l', 0, "search devices by a filesystem label", 0, 0},
 
36
    {"set", 's', GRUB_ARG_OPTION_OPTIONAL, "set a variable to the first device found", "VAR", ARG_TYPE_STRING},
 
37
    {0, 0, 0, 0, 0, 0}
 
38
  };
 
39
 
 
40
static void
 
41
search_label (const char *key, const char *var)
 
42
{
 
43
  int count = 0;
 
44
  auto int iterate_device (const char *name);
 
45
 
 
46
  int iterate_device (const char *name)
 
47
    {
 
48
      grub_device_t dev;
 
49
      
 
50
      dev = grub_device_open (name);
 
51
      if (dev)
 
52
        {
 
53
          grub_fs_t fs;
 
54
          
 
55
          fs = grub_fs_probe (dev);
 
56
          if (fs && fs->label)
 
57
            {
 
58
              char *label;
 
59
              
 
60
              (fs->label) (dev, &label);
 
61
              if (grub_errno == GRUB_ERR_NONE && label)
 
62
                {
 
63
                  if (grub_strcmp (label, key) == 0)
 
64
                    {
 
65
                      /* Found!  */
 
66
                      grub_printf (" %s", name);
 
67
                      if (count++ == 0 && var)
 
68
                        grub_env_set (var, name);
 
69
                    }
 
70
                  
 
71
                  grub_free (label);
 
72
                }
 
73
            }
 
74
          
 
75
          grub_device_close (dev);
 
76
        }
 
77
 
 
78
      grub_errno = GRUB_ERR_NONE;
 
79
      return 0;
 
80
    }
 
81
  
 
82
  grub_device_iterate (iterate_device);
 
83
  
 
84
  if (count == 0)
 
85
    grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
 
86
}
 
87
 
 
88
static void
 
89
search_file (const char *key, const char *var)
 
90
{
 
91
  int count = 0;
 
92
  char *buf = 0;
 
93
  auto int iterate_device (const char *name);
 
94
 
 
95
  int iterate_device (const char *name)
 
96
    {
 
97
      grub_size_t len;
 
98
      char *p;
 
99
      grub_file_t file;
 
100
      
 
101
      len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
 
102
      p = grub_realloc (buf, len);
 
103
      if (! p)
 
104
        return 1;
 
105
 
 
106
      buf = p;
 
107
      grub_sprintf (buf, "(%s)%s", name, key);
 
108
      
 
109
      file = grub_file_open (buf);
 
110
      if (file)
 
111
        {
 
112
          /* Found!  */
 
113
          grub_printf (" %s", name);
 
114
          if (count++ == 0 && var)
 
115
            grub_env_set (var, name);
 
116
 
 
117
          grub_file_close (file);
 
118
        }
 
119
      
 
120
      grub_errno = GRUB_ERR_NONE;
 
121
      return 0;
 
122
    }
 
123
  
 
124
  grub_device_iterate (iterate_device);
 
125
  
 
126
  grub_free (buf);
 
127
  
 
128
  if (grub_errno == GRUB_ERR_NONE && count == 0)
 
129
    grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device");
 
130
}
 
131
 
 
132
static grub_err_t
 
133
grub_cmd_search (struct grub_arg_list *state, int argc, char **args)
 
134
{
 
135
  const char *var = 0;
 
136
  
 
137
  if (argc == 0)
 
138
    return grub_error (GRUB_ERR_INVALID_COMMAND, "no argument specified");
 
139
 
 
140
  if (state[2].set)
 
141
    var = state[2].arg ? : "root";
 
142
  
 
143
  if (state[1].set)
 
144
    search_label (args[0], var);
 
145
  else
 
146
    search_file (args[0], var);
 
147
 
 
148
  return grub_errno;
 
149
}
 
150
 
 
151
GRUB_MOD_INIT(search)
 
152
{
 
153
  (void) mod;                   /* To stop warning. */
 
154
  grub_register_command ("search", grub_cmd_search, GRUB_COMMAND_FLAG_BOTH,
 
155
                         "search [-f|-l|-s] NAME",
 
156
                         "Search devices by a file or a filesystem label."
 
157
                         " If --set is specified, the first device found is"
 
158
                         " set to a variable. If no variable name is"
 
159
                         " specified, \"root\" is used.",
 
160
                         options);
 
161
}
 
162
 
 
163
GRUB_MOD_FINI(search)
 
164
{
 
165
  grub_unregister_command ("search");
 
166
}