XRootD
XrdOssUnlink.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O s s U n l i n k . c c */
4 /* */
5 /* (c) 2003 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <unistd.h>
32 #include <cerrno>
33 #include <strings.h>
34 #include <limits.h>
35 #include <cstdio>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 
40 #include "XrdSys/XrdSysHeaders.hh"
41 #include "XrdSys/XrdSysPlatform.hh"
42 #include "XrdOss/XrdOssApi.hh"
43 #include "XrdOss/XrdOssCache.hh"
44 #include "XrdOss/XrdOssConfig.hh"
45 #include "XrdOss/XrdOssError.hh"
46 #include "XrdOss/XrdOssOpaque.hh"
47 #include "XrdOss/XrdOssPath.hh"
48 #include "XrdOss/XrdOssTrace.hh"
49 
50 /******************************************************************************/
51 /* G l o b a l E r r o r R o u t i n g O b j e c t */
52 /******************************************************************************/
53 
54 extern XrdSysError OssEroute;
55 
56 extern XrdSysTrace OssTrace;
57 
58 /******************************************************************************/
59 /* R e m d i r */
60 /******************************************************************************/
61 
62 /*
63  Function: Delete a directory from the namespace.
64 
65  Input: path - Is the fully qualified name of the dir to be removed.
66  envP - Environmental information.
67 
68  Output: Returns XrdOssOK upon success and -errno upon failure.
69 */
70 int XrdOssSys::Remdir(const char *path, int Opts, XrdOucEnv *eP)
71 {
72  unsigned long long opts;
73  int retc;
74  struct stat statbuff;
75  char local_path[MAXPATHLEN+1+8];
76 
77 // Build the right local and remote paths.
78 //
79  if (Opts & XRDOSS_isPFN) strcpy(local_path, path);
80  else {retc = Check_RO(Unlink, opts, path, "remove");
81  if ( (retc = GenLocalPath( path, local_path))) return retc;
82  }
83 
84 // Check if this path is really a directory
85 //
86  if (lstat(local_path, &statbuff)) return (errno == ENOENT ? 0 : -errno);
87  if ((statbuff.st_mode & S_IFMT) != S_IFDIR) return -ENOTDIR;
88 
89 // Complete by calling Unlink()
90 //
91  return Unlink(path, Opts);
92 }
93 
94 /******************************************************************************/
95 /* U n l i n k */
96 /******************************************************************************/
97 
98 /*
99  Function: Delete a file from the namespace and release it's data storage.
100 
101  Input: path - Is the fully qualified name of the file to be removed.
102  envP - Environmental information.
103 
104  Output: Returns XrdOssOK upon success and -errno upon failure.
105 */
106 int XrdOssSys::Unlink(const char *path, int Opts, XrdOucEnv *eP)
107 {
108  EPNAME("Unlink")
109  unsigned long long dummy, remotefs;
110  int i, retc2, doAdjust = 0, retc = XrdOssOK;
111  struct stat statbuff;
112  //char *fnp;
113  char local_path[MAXPATHLEN+1+8];
114  char remote_path[MAXPATHLEN+1];
115 
116 // Build the right local and remote paths.
117 //
118  if (Opts & XRDOSS_isPFN)
119  {strcpy(local_path, path),
120  *remote_path = '\0';
121  remotefs = 0;
122  } else {
123  remotefs = Check_RO(Unlink, dummy, path, "remove");
124  if ( (retc = GenLocalPath( path, local_path))
125  || (retc = GenRemotePath(path, remote_path)) ) return retc;
126  }
127 
128 // Check if this path is really a directory of a symbolic link elsewhere
129 //
130  if (lstat(local_path, &statbuff)) retc = (errno == ENOENT ? 0 : -errno);
131  else if ((statbuff.st_mode & S_IFMT) == S_IFLNK)
132  retc = BreakLink(local_path, statbuff);
133  else if ((statbuff.st_mode & S_IFMT) == S_IFDIR)
134  {i = strlen(local_path);
135  if (local_path[i-1] != '/') strcpy(local_path+i, "/");
136  if ((retc = rmdir(local_path))) retc = -errno;
137  DEBUG("dir rc=" <<retc <<" path=" <<local_path);
138  return retc;
139  } else doAdjust = 1;
140 
141 // Delete the local copy and adjust usage
142 //
143  if (!retc)
144  {if (unlink(local_path)) retc = -errno;
145  else {i = strlen(local_path); //fnp = &local_path[i];
146  if (doAdjust && statbuff.st_size)
147  XrdOssCache::Adjust(statbuff.st_dev, -statbuff.st_size);
148  }
149  DEBUG("lcl rc=" <<retc <<" path=" <<local_path);
150  }
151 
152 // If local copy effectively deleted. delete the remote copy if need be
153 //
154  if (remotefs && !(Opts & XRDOSS_Online)
155  && (!retc || retc == -ENOENT) && RSSCmd)
156  {if ((retc2 = MSS_Unlink(remote_path)) != -ENOENT) retc = retc2;
157  DEBUG("rmt rc=" <<retc2 <<" path=" <<remote_path);
158  }
159 
160 // All done
161 //
162  return retc;
163 }
164 
165 /******************************************************************************/
166 /* P r i v a t e M e t h o d s */
167 /******************************************************************************/
168 /******************************************************************************/
169 /* B r e a k L i n k */
170 /******************************************************************************/
171 
172 int XrdOssSys::BreakLink(const char *local_path, struct stat &statbuff)
173 {
174  EPNAME("BreakLink")
175  char *lP, lnkbuff[MAXPATHLEN+64];
176  int lnklen, retc = 0;
177 
178 // Read the contents of the link
179 //
180  if ((lnklen = readlink(local_path, lnkbuff, sizeof(lnkbuff)-1)) < 0)
181  return -errno;
182 
183 // Return the actual stat information on the target (which might not exist
184 //
185  lnkbuff[lnklen] = '\0';
186  if (stat(lnkbuff, &statbuff)) statbuff.st_size = 0;
187  else if (unlink(lnkbuff) && errno != ENOENT)
188  {retc = -errno;
189  OssEroute.Emsg("BreakLink",retc,"unlink symlink target",lnkbuff);
190  } else {DEBUG("broke link " <<local_path <<"->" <<lnkbuff);}
191 
192 // If this is a new-style cache, then we must also remove the pfn file.
193 // In any case, return the appropriate cache group.
194 //
195  lP = lnkbuff+lnklen-1;
196  if (*lP == XrdOssPath::xChar)
197  {if (statbuff.st_size)
199  XrdOssCache::Adjust(lnkbuff, -statbuff.st_size);
200  }
201  } else if (statbuff.st_size)
202  XrdOssCache::Adjust(statbuff.st_dev, -statbuff.st_size);
203 
204 // All done
205 //
206  return retc;
207 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56
#define Check_RO(act, flags, path, opname)
Definition: XrdOssApi.hh:387
#define XRDOSS_isPFN
Definition: XrdOss.hh:469
#define XRDOSS_Online
Definition: XrdOss.hh:468
#define XrdOssOK
Definition: XrdOss.hh:50
int stat(const char *path, struct stat *buf)
int lstat(const char *path, struct stat *buf)
int unlink(const char *path)
int rmdir(const char *path)
struct myOpts opts
static void Adjust(dev_t devid, off_t size)
Definition: XrdOssCache.cc:397
static void Trim2Base(char *eP)
Definition: XrdOssPath.cc:304
static const char xChar
Definition: XrdOssPath.hh:47
int GenRemotePath(const char *, char *)
Definition: XrdOssApi.cc:249
int Unlink(const char *, int Opts=0, XrdOucEnv *eP=0)
int GenLocalPath(const char *, char *)
Definition: XrdOssApi.cc:232
int MSS_Unlink(const char *)
Definition: XrdOssMSS.cc:333
int BreakLink(const char *local_path, struct stat &statbuff)
int Remdir(const char *, int Opts=0, XrdOucEnv *eP=0)
Definition: XrdOssUnlink.cc:70
char * RSSCmd
Definition: XrdOssApi.hh:238
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
int Opts
Definition: XrdMpxStats.cc:58