1 /* Copyright (C) 2010-2017 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (file_path.c).
5 * ---------------------------------------------------------------------------------------
6 *
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <errno.h>
28
29 #include <sys/stat.h>
30
31 #include <boolean.h>
32 #include <file/file_path.h>
33
34 #ifndef __MACH__
35 #include <compat/strl.h>
36 #include <compat/posix_string.h>
37 #endif
38 #include <compat/strcasestr.h>
39 #include <retro_miscellaneous.h>
40
41 #if defined(_WIN32)
42 #ifdef _MSC_VER
43 #define setmode _setmode
44 #endif
45 #include <sys/stat.h>
46 #ifdef _XBOX
47 #include <xtl.h>
48 #define INVALID_FILE_ATTRIBUTES -1
49 #else
50 #include <io.h>
51 #include <fcntl.h>
52 #include <direct.h>
53 #include <windows.h>
54 #endif
55 #elif defined(VITA)
56 #define SCE_ERROR_ERRNO_EEXIST 0x80010011
57 #include <psp2/io/fcntl.h>
58 #include <psp2/io/dirent.h>
59 #include <psp2/io/stat.h>
60 #else
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <unistd.h>
64 #endif
65
66 #if defined(PSP)
67 #include <pspkernel.h>
68 #endif
69
70 #ifdef __HAIKU__
71 #include <kernel/image.h>
72 #endif
73
74 #if defined(VITA)
75 #define FIO_S_ISDIR SCE_S_ISDIR
76 #endif
77
78 #if defined(__QNX__) || defined(PSP)
79 #include <unistd.h> /* stat() is defined here */
80 #endif
81
82 enum stat_mode
83 {
84 IS_DIRECTORY = 0,
85 IS_CHARACTER_SPECIAL,
86 IS_VALID
87 };
88
path_stat(const char * path,enum stat_mode mode,int32_t * size)89 static bool path_stat(const char *path, enum stat_mode mode, int32_t *size)
90 {
91 #if defined(VITA) || defined(PSP)
92 SceIoStat buf;
93 char *tmp = strdup(path);
94 size_t len = strlen(tmp);
95 if (tmp[len-1] == '/')
96 tmp[len-1]='\0';
97
98 if (sceIoGetstat(tmp, &buf) < 0)
99 {
100 free(tmp);
101 return false;
102 }
103 free(tmp);
104
105 #elif defined(_WIN32)
106 struct _stat buf;
107 DWORD file_info = GetFileAttributes(path);
108
109 _stat(path, &buf);
110
111 if (file_info == INVALID_FILE_ATTRIBUTES)
112 return false;
113 #else
114 struct stat buf;
115 if (stat(path, &buf) < 0)
116 return false;
117 #endif
118
119 if (size)
120 *size = (int32_t)buf.st_size;
121
122 switch (mode)
123 {
124 case IS_DIRECTORY:
125 #if defined(VITA) || defined(PSP)
126 return FIO_S_ISDIR(buf.st_mode);
127 #elif defined(_WIN32)
128 return (file_info & FILE_ATTRIBUTE_DIRECTORY);
129 #else
130 return S_ISDIR(buf.st_mode);
131 #endif
132 case IS_CHARACTER_SPECIAL:
133 #if defined(VITA) || defined(PSP) || defined(_WIN32)
134 return false;
135 #else
136 return S_ISCHR(buf.st_mode);
137 #endif
138 case IS_VALID:
139 return true;
140 }
141
142 return false;
143 }
144
145 /**
146 * path_is_directory:
147 * @path : path
148 *
149 * Checks if path is a directory.
150 *
151 * Returns: true (1) if path is a directory, otherwise false (0).
152 */
path_is_directory(const char * path)153 bool path_is_directory(const char *path)
154 {
155 return path_stat(path, IS_DIRECTORY, NULL);
156 }
157
path_is_character_special(const char * path)158 bool path_is_character_special(const char *path)
159 {
160 return path_stat(path, IS_CHARACTER_SPECIAL, NULL);
161 }
162
path_is_valid(const char * path)163 bool path_is_valid(const char *path)
164 {
165 return path_stat(path, IS_VALID, NULL);
166 }
167
path_get_size(const char * path)168 int32_t path_get_size(const char *path)
169 {
170 int32_t filesize = 0;
171 if (path_stat(path, IS_VALID, &filesize))
172 return filesize;
173
174 return -1;
175 }
176
177 /**
178 * path_mkdir:
179 * @dir : directory
180 *
181 * Create directory on filesystem.
182 *
183 * Returns: true (1) if directory could be created, otherwise false (0).
184 **/
path_mkdir(const char * dir)185 bool path_mkdir(const char *dir)
186 {
187 /* Use heap. Real chance of stack overflow if we recurse too hard. */
188 char *basedir = strdup(dir);
189 const char *target = NULL;
190 bool sret = false;
191 bool norecurse = false;
192
193 if (!basedir)
194 return false;
195
196 path_parent_dir(basedir);
197 if (!*basedir || !strcmp(basedir, dir))
198 goto end;
199
200 if (path_is_directory(basedir))
201 {
202 target = dir;
203 norecurse = true;
204 }
205 else
206 {
207 target = basedir;
208 sret = path_mkdir(basedir);
209
210 if (sret)
211 {
212 target = dir;
213 norecurse = true;
214 }
215 }
216
217 if (norecurse)
218 {
219 #if defined(_WIN32)
220 int ret = _mkdir(dir);
221 #elif defined(IOS)
222 int ret = mkdir(dir, 0755);
223 #elif defined(VITA) || defined(PSP)
224 int ret = sceIoMkdir(dir, 0777);
225 #elif defined(__QNX__)
226 int ret = mkdir(dir, 0777);
227 #else
228 int ret = mkdir(dir, 0750);
229 #endif
230
231 /* Don't treat this as an error. */
232 #if defined(VITA)
233 if ((ret == SCE_ERROR_ERRNO_EEXIST) && path_is_directory(dir))
234 ret = 0;
235 #elif defined(PSP) || defined(_3DS) || defined(WIIU)
236 if ((ret == -1) && path_is_directory(dir))
237 ret = 0;
238 #else
239 if (ret < 0 && errno == EEXIST && path_is_directory(dir))
240 ret = 0;
241 #endif
242 if (ret < 0)
243 printf("mkdir(%s) error: %s.\n", dir, strerror(errno));
244 sret = (ret == 0);
245 }
246
247 end:
248 if (target && !sret)
249 printf("Failed to create directory: \"%s\".\n", target);
250 free(basedir);
251 return sret;
252 }
253
254 /**
255 * path_get_archive_delim:
256 * @path : path
257 *
258 * Find delimiter of an archive file. Only the first '#'
259 * after a compression extension is considered.
260 *
261 * Returns: pointer to the delimiter in the path if it contains
262 * a path inside a compressed file, otherwise NULL.
263 */
path_get_archive_delim(const char * path)264 const char *path_get_archive_delim(const char *path)
265 {
266 const char *last = find_last_slash(path);
267 const char *delim = NULL;
268
269 if (last)
270 {
271 delim = strcasestr(last, ".zip#");
272
273 if (!delim)
274 delim = strcasestr(last, ".apk#");
275 }
276
277 if (delim)
278 return delim + 4;
279
280 if (last)
281 delim = strcasestr(last, ".7z#");
282
283 if (delim)
284 return delim + 3;
285
286 return NULL;
287 }
288
289 /**
290 * path_get_extension:
291 * @path : path
292 *
293 * Gets extension of file. Only '.'s
294 * after the last slash are considered.
295 *
296 * Returns: extension part from the path.
297 */
path_get_extension(const char * path)298 const char *path_get_extension(const char *path)
299 {
300 const char *ext = strrchr(path_basename(path), '.');
301 if (!ext)
302 return "";
303 return ext + 1;
304 }
305
306 /**
307 * path_remove_extension:
308 * @path : path
309 *
310 * Removes the extension from the path and returns the result.
311 * Removes all text after and including the last '.'.
312 * Only '.'s after the last slash are considered.
313 *
314 * Returns: path with the extension part removed.
315 */
path_remove_extension(char * path)316 char *path_remove_extension(char *path)
317 {
318 char *last = (char*)strrchr(path_basename(path), '.');
319 if (!last)
320 return NULL;
321 if (*last)
322 *last = '\0';
323 return last;
324 }
325
326 /**
327 * path_is_compressed_file:
328 * @path : path
329 *
330 * Checks if path is a compressed file.
331 *
332 * Returns: true (1) if path is a compressed file, otherwise false (0).
333 **/
path_is_compressed_file(const char * path)334 bool path_is_compressed_file(const char* path)
335 {
336 const char *ext = path_get_extension(path);
337
338 if ( strcasestr(ext, "zip")
339 || strcasestr(ext, "apk")
340 || strcasestr(ext, "7z"))
341 return true;
342
343 return false;
344 }
345
346 /**
347 * path_file_exists:
348 * @path : path
349 *
350 * Checks if a file already exists at the specified path (@path).
351 *
352 * Returns: true (1) if file already exists, otherwise false (0).
353 */
path_file_exists(const char * path)354 bool path_file_exists(const char *path)
355 {
356 FILE *dummy = fopen(path, "rb");
357
358 if (!dummy)
359 return false;
360
361 fclose(dummy);
362 return true;
363 }
364
365 /**
366 * fill_pathname:
367 * @out_path : output path
368 * @in_path : input path
369 * @replace : what to replace
370 * @size : buffer size of output path
371 *
372 * FIXME: Verify
373 *
374 * Replaces filename extension with 'replace' and outputs result to out_path.
375 * The extension here is considered to be the string from the last '.'
376 * to the end.
377 *
378 * Only '.'s after the last slash are considered as extensions.
379 * If no '.' is present, in_path and replace will simply be concatenated.
380 * 'size' is buffer size of 'out_path'.
381 * E.g.: in_path = "/foo/bar/baz/boo.c", replace = ".asm" =>
382 * out_path = "/foo/bar/baz/boo.asm"
383 * E.g.: in_path = "/foo/bar/baz/boo.c", replace = "" =>
384 * out_path = "/foo/bar/baz/boo"
385 */
fill_pathname(char * out_path,const char * in_path,const char * replace,size_t size)386 void fill_pathname(char *out_path, const char *in_path,
387 const char *replace, size_t size)
388 {
389 char tmp_path[PATH_MAX_LENGTH];
390 char *tok = NULL;
391
392 tmp_path[0] = '\0';
393
394 strlcpy(tmp_path, in_path, sizeof(tmp_path));
395 if ((tok = (char*)strrchr(path_basename(tmp_path), '.')))
396 *tok = '\0';
397
398 fill_pathname_noext(out_path, tmp_path, replace, size);
399 }
400
401 /**
402 * fill_pathname_noext:
403 * @out_path : output path
404 * @in_path : input path
405 * @replace : what to replace
406 * @size : buffer size of output path
407 *
408 * Appends a filename extension 'replace' to 'in_path', and outputs
409 * result in 'out_path'.
410 *
411 * Assumes in_path has no extension. If an extension is still
412 * present in 'in_path', it will be ignored.
413 *
414 */
fill_pathname_noext(char * out_path,const char * in_path,const char * replace,size_t size)415 void fill_pathname_noext(char *out_path, const char *in_path,
416 const char *replace, size_t size)
417 {
418 strlcpy(out_path, in_path, size);
419 strlcat(out_path, replace, size);
420 }
421
find_last_slash(const char * str)422 char *find_last_slash(const char *str)
423 {
424 const char *slash = strrchr(str, '/');
425 #ifdef _WIN32
426 const char *backslash = strrchr(str, '\\');
427
428 if (backslash && ((slash && backslash > slash) || !slash))
429 slash = backslash;
430 #endif
431
432 return (char*)slash;
433 }
434
435 /**
436 * fill_pathname_slash:
437 * @path : path
438 * @size : size of path
439 *
440 * Assumes path is a directory. Appends a slash
441 * if not already there.
442 **/
fill_pathname_slash(char * path,size_t size)443 void fill_pathname_slash(char *path, size_t size)
444 {
445 size_t path_len = strlen(path);
446 const char *last_slash = find_last_slash(path);
447
448 /* Try to preserve slash type. */
449 if (last_slash && (last_slash != (path + path_len - 1)))
450 {
451 char join_str[2];
452
453 join_str[0] = '\0';
454
455 strlcpy(join_str, last_slash, sizeof(join_str));
456 strlcat(path, join_str, size);
457 }
458 else if (!last_slash)
459 strlcat(path, path_default_slash(), size);
460 }
461
462 /**
463 * fill_pathname_dir:
464 * @in_dir : input directory path
465 * @in_basename : input basename to be appended to @in_dir
466 * @replace : replacement to be appended to @in_basename
467 * @size : size of buffer
468 *
469 * Appends basename of 'in_basename', to 'in_dir', along with 'replace'.
470 * Basename of in_basename is the string after the last '/' or '\\',
471 * i.e the filename without directories.
472 *
473 * If in_basename has no '/' or '\\', the whole 'in_basename' will be used.
474 * 'size' is buffer size of 'in_dir'.
475 *
476 * E.g..: in_dir = "/tmp/some_dir", in_basename = "/some_content/foo.c",
477 * replace = ".asm" => in_dir = "/tmp/some_dir/foo.c.asm"
478 **/
fill_pathname_dir(char * in_dir,const char * in_basename,const char * replace,size_t size)479 void fill_pathname_dir(char *in_dir, const char *in_basename,
480 const char *replace, size_t size)
481 {
482 const char *base = NULL;
483
484 fill_pathname_slash(in_dir, size);
485 base = path_basename(in_basename);
486 strlcat(in_dir, base, size);
487 strlcat(in_dir, replace, size);
488 }
489
490 /**
491 * fill_pathname_base:
492 * @out : output path
493 * @in_path : input path
494 * @size : size of output path
495 *
496 * Copies basename of @in_path into @out_path.
497 **/
fill_pathname_base(char * out,const char * in_path,size_t size)498 void fill_pathname_base(char *out, const char *in_path, size_t size)
499 {
500 const char *ptr = path_basename(in_path);
501
502 if (!ptr)
503 ptr = in_path;
504
505 strlcpy(out, ptr, size);
506 }
507
fill_pathname_base_noext(char * out,const char * in_path,size_t size)508 void fill_pathname_base_noext(char *out, const char *in_path, size_t size)
509 {
510 fill_pathname_base(out, in_path, size);
511 path_remove_extension(out);
512 }
513
fill_pathname_base_ext(char * out,const char * in_path,const char * ext,size_t size)514 void fill_pathname_base_ext(char *out, const char *in_path, const char *ext,
515 size_t size)
516 {
517 fill_pathname_base_noext(out, in_path, size);
518 strlcat(out, ext, size);
519 }
520
521 /**
522 * fill_pathname_basedir:
523 * @out_dir : output directory
524 * @in_path : input path
525 * @size : size of output directory
526 *
527 * Copies base directory of @in_path into @out_path.
528 * If in_path is a path without any slashes (relative current directory),
529 * @out_path will get path "./".
530 **/
fill_pathname_basedir(char * out_dir,const char * in_path,size_t size)531 void fill_pathname_basedir(char *out_dir,
532 const char *in_path, size_t size)
533 {
534 if (out_dir != in_path)
535 strlcpy(out_dir, in_path, size);
536 path_basedir(out_dir);
537 }
538
fill_pathname_basedir_noext(char * out_dir,const char * in_path,size_t size)539 void fill_pathname_basedir_noext(char *out_dir,
540 const char *in_path, size_t size)
541 {
542 fill_pathname_basedir(out_dir, in_path, size);
543 path_remove_extension(out_dir);
544 }
545
546 /**
547 * fill_pathname_parent_dir:
548 * @out_dir : output directory
549 * @in_dir : input directory
550 * @size : size of output directory
551 *
552 * Copies parent directory of @in_dir into @out_dir.
553 * Assumes @in_dir is a directory. Keeps trailing '/'.
554 **/
fill_pathname_parent_dir(char * out_dir,const char * in_dir,size_t size)555 void fill_pathname_parent_dir(char *out_dir,
556 const char *in_dir, size_t size)
557 {
558 if (out_dir != in_dir)
559 strlcpy(out_dir, in_dir, size);
560 path_parent_dir(out_dir);
561 }
562
563 /**
564 * fill_dated_filename:
565 * @out_filename : output filename
566 * @ext : extension of output filename
567 * @size : buffer size of output filename
568 *
569 * Creates a 'dated' filename prefixed by 'RetroArch', and
570 * concatenates extension (@ext) to it.
571 *
572 * E.g.:
573 * out_filename = "RetroArch-{month}{day}-{Hours}{Minutes}.{@ext}"
574 **/
fill_dated_filename(char * out_filename,const char * ext,size_t size)575 void fill_dated_filename(char *out_filename,
576 const char *ext, size_t size)
577 {
578 time_t cur_time = time(NULL);
579
580 strftime(out_filename, size,
581 "RetroArch-%m%d-%H%M%S.", localtime(&cur_time));
582 strlcat(out_filename, ext, size);
583 }
584
585 /**
586 * fill_str_dated_filename:
587 * @out_filename : output filename
588 * @in_str : input string
589 * @ext : extension of output filename
590 * @size : buffer size of output filename
591 *
592 * Creates a 'dated' filename prefixed by the string @in_str, and
593 * concatenates extension (@ext) to it.
594 *
595 * E.g.:
596 * out_filename = "RetroArch-{year}{month}{day}-{Hour}{Minute}{Second}.{@ext}"
597 **/
fill_str_dated_filename(char * out_filename,const char * in_str,const char * ext,size_t size)598 void fill_str_dated_filename(char *out_filename,
599 const char *in_str, const char *ext, size_t size)
600 {
601 char format[256];
602 time_t cur_time = time(NULL);
603
604 format[0] = '\0';
605
606 strftime(format, sizeof(format), "-%y%m%d-%H%M%S.", localtime(&cur_time));
607 strlcpy(out_filename, in_str, size);
608 strlcat(out_filename, format, size);
609 strlcat(out_filename, ext, size);
610 }
611
612 /**
613 * path_basedir:
614 * @path : path
615 *
616 * Extracts base directory by mutating path.
617 * Keeps trailing '/'.
618 **/
path_basedir(char * path)619 void path_basedir(char *path)
620 {
621 char *last = NULL;
622 if (strlen(path) < 2)
623 return;
624
625 last = find_last_slash(path);
626
627 if (last)
628 last[1] = '\0';
629 else
630 snprintf(path, 3, ".%s", path_default_slash());
631 }
632
633 /**
634 * path_parent_dir:
635 * @path : path
636 *
637 * Extracts parent directory by mutating path.
638 * Assumes that path is a directory. Keeps trailing '/'.
639 **/
path_parent_dir(char * path)640 void path_parent_dir(char *path)
641 {
642 size_t len = strlen(path);
643 if (len && path_char_is_slash(path[len - 1]))
644 path[len - 1] = '\0';
645 path_basedir(path);
646 }
647
648 /**
649 * path_basename:
650 * @path : path
651 *
652 * Get basename from @path.
653 *
654 * Returns: basename from path.
655 **/
path_basename(const char * path)656 const char *path_basename(const char *path)
657 {
658 /* We cut either at the first compression-related hash
659 * or the last slash; whichever comes last */
660 const char *last = find_last_slash(path);
661 const char *delim = path_get_archive_delim(path);
662
663 if (delim)
664 return delim + 1;
665
666 if (last)
667 return last + 1;
668
669 return path;
670 }
671
672 /**
673 * path_is_absolute:
674 * @path : path
675 *
676 * Checks if @path is an absolute path or a relative path.
677 *
678 * Returns: true if path is absolute, false if path is relative.
679 **/
path_is_absolute(const char * path)680 bool path_is_absolute(const char *path)
681 {
682 if (path[0] == '/')
683 return true;
684 #ifdef _WIN32
685 /* Many roads lead to Rome ... */
686 if (( strstr(path, "\\\\") == path)
687 || strstr(path, ":/")
688 || strstr(path, ":\\")
689 || strstr(path, ":\\\\"))
690 return true;
691 #endif
692 return false;
693 }
694
695 /**
696 * path_resolve_realpath:
697 * @buf : buffer for path
698 * @size : size of buffer
699 *
700 * Turns relative paths into absolute path.
701 * If relative, rebases on current working dir.
702 **/
path_resolve_realpath(char * buf,size_t size)703 void path_resolve_realpath(char *buf, size_t size)
704 {
705 #ifndef RARCH_CONSOLE
706 char tmp[PATH_MAX_LENGTH];
707
708 tmp[0] = '\0';
709
710 strlcpy(tmp, buf, sizeof(tmp));
711
712 #ifdef _WIN32
713 if (!_fullpath(buf, tmp, size))
714 strlcpy(buf, tmp, size);
715 #else
716
717 /* NOTE: realpath() expects at least PATH_MAX_LENGTH bytes in buf.
718 * Technically, PATH_MAX_LENGTH needn't be defined, but we rely on it anyways.
719 * POSIX 2008 can automatically allocate for you,
720 * but don't rely on that. */
721 if (!realpath(tmp, buf))
722 strlcpy(buf, tmp, size);
723 #endif
724 #endif
725 }
726
727 /**
728 * fill_pathname_resolve_relative:
729 * @out_path : output path
730 * @in_refpath : input reference path
731 * @in_path : input path
732 * @size : size of @out_path
733 *
734 * Joins basedir of @in_refpath together with @in_path.
735 * If @in_path is an absolute path, out_path = in_path.
736 * E.g.: in_refpath = "/foo/bar/baz.a", in_path = "foobar.cg",
737 * out_path = "/foo/bar/foobar.cg".
738 **/
fill_pathname_resolve_relative(char * out_path,const char * in_refpath,const char * in_path,size_t size)739 void fill_pathname_resolve_relative(char *out_path,
740 const char *in_refpath, const char *in_path, size_t size)
741 {
742 if (path_is_absolute(in_path))
743 {
744 strlcpy(out_path, in_path, size);
745 return;
746 }
747
748 fill_pathname_basedir(out_path, in_refpath, size);
749 strlcat(out_path, in_path, size);
750 }
751
752 /**
753 * fill_pathname_join:
754 * @out_path : output path
755 * @dir : directory
756 * @path : path
757 * @size : size of output path
758 *
759 * Joins a directory (@dir) and path (@path) together.
760 * Makes sure not to get two consecutive slashes
761 * between directory and path.
762 **/
fill_pathname_join(char * out_path,const char * dir,const char * path,size_t size)763 void fill_pathname_join(char *out_path,
764 const char *dir, const char *path, size_t size)
765 {
766 if (out_path != dir)
767 strlcpy(out_path, dir, size);
768
769 if (*out_path)
770 fill_pathname_slash(out_path, size);
771
772 strlcat(out_path, path, size);
773 }
774
fill_pathname_join_special_ext(char * out_path,const char * dir,const char * path,const char * last,const char * ext,size_t size)775 void fill_pathname_join_special_ext(char *out_path,
776 const char *dir, const char *path,
777 const char *last, const char *ext,
778 size_t size)
779 {
780 fill_pathname_join(out_path, dir, path, size);
781 if (*out_path)
782 fill_pathname_slash(out_path, size);
783
784 strlcat(out_path, last, size);
785 strlcat(out_path, ext, size);
786 }
787
fill_pathname_join_concat(char * out_path,const char * dir,const char * path,const char * concat,size_t size)788 void fill_pathname_join_concat(char *out_path,
789 const char *dir, const char *path,
790 const char *concat,
791 size_t size)
792 {
793 fill_pathname_join(out_path, dir, path, size);
794 strlcat(out_path, concat, size);
795 }
796
fill_pathname_join_noext(char * out_path,const char * dir,const char * path,size_t size)797 void fill_pathname_join_noext(char *out_path,
798 const char *dir, const char *path, size_t size)
799 {
800 fill_pathname_join(out_path, dir, path, size);
801 path_remove_extension(out_path);
802 }
803
804
805 /**
806 * fill_pathname_join_delim:
807 * @out_path : output path
808 * @dir : directory
809 * @path : path
810 * @delim : delimiter
811 * @size : size of output path
812 *
813 * Joins a directory (@dir) and path (@path) together
814 * using the given delimiter (@delim).
815 **/
fill_pathname_join_delim(char * out_path,const char * dir,const char * path,const char delim,size_t size)816 void fill_pathname_join_delim(char *out_path, const char *dir,
817 const char *path, const char delim, size_t size)
818 {
819 size_t copied = strlcpy(out_path, dir, size);
820
821 out_path[copied] = delim;
822 out_path[copied+1] = '\0';
823
824 strlcat(out_path, path, size);
825 }
826
fill_pathname_join_delim_concat(char * out_path,const char * dir,const char * path,const char delim,const char * concat,size_t size)827 void fill_pathname_join_delim_concat(char *out_path, const char *dir,
828 const char *path, const char delim, const char *concat,
829 size_t size)
830 {
831 fill_pathname_join_delim(out_path, dir, path, delim, size);
832 strlcat(out_path, concat, size);
833 }
834
835 /**
836 * fill_short_pathname_representation:
837 * @out_rep : output representation
838 * @in_path : input path
839 * @size : size of output representation
840 *
841 * Generates a short representation of path. It should only
842 * be used for displaying the result; the output representation is not
843 * binding in any meaningful way (for a normal path, this is the same as basename)
844 * In case of more complex URLs, this should cut everything except for
845 * the main image file.
846 *
847 * E.g.: "/path/to/game.img" -> game.img
848 * "/path/to/myarchive.7z#folder/to/game.img" -> game.img
849 */
fill_short_pathname_representation(char * out_rep,const char * in_path,size_t size)850 void fill_short_pathname_representation(char* out_rep,
851 const char *in_path, size_t size)
852 {
853 char path_short[PATH_MAX_LENGTH];
854
855 path_short[0] = '\0';
856
857 fill_pathname(path_short, path_basename(in_path), "",
858 sizeof(path_short));
859
860 strlcpy(out_rep, path_short, size);
861 }
862
fill_short_pathname_representation_noext(char * out_rep,const char * in_path,size_t size)863 void fill_short_pathname_representation_noext(char* out_rep,
864 const char *in_path, size_t size)
865 {
866 fill_short_pathname_representation(out_rep, in_path, size);
867 path_remove_extension(out_rep);
868 }
869