1 /*
2    Wrapper for routines to notify the
3    tree about the changes made to the directory
4    structure.
5 
6    Copyright (C) 2011-2021
7    Free Software Foundation, Inc.
8 
9    Author:
10    Janne Kukonlehto
11    Miguel de Icaza
12    Slava Zanko <slavazanko@gmail.com>, 2013
13 
14    This file is part of the Midnight Commander.
15 
16    The Midnight Commander is free software: you can redistribute it
17    and/or modify it under the terms of the GNU General Public License as
18    published by the Free Software Foundation, either version 3 of the License,
19    or (at your option) any later version.
20 
21    The Midnight Commander is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25 
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  */
29 
30 
31 /** \file  filenot.c
32  *  \brief Source: wrapper for routines to notify the
33  *  tree about the changes made to the directory
34  *  structure.
35  */
36 
37 #include <config.h>
38 
39 #include <errno.h>
40 #include <string.h>
41 
42 #include "lib/global.h"
43 #include "lib/fs.h"
44 #include "lib/util.h"
45 #include "lib/vfs/vfs.h"
46 
47 #include "filenot.h"
48 
49 /*** global variables ****************************************************************************/
50 
51 /*** file scope macro definitions ****************************************************************/
52 
53 /*** file scope type declarations ****************************************************************/
54 
55 /*** file scope variables ************************************************************************/
56 
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
59 
60 static vfs_path_t *
get_absolute_name(const vfs_path_t * vpath)61 get_absolute_name (const vfs_path_t * vpath)
62 {
63     if (vpath == NULL)
64         return NULL;
65 
66     if (IS_PATH_SEP (*vfs_path_get_by_index (vpath, 0)->path))
67         return vfs_path_clone (vpath);
68 
69     return vfs_path_append_vpath_new (vfs_get_raw_current_dir (), vpath, NULL);
70 }
71 
72 /* --------------------------------------------------------------------------------------------- */
73 
74 static int
my_mkdir_rec(const vfs_path_t * vpath,mode_t mode)75 my_mkdir_rec (const vfs_path_t * vpath, mode_t mode)
76 {
77     vfs_path_t *q;
78     int result;
79 
80     if (mc_mkdir (vpath, mode) == 0)
81         return 0;
82     if (errno != ENOENT)
83         return (-1);
84 
85     /* FIXME: should check instead if vpath is at the root of that filesystem */
86     if (!vfs_file_is_local (vpath))
87         return (-1);
88 
89     if (strcmp (vfs_path_as_str (vpath), PATH_SEP_STR) == 0)
90     {
91         errno = ENOTDIR;
92         return (-1);
93     }
94 
95     q = vfs_path_append_new (vpath, "..", (char *) NULL);
96     result = my_mkdir_rec (q, mode);
97     vfs_path_free (q, TRUE);
98 
99     if (result == 0)
100         result = mc_mkdir (vpath, mode);
101 
102     return result;
103 }
104 
105 /* --------------------------------------------------------------------------------------------- */
106 /*** public functions ****************************************************************************/
107 /* --------------------------------------------------------------------------------------------- */
108 
109 int
my_mkdir(const vfs_path_t * vpath,mode_t mode)110 my_mkdir (const vfs_path_t * vpath, mode_t mode)
111 {
112     int result;
113 
114     result = my_mkdir_rec (vpath, mode);
115     if (result == 0)
116     {
117         vfs_path_t *my_s;
118 
119         my_s = get_absolute_name (vpath);
120         vfs_path_free (my_s, TRUE);
121     }
122     return result;
123 }
124 
125 /* --------------------------------------------------------------------------------------------- */
126 
127 int
my_rmdir(const char * path)128 my_rmdir (const char *path)
129 {
130     int result;
131     vfs_path_t *vpath;
132 
133     vpath = vfs_path_from_str_flags (path, VPF_NO_CANON);
134     /* FIXME: Should receive a Wtree! */
135     result = mc_rmdir (vpath);
136     if (result == 0)
137     {
138         vfs_path_t *my_s;
139 
140         my_s = get_absolute_name (vpath);
141         vfs_path_free (my_s, TRUE);
142     }
143     vfs_path_free (vpath, TRUE);
144     return result;
145 }
146 
147 /* --------------------------------------------------------------------------------------------- */
148