util.c (3d886aa3be15439e05784ac1cbd4acc2f13c0048) | util.c (f045b8c4b36baddcfbdd4d3d956446e688b0b3cd) |
---|---|
1#include "../perf.h" 2#include "util.h" 3#include "debug.h" 4#include <api/fs/fs.h> 5#include <sys/mman.h> 6#include <sys/stat.h> 7#include <sys/utsname.h> 8#include <dirent.h> --- 129 unchanged lines hidden (view full) --- 138 strlist__add(list, d->d_name); 139 } 140 141out: 142 closedir(dir); 143 return list; 144} 145 | 1#include "../perf.h" 2#include "util.h" 3#include "debug.h" 4#include <api/fs/fs.h> 5#include <sys/mman.h> 6#include <sys/stat.h> 7#include <sys/utsname.h> 8#include <dirent.h> --- 129 unchanged lines hidden (view full) --- 138 strlist__add(list, d->d_name); 139 } 140 141out: 142 closedir(dir); 143 return list; 144} 145 |
146static int slow_copyfile(const char *from, const char *to) | 146static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi) |
147{ 148 int err = -1; 149 char *line = NULL; 150 size_t n; | 147{ 148 int err = -1; 149 char *line = NULL; 150 size_t n; |
151 FILE *from_fp = fopen(from, "r"), *to_fp; | 151 FILE *from_fp, *to_fp; 152 struct nscookie nsc; |
152 | 153 |
154 nsinfo__mountns_enter(nsi, &nsc); 155 from_fp = fopen(from, "r"); 156 nsinfo__mountns_exit(&nsc); |
|
153 if (from_fp == NULL) 154 goto out; 155 156 to_fp = fopen(to, "w"); 157 if (to_fp == NULL) 158 goto out_fclose_from; 159 160 while (getline(&line, &n, from_fp) > 0) --- 32 unchanged lines hidden (view full) --- 193 off_in += ret; 194 off_out -= ret; 195 } 196 munmap(ptr, off_in + size); 197 198 return size ? -1 : 0; 199} 200 | 157 if (from_fp == NULL) 158 goto out; 159 160 to_fp = fopen(to, "w"); 161 if (to_fp == NULL) 162 goto out_fclose_from; 163 164 while (getline(&line, &n, from_fp) > 0) --- 32 unchanged lines hidden (view full) --- 197 off_in += ret; 198 off_out -= ret; 199 } 200 munmap(ptr, off_in + size); 201 202 return size ? -1 : 0; 203} 204 |
201int copyfile_mode(const char *from, const char *to, mode_t mode) | 205static int copyfile_mode_ns(const char *from, const char *to, mode_t mode, 206 struct nsinfo *nsi) |
202{ 203 int fromfd, tofd; 204 struct stat st; | 207{ 208 int fromfd, tofd; 209 struct stat st; |
205 int err = -1; | 210 int err; |
206 char *tmp = NULL, *ptr = NULL; | 211 char *tmp = NULL, *ptr = NULL; |
212 struct nscookie nsc; |
|
207 | 213 |
208 if (stat(from, &st)) | 214 nsinfo__mountns_enter(nsi, &nsc); 215 err = stat(from, &st); 216 nsinfo__mountns_exit(&nsc); 217 if (err) |
209 goto out; | 218 goto out; |
219 err = -1; |
|
210 211 /* extra 'x' at the end is to reserve space for '.' */ 212 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) { 213 tmp = NULL; 214 goto out; 215 } 216 ptr = strrchr(tmp, '/'); 217 if (!ptr) --- 4 unchanged lines hidden (view full) --- 222 tofd = mkstemp(tmp); 223 if (tofd < 0) 224 goto out; 225 226 if (fchmod(tofd, mode)) 227 goto out_close_to; 228 229 if (st.st_size == 0) { /* /proc? do it slowly... */ | 220 221 /* extra 'x' at the end is to reserve space for '.' */ 222 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) { 223 tmp = NULL; 224 goto out; 225 } 226 ptr = strrchr(tmp, '/'); 227 if (!ptr) --- 4 unchanged lines hidden (view full) --- 232 tofd = mkstemp(tmp); 233 if (tofd < 0) 234 goto out; 235 236 if (fchmod(tofd, mode)) 237 goto out_close_to; 238 239 if (st.st_size == 0) { /* /proc? do it slowly... */ |
230 err = slow_copyfile(from, tmp); | 240 err = slow_copyfile(from, tmp, nsi); |
231 goto out_close_to; 232 } 233 | 241 goto out_close_to; 242 } 243 |
244 nsinfo__mountns_enter(nsi, &nsc); |
|
234 fromfd = open(from, O_RDONLY); | 245 fromfd = open(from, O_RDONLY); |
246 nsinfo__mountns_exit(&nsc); |
|
235 if (fromfd < 0) 236 goto out_close_to; 237 238 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size); 239 240 close(fromfd); 241out_close_to: 242 close(tofd); 243 if (!err) 244 err = link(tmp, to); 245 unlink(tmp); 246out: 247 free(tmp); 248 return err; 249} 250 | 247 if (fromfd < 0) 248 goto out_close_to; 249 250 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size); 251 252 close(fromfd); 253out_close_to: 254 close(tofd); 255 if (!err) 256 err = link(tmp, to); 257 unlink(tmp); 258out: 259 free(tmp); 260 return err; 261} 262 |
263int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi) 264{ 265 return copyfile_mode_ns(from, to, 0755, nsi); 266} 267 268int copyfile_mode(const char *from, const char *to, mode_t mode) 269{ 270 return copyfile_mode_ns(from, to, mode, NULL); 271} 272 |
|
251int copyfile(const char *from, const char *to) 252{ 253 return copyfile_mode(from, to, 0755); 254} 255 256static ssize_t ion(bool is_read, int fd, void *buf, size_t n) 257{ 258 void *buf_start = buf; --- 199 unchanged lines hidden --- | 273int copyfile(const char *from, const char *to) 274{ 275 return copyfile_mode(from, to, 0755); 276} 277 278static ssize_t ion(bool is_read, int fd, void *buf, size_t n) 279{ 280 void *buf_start = buf; --- 199 unchanged lines hidden --- |