archive.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "mk.h"
  2. #include <ar.h>
  3. static void atimes(char *);
  4. static char *split(char*, char**);
  5. long
  6. atimeof(int force, char *name)
  7. {
  8. Symtab *sym;
  9. long t;
  10. char *archive, *member, buf[512];
  11. archive = split(name, &member);
  12. if(archive == 0)
  13. Exit();
  14. t = mtime(archive);
  15. sym = symlook(archive, S_AGG, 0);
  16. if(sym){
  17. if(force || (t > (long)sym->value)){
  18. atimes(archive);
  19. sym->value = (void *)t;
  20. }
  21. }
  22. else{
  23. atimes(archive);
  24. /* mark the aggegate as having been done */
  25. symlook(strdup(archive), S_AGG, "")->value = (void *)t;
  26. }
  27. /* truncate long member name to sizeof of name field in archive header */
  28. snprint(buf, sizeof(buf), "%s(%.*s)", archive, utfnlen(member, SARNAME), member);
  29. sym = symlook(buf, S_TIME, 0);
  30. if (sym)
  31. return (long)sym->value; /* uggh */
  32. return 0;
  33. }
  34. void
  35. atouch(char *name)
  36. {
  37. char *archive, *member;
  38. int fd, i;
  39. struct ar_hdr h;
  40. long t;
  41. archive = split(name, &member);
  42. if(archive == 0)
  43. Exit();
  44. fd = open(archive, ORDWR);
  45. if(fd < 0){
  46. fd = create(archive, OWRITE, 0666);
  47. if(fd < 0){
  48. perror(archive);
  49. Exit();
  50. }
  51. write(fd, ARMAG, SARMAG);
  52. }
  53. if(symlook(name, S_TIME, 0)){
  54. /* hoon off and change it in situ */
  55. LSEEK(fd, SARMAG, 0);
  56. while(read(fd, (char *)&h, sizeof(h)) == sizeof(h)){
  57. for(i = SARNAME-1; i > 0 && h.name[i] == ' '; i--)
  58. ;
  59. h.name[i+1]=0;
  60. if(strcmp(member, h.name) == 0){
  61. t = SARNAME-sizeof(h); /* ughgghh */
  62. LSEEK(fd, t, 1);
  63. fprint(fd, "%-12ld", time(0));
  64. break;
  65. }
  66. t = atol(h.size);
  67. if(t&01) t++;
  68. LSEEK(fd, t, 1);
  69. }
  70. }
  71. close(fd);
  72. }
  73. static void
  74. atimes(char *ar)
  75. {
  76. struct ar_hdr h;
  77. long t;
  78. int fd, i;
  79. char buf[BIGBLOCK];
  80. fd = open(ar, OREAD);
  81. if(fd < 0)
  82. return;
  83. if(read(fd, buf, SARMAG) != SARMAG){
  84. close(fd);
  85. return;
  86. }
  87. while(read(fd, (char *)&h, sizeof(h)) == sizeof(h)){
  88. t = atol(h.date);
  89. if(t == 0) /* as it sometimes happens; thanks ken */
  90. t = 1;
  91. for(i = sizeof(h.name)-1; i > 0 && h.name[i] == ' '; i--)
  92. ;
  93. if(h.name[i] == '/') /* system V bug */
  94. i--;
  95. h.name[i+1]=0; /* can stomp on date field */
  96. sprint(buf, "%s(%s)", ar, h.name);
  97. symlook(strdup(buf), S_TIME, (void *)t)->value = (void *)t;
  98. t = atol(h.size);
  99. if(t&01) t++;
  100. LSEEK(fd, t, 1);
  101. }
  102. close(fd);
  103. }
  104. static int
  105. type(char *file)
  106. {
  107. int fd;
  108. char buf[SARMAG];
  109. fd = open(file, OREAD);
  110. if(fd < 0){
  111. if(symlook(file, S_BITCH, 0) == 0){
  112. Bprint(&bout, "%s doesn't exist: assuming it will be an archive\n", file);
  113. symlook(file, S_BITCH, (void *)file);
  114. }
  115. return 1;
  116. }
  117. if(read(fd, buf, SARMAG) != SARMAG){
  118. close(fd);
  119. return 0;
  120. }
  121. close(fd);
  122. return !strncmp(ARMAG, buf, SARMAG);
  123. }
  124. static char*
  125. split(char *name, char **member)
  126. {
  127. char *p, *q;
  128. p = strdup(name);
  129. q = utfrune(p, '(');
  130. if(q){
  131. *q++ = 0;
  132. if(member)
  133. *member = q;
  134. q = utfrune(q, ')');
  135. if (q)
  136. *q = 0;
  137. if(type(p))
  138. return p;
  139. free(p);
  140. fprint(2, "mk: '%s' is not an archive\n", name);
  141. }
  142. return 0;
  143. }