2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / normal / autofs.c
blob39f2f9ddccc40c6aaae771903fd344168ff6adad
1 /* autofs.c - support auto-loading from fs.lst */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
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 3 of the License, or
9 * (at your option) any later version.
11 * GRUB 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.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/mm.h>
21 #include <grub/dl.h>
22 #include <grub/env.h>
23 #include <grub/misc.h>
24 #include <grub/fs.h>
25 #include <grub/normal.h>
27 /* This is used to store the names of filesystem modules for auto-loading. */
28 struct grub_fs_module_list
30 char *name;
31 struct grub_fs_module_list *next;
33 typedef struct grub_fs_module_list *grub_fs_module_list_t;
35 static grub_fs_module_list_t fs_module_list = 0;
37 /* The auto-loading hook for filesystems. */
38 static int
39 autoload_fs_module (void)
41 grub_fs_module_list_t p;
43 while ((p = fs_module_list) != 0)
45 if (! grub_dl_get (p->name) && grub_dl_load (p->name))
46 return 1;
48 fs_module_list = p->next;
49 grub_free (p->name);
50 grub_free (p);
53 return 0;
56 /* Read the file fs.lst for auto-loading. */
57 void
58 read_fs_list (void)
60 const char *prefix;
61 static int first_time = 1;
63 /* Make sure that this function does not get executed twice. */
64 if (! first_time)
65 return;
66 first_time = 0;
68 prefix = grub_env_get ("prefix");
69 if (prefix)
71 char *filename;
73 filename = grub_malloc (grub_strlen (prefix) + sizeof ("/fs.lst"));
74 if (filename)
76 grub_file_t file;
78 grub_sprintf (filename, "%s/fs.lst", prefix);
79 file = grub_file_open (filename);
80 if (file)
82 while (1)
84 char *buf;
85 char *p;
86 char *q;
87 grub_fs_module_list_t fs_mod;
89 buf = grub_file_getline (file);
90 if (! buf)
91 break;
93 p = buf;
94 q = buf + grub_strlen (buf) - 1;
96 /* Ignore space. */
97 while (grub_isspace (*p))
98 p++;
100 while (p < q && grub_isspace (*q))
101 *q-- = '\0';
103 /* If the line is empty, skip it. */
104 if (p >= q)
105 continue;
107 fs_mod = grub_malloc (sizeof (*fs_mod));
108 if (! fs_mod)
109 continue;
111 fs_mod->name = grub_strdup (p);
112 if (! fs_mod->name)
114 grub_free (fs_mod);
115 continue;
118 fs_mod->next = fs_module_list;
119 fs_module_list = fs_mod;
122 grub_file_close (file);
125 grub_free (filename);
129 /* Ignore errors. */
130 grub_errno = GRUB_ERR_NONE;
132 /* Set the hook. */
133 grub_fs_autoload_hook = autoload_fs_module;