/src/zziplib/zzip/stat.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 136 lines · 79 code · 16 blank · 41 comment · 12 complexity · 06ceed2731027400d178fb6ecab0573a MD5 · raw file

  1. /*
  2. * Author:
  3. * Guido Draheim <guidod@gmx.de>
  4. * Tomi Ollila <Tomi.Ollila@iki.fi>
  5. *
  6. * Copyright (c) 1999,2000,2001,2002 Guido Draheim
  7. * All rights reserved,
  8. * use under the restrictions of the
  9. * Lesser GNU General Public License
  10. * or alternatively the restrictions
  11. * of the Mozilla Public License 1.1
  12. *
  13. * Description:
  14. * although this file is defining a function called zzip_stat it
  15. * will not need a real stat(2) exported by the Operating System.
  16. * It will just try to fill the fields of the ZZIP_STAT structure
  17. * of
  18. */
  19. #include <zzip/lib.h> /* exported... */
  20. #include <zzip/file.h>
  21. #include <string.h>
  22. #if defined(_AIX)
  23. #include <strings.h> /* for strcasecmp */
  24. #endif
  25. #include <sys/stat.h>
  26. #define ZZIP_USE_INTERNAL
  27. #include <zzip/info.h>
  28. /**
  29. * obtain information about a filename in an opened zip-archive without
  30. * opening that file first. Mostly used to obtain the uncompressed
  31. * size of a file inside a zip-archive. see => zzip_dir_open.
  32. */
  33. int
  34. zzip_dir_stat(ZZIP_DIR * dir, zzip_char_t * name, ZZIP_STAT * zs, int flags)
  35. {
  36. struct zzip_dir_hdr *hdr = dir->hdr0;
  37. int (*cmp) (zzip_char_t *, zzip_char_t *);
  38. if (flags & ZZIP_CASEINSENSITIVE) flags |= ZZIP_CASELESS;
  39. cmp = (flags & ZZIP_CASELESS) ? strcasecmp : strcmp;
  40. if (! hdr)
  41. {
  42. dir->errcode = ZZIP_ENOENT;
  43. return -1;
  44. }
  45. if (flags & ZZIP_IGNOREPATH)
  46. {
  47. char *n = strrchr(name, '/');
  48. if (n)
  49. name = n + 1;
  50. }
  51. while (1)
  52. {
  53. register char *hdr_name = hdr->d_name;
  54. if (flags & ZZIP_IGNOREPATH)
  55. {
  56. register char *n = strrchr(hdr_name, '/');
  57. if (n)
  58. hdr_name = n + 1;
  59. }
  60. if (! cmp(hdr_name, name))
  61. break;
  62. if (! hdr->d_reclen)
  63. {
  64. dir->errcode = ZZIP_ENOENT;
  65. return -1;
  66. }
  67. hdr = (struct zzip_dir_hdr *) ((char *) hdr + hdr->d_reclen);
  68. }
  69. zs->d_compr = hdr->d_compr;
  70. zs->d_csize = hdr->d_csize;
  71. zs->st_size = hdr->d_usize;
  72. zs->d_name = hdr->d_name;
  73. return 0;
  74. }
  75. /** => zzip_dir_stat
  76. * This function will obtain information about a opened file _within_ a
  77. * zip-archive. The file is supposed to be open (otherwise -1 is returned).
  78. * The st_size stat-member contains the uncompressed size. The optional
  79. * d_name is never set here.
  80. */
  81. int
  82. zzip_file_stat(ZZIP_FILE * file, ZZIP_STAT * zs)
  83. {
  84. if (! file)
  85. return -1;
  86. zs->d_compr = file->method;
  87. zs->d_csize = file->csize;
  88. zs->st_size = file->usize;
  89. zs->d_name = 0;
  90. return 0;
  91. }
  92. /** => zzip_dir_stat
  93. * This function will obtain information about a opened file which may be
  94. * either real/zipped. The file is supposed to be open (otherwise -1 is
  95. * returned). The st_size stat-member contains the uncompressed size.
  96. * The optional d_name is never set here. For a real file, we do set the
  97. * d_csize := st_size and d_compr := 0 for meaningful defaults.
  98. */
  99. int
  100. zzip_fstat(ZZIP_FILE * file, ZZIP_STAT * zs)
  101. {
  102. if (ZZIP_file_real(file))
  103. {
  104. struct stat st;
  105. if (fstat(file->fd, &st) < 0)
  106. return -1;
  107. zs->st_size = st.st_size;
  108. zs->d_csize = st.st_size;
  109. zs->d_compr = 0;
  110. return 0;
  111. } else
  112. {
  113. return zzip_file_stat(file, zs);
  114. }
  115. }
  116. /*
  117. * Local variables:
  118. * c-file-style: "stroustrup"
  119. * End:
  120. */