summaryrefslogtreecommitdiff
path: root/src/e_mod_mount.c
blob: 178ed5f14b059af049c96724f7df7ad82d60d3f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_MOUNT

#include <e.h>
#include "e_mod_main.h"
#include "e_mod_places.h"



#define FSTAB_FILE "/etc/fstab"
#define MTAB_FILE "/proc/mounts"
#define MOUNT_TAG "places_mount"
#define UMOUNT_TAG "places_umount"

static Ecore_Timer *mtab_timer = NULL;
static Eina_List *know_mounts = NULL;
static Ecore_Event_Handler *eeh1 = NULL;
static Ecore_Event_Handler *eeh2 = NULL;


static Eina_Bool
_places_mount_exe_del_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
   Ecore_Exe_Event_Del *e = (Ecore_Exe_Event_Del *)event;
   const char *tag = ecore_exe_tag_get(e->exe);

   if (!tag || (strcmp(tag, MOUNT_TAG) && strcmp(tag, UMOUNT_TAG)))
      return ECORE_CALLBACK_PASS_ON;

   Volume *vol = ecore_exe_data_get(e->exe);
   if (vol && (e->exit_code == 0))
   {
      vol->mounted = strcmp(tag, MOUNT_TAG) ? EINA_FALSE : EINA_TRUE;
      places_volume_update(vol);
   }
   return ECORE_CALLBACK_DONE;
}

static Eina_Bool
_places_mount_exe_error_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
   Ecore_Exe_Event_Data *e = (Ecore_Exe_Event_Data *)event;
   const char *tag = ecore_exe_tag_get(e->exe);

   if (!tag || (strcmp(tag, MOUNT_TAG) && strcmp(tag, UMOUNT_TAG)))
      return ECORE_CALLBACK_PASS_ON;

   Ecore_Exe_Event_Data_Line line = e->lines[0];
   e_util_dialog_internal(D_("Operation failed"),
                          line.size ? line.line : D_("Unknown error"));

   return ECORE_CALLBACK_DONE;
}

static void
_places_mount_mount_func(Volume *vol, Eina_List *opts)
{
   char cmd[4096];
   Ecore_Exe *exe;

   snprintf(cmd, sizeof(cmd), "mount %s", vol->mount_point);
   exe = ecore_exe_pipe_run(cmd, ECORE_EXE_PIPE_ERROR_LINE_BUFFERED |
                                 ECORE_EXE_PIPE_ERROR |
                                 ECORE_EXE_TERM_WITH_PARENT, vol);
   ecore_exe_tag_set(exe, MOUNT_TAG);
}

static void
_places_mount_unmount_func(Volume *vol, Eina_List *opts)
{
   char cmd[4096];
   Ecore_Exe *exe;

   snprintf(cmd, sizeof(cmd), "umount %s", vol->mount_point);
   exe = ecore_exe_pipe_run(cmd, ECORE_EXE_PIPE_ERROR_LINE_BUFFERED |
                                 ECORE_EXE_PIPE_ERROR |
                                 ECORE_EXE_TERM_WITH_PARENT, vol);
   ecore_exe_tag_set(exe, UMOUNT_TAG);
}

static void
_places_mount_free_func(Volume *vol)
{
   know_mounts = eina_list_remove(know_mounts, vol);
}

void
_places_mount_volume_add(const char *mpoint, const char *fstype)
{
   Volume *vol;

   // printf("PLACES: found mount: %s (%s)\n", mpoint, fstype);
   vol = places_volume_add(mpoint, EINA_TRUE);
   vol->valid = EINA_TRUE;
   vol->mounted = EINA_FALSE;
   eina_stringshare_replace(&vol->label,  ecore_file_file_get(mpoint));
   eina_stringshare_replace(&vol->mount_point, mpoint);
   eina_stringshare_replace(&vol->fstype, fstype);

   vol->mount_func = _places_mount_mount_func;
   vol->unmount_func = _places_mount_unmount_func;
   vol->free_func = _places_mount_free_func;

   know_mounts = eina_list_append(know_mounts, vol);
}

char *
_places_mount_file_read(const char *path)
{
   char buf[10000]; //   :/
   FILE * f;
   size_t len;
   
   f = fopen(path, "r");
   if (!f)
      return NULL;

   len = fread(buf, sizeof(char), sizeof(buf), f);
   fclose(f);

   if (len < 1)
      return NULL;

   // printf("PLACES: readed %ld\n", len);
   buf[++len] = '\0';
   return strdup(buf);
}

void
_places_mount_mtab_parse(void)
{
   char *contents;
   char **lines;
   unsigned int i, num_splits;
   char mpoint[256];
   Eina_List *to_search, *l, *l2;
   Volume *vol;

   contents = _places_mount_file_read("/proc/mounts");
   if (!contents)
      return;

   lines = eina_str_split(contents, "\n", 0);
   if (!lines)
      return;

   to_search = eina_list_clone(know_mounts);

   for (i = 0; lines[i]; i++)
   {
      num_splits = sscanf(lines[i], "%*s %s %*s %*s %*d %*d", mpoint);
      if (num_splits != 1)
         continue;

      EINA_LIST_FOREACH_SAFE(to_search, l, l2, vol)
      {
         if (strcmp(vol->mount_point, mpoint))
           continue;

         // printf("PLACES: Mounted: %s\n", mpoint);
         to_search = eina_list_remove(to_search, vol);
         if (vol->mounted == EINA_FALSE)
         {
            vol->mounted = EINA_TRUE;
            places_volume_update(vol);
         }
         break;
      }
      continue;
   }

   EINA_LIST_FREE(to_search, vol)
   {
      // printf("PLACES: NOT Mounted: %s\n", vol->mount_point);
      if (vol->mounted == EINA_TRUE)
      {
         vol->mounted = EINA_FALSE;
         places_volume_update(vol);
      }
   }

   free(lines[0]);
   free(lines);
   free(contents);
}

void
_places_mount_fstab_parse(void)
{
   char *contents, *line;
   char **lines;
   unsigned int i, num_splits;
   char mpoint[256];
   char fstype[64];

   contents = _places_mount_file_read(FSTAB_FILE);
   if (!contents)
      return;

   lines = eina_str_split(contents, "\n", 0);
   if (!lines)
      return;

   for (i = 0; lines[i]; i++)
   {
      line = lines[i];

      if (!line[0] || line[0] == '\n' || line[0] == '#')
         continue;

      num_splits = sscanf(line, "%*s %s %s %*s %*d %*d", mpoint, fstype);
      if (num_splits != 2)
         continue;

      if (!strcmp(fstype, "nfs") ||
          !strcmp(fstype, "cifs"))
      {
         _places_mount_volume_add(mpoint, fstype);
      }
   }

   free(lines[0]);
   free(lines);
   free(contents);
}

static Eina_Bool
_places_mount_mtab_timer_cb(void *data)
{
   _places_mount_mtab_parse();
   return ECORE_CALLBACK_RENEW;
}

Eina_Bool
places_mount_init(void)
{
   printf("PLACES: mount: init()\n");

   if (!ecore_file_exists(FSTAB_FILE) ||
       !ecore_file_exists(MTAB_FILE))
   {
      printf("PLACES: mount: Cannot find required files\n");
      return EINA_FALSE;
   }

   _places_mount_fstab_parse();
   _places_mount_mtab_parse();

   eeh1 = ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
                                  _places_mount_exe_del_cb, NULL);
   eeh2 = ecore_event_handler_add(ECORE_EXE_EVENT_ERROR,
                                  _places_mount_exe_error_cb, NULL);
   mtab_timer = ecore_timer_add(4.0, _places_mount_mtab_timer_cb, NULL);

   return EINA_TRUE;
}

void
places_mount_shutdown(void)
{
   printf("PLACES: mtab: shutdown()\n");
   E_FREE_FUNC(mtab_timer, ecore_timer_del);
   E_FREE_FUNC(know_mounts, eina_list_free);
   E_FREE_FUNC(eeh1, ecore_event_handler_del);
   E_FREE_FUNC(eeh2, ecore_event_handler_del);
}


#endif