IDZEBRA  2.2.7
update_path.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 */
19 
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdio.h>
24 #include <assert.h>
25 #include <sys/types.h>
26 #ifdef WIN32
27 #include <io.h>
28 #define S_ISREG(x) (x & _S_IFREG)
29 #define S_ISDIR(x) (x & _S_IFDIR)
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #include <direntz.h>
35 #include <fcntl.h>
36 #include <time.h>
37 
38 #include "index.h"
39 
40 static void repositoryExtractR(ZebraHandle zh, char *rep, int level,
41  enum zebra_recctrl_action_t action)
42 {
43  struct dir_entry *e;
44  int i;
45  size_t rep_len = strlen(rep);
46 
47  e = dir_open(rep, zh->path_reg, zh->m_follow_links);
48  if (!e)
49  return;
50  yaz_log(YLOG_LOG, "dir %s", rep);
51  if (rep[rep_len-1] != '/')
52  rep[rep_len] = '/';
53  else
54  --rep_len;
55 
56  for (i=0; e[i].name; i++)
57  {
58  char *ecp;
59  strcpy(rep +rep_len+1, e[i].name);
60  if ((ecp = strrchr(e[i].name, '/')))
61  *ecp = '\0';
62 
63  switch (e[i].kind)
64  {
65  case dirs_file:
66  zebra_extract_file(zh, NULL, rep, action);
67  break;
68  case dirs_dir:
69  repositoryExtractR(zh, rep, level+1, action);
70  break;
71  }
72  }
73  dir_free(&e);
74 
75 }
76 
77 void repositoryShow(ZebraHandle zh, const char *path)
78 {
79  char src[1024];
80  int src_len;
81  struct dirs_entry *dst;
82  Dict dict;
83  struct dirs_info *di;
84 
85  if (!(dict = dict_open_res(zh->reg->bfs, FMATCH_DICT, 50, 0, 0, zh->res)))
86  {
87  yaz_log(YLOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
88  return;
89  }
90 
91  strncpy(src, path, sizeof(src)-1);
92  src[sizeof(src)-1]='\0';
93  src_len = strlen(src);
94 
95  if (src_len && src[src_len-1] != '/')
96  {
97  src[src_len] = '/';
98  src[++src_len] = '\0';
99  }
100 
101  di = dirs_open(dict, src, zh->m_flag_rw);
102 
103  while ((dst = dirs_read(di)))
104  yaz_log(YLOG_LOG, "%s", dst->path);
105  dirs_free(&di);
106  dict_close(dict);
107 }
108 
110  const char *path,
111  enum zebra_recctrl_action_t action)
112 
113 {
114  struct stat sbuf;
115  char src[1024];
116  int ret;
117 
118  assert (path);
119 
120  if (zh->path_reg && !yaz_is_abspath(path))
121  {
122  strcpy(src, zh->path_reg);
123  strcat(src, "/");
124  }
125  else
126  *src = '\0';
127  strcat(src, path);
128  ret = zebra_file_stat(src, &sbuf, zh->m_follow_links);
129 
130  strcpy(src, path);
131 
132  if (ret == -1)
133  yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot access path %s", src);
134  else if (S_ISREG(sbuf.st_mode))
135  zebra_extract_file(zh, NULL, src, action);
136  else if (S_ISDIR(sbuf.st_mode))
137  repositoryExtractR(zh, src, 0, action);
138  else
139  yaz_log(YLOG_WARN, "Skipping path %s", src);
140 }
141 
142 
144  enum zebra_recctrl_action_t action)
145 {
146  if (!strcmp(path, "") || !strcmp(path, "-"))
147  {
148  char src[1024];
149 
150  while (scanf("%1020s", src) == 1)
151  repositoryExtract(zh, src, action);
152  }
153  else
154  repositoryExtract(zh, path, action);
155  return ZEBRA_OK;
156 }
157 
158 /*
159  * Local variables:
160  * c-basic-offset: 4
161  * c-file-style: "Stroustrup"
162  * indent-tabs-mode: nil
163  * End:
164  * vim: shiftwidth=4 tabstop=8 expandtab
165  */
166 
int dict_close(Dict dict)
closes dictionary
Definition: close.c:32
static Dict dict
Definition: dicttest.c:35
struct dir_entry * dir_open(const char *rep, const char *base, int follow_links)
Definition: dir.c:48
int zebra_file_stat(const char *file_name, struct stat *buf, int follow_links)
Definition: dir.c:38
void dir_free(struct dir_entry **e_p)
Definition: dir.c:146
void dirs_free(struct dirs_info **pp)
Definition: dirs.c:217
struct dirs_entry * dirs_read(struct dirs_info *p)
Definition: dirs.c:134
struct dirs_info * dirs_open(Dict dict, const char *rep, int rw)
Definition: dirs.c:83
ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname, enum zebra_recctrl_action_t action)
Definition: extract.c:609
Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw, int compact_flag, Res res)
Definition: zebraapi.c:321
#define FMATCH_DICT
Definition: index.h:105
@ dirs_file
Definition: index.h:55
@ dirs_dir
Definition: index.h:55
zebra_recctrl_action_t
Definition: recctrl.h:87
Definition: index.h:57
enum dirsKind kind
Definition: index.h:58
char * name
Definition: index.h:59
Definition: index.h:63
char path[256]
Definition: index.h:65
Definition: dirs.c:35
BFiles bfs
Definition: index.h:137
struct zebra_register * reg
Definition: index.h:174
int m_flag_rw
Definition: index.h:225
char * path_reg
Definition: index.h:182
int m_follow_links
Definition: index.h:218
ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path, enum zebra_recctrl_action_t action)
Definition: update_path.c:143
static void repositoryExtractR(ZebraHandle zh, char *rep, int level, enum zebra_recctrl_action_t action)
Definition: update_path.c:40
static void repositoryExtract(ZebraHandle zh, const char *path, enum zebra_recctrl_action_t action)
Definition: update_path.c:109
void repositoryShow(ZebraHandle zh, const char *path)
Definition: update_path.c:77
#define ZEBRA_OK
Definition: util.h:82
short ZEBRA_RES
Common return type for Zebra API.
Definition: util.h:80