File: file.c

package info (click to toggle)
epm 4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,616 kB
  • ctags: 475
  • sloc: ansic: 10,798; cpp: 1,596; makefile: 309
file content (327 lines) | stat: -rw-r--r-- 7,277 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * "$Id: file.c 703 2006-08-29 16:43:09Z mike $"
 *
 *   File functions for the ESP Package Manager (EPM).
 *
 *   Copyright 1999-2005 by Easy Software Products.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 * Contents:
 *
 *   copy_file()      - Copy a file...
 *   make_directory() - Make a directory.
 *   make_link()      - Make a symbolic link.
 *   strip_execs()    - Strip symbols from executable files in the
 *                      distribution.
 *   unlink_package() - Delete a package file.
 */

/*
 * Include necessary headers...
 */

#include "epm.h"


/*
 * 'copy_file()' - Copy a file...
 */

int					/* O - 0 on success, -1 on failure */
copy_file(const char *dst,		/* I - Destination file */
          const char *src,		/* I - Source file */
          mode_t     mode,		/* I - Permissions */
	  uid_t      owner,		/* I - Owner ID */
	  gid_t      group)		/* I - Group ID */
{
  FILE		*dstfile,		/* Destination file */
		*srcfile;		/* Source file */
  char		buffer[8192];		/* Copy buffer */
  char		*slash;			/* Pointer to trailing slash */
  size_t	bytes;			/* Number of bytes read/written */


 /*
  * Check that the destination directory exists...
  */

  strcpy(buffer, dst);
  if ((slash = strrchr(buffer, '/')) != NULL)
    *slash = '\0';

  if (access(buffer, F_OK))
    make_directory(buffer, 0755, owner, group);

 /*
  * Open files...
  */

  unlink(dst);

  if ((dstfile = fopen(dst, "wb")) == NULL)
  {
    fprintf(stderr, "epm: Unable to create \"%s\" -\n     %s\n", dst,
            strerror(errno));
    return (-1);
  }

  if ((srcfile = fopen(src, "rb")) == NULL)
  {
    fclose(dstfile);
    unlink(dst);

    fprintf(stderr, "epm: Unable to open \"%s\" -\n     %s\n", src,
            strerror(errno));
    return (-1);
  }

 /*
  * Copy from src to dst...
  */

  while ((bytes = fread(buffer, 1, sizeof(buffer), srcfile)) > 0)
    if (fwrite(buffer, 1, bytes, dstfile) < bytes)
    {
      fprintf(stderr, "epm: Unable to write to \"%s\" -\n     %s\n", dst,
              strerror(errno));

      fclose(srcfile);
      fclose(dstfile);
      unlink(dst);

      return (-1);
    }

 /*
  * Close files, change permissions, and return...
  */

  fclose(srcfile);
  fclose(dstfile);

  chmod(dst, mode);
  chown(dst, owner, group);

  return (0);
}


/*
 * 'make_directory()' - Make a directory.
 */

int					/* O - 0 = success, -1 = error */
make_directory(const char *directory,	/* I - Directory */
               mode_t     mode,		/* I - Permissions */
	       uid_t      owner,	/* I - Owner ID */
	       gid_t      group)	/* I - Group ID */
{
  char	buffer[8192],			/* Filename buffer */
	*bufptr;			/* Pointer into buffer */


  for (bufptr = buffer; *directory;)
  {
    if (*directory == '/' && bufptr > buffer)
    {
      *bufptr = '\0';

      if (access(buffer, F_OK))
      {
	mkdir(buffer, 0777);
	chmod(buffer, mode | 0700);
	chown(buffer, owner, group);
      }
    }

    *bufptr++ = *directory++;
  }

  *bufptr = '\0';

  if (access(buffer, F_OK))
  {
    mkdir(buffer, 0777);
    chmod(buffer, mode | 0700);
    chown(buffer, owner, group);
  }

  return (0);
}


/*
 * 'make_link()' - Make a symbolic link.
 */

int					/* O - 0 = success, -1 = error */
make_link(const char *dst,		/* I - Destination file */
          const char *src)		/* I - Link */
{
  char	buffer[8192],			/* Copy buffer */
	*slash;				/* Pointer to trailing slash */


 /*
  * Check that the destination directory exists...
  */

  strcpy(buffer, dst);
  if ((slash = strrchr(buffer, '/')) != NULL)
    *slash = '\0';

  if (access(buffer, F_OK))
    make_directory(buffer, 0755, 0, 0);

 /* 
  * Make the symlink...
  */

  return (symlink(src, dst));
}


/*
 * 'strip_execs()' - Strip symbols from executable files in the distribution.
 */

void
strip_execs(dist_t *dist)		/* I - Distribution to strip... */
{
  int		i;			/* Looping var */
  file_t	*file;			/* Software file */
  FILE		*fp;			/* File pointer */
  char		header[4];		/* File header... */


 /*
  * Loop through the distribution files and strip any executable
  * files.
  */

  for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
    if (tolower(file->type) == 'f' && (file->mode & 0111) &&
        strstr(file->options, "nostrip()") == NULL)
    {
     /*
      * OK, this file has executable permissions; see if it is a
      * script...
      */

      if ((fp = fopen(file->src, "rb")) != NULL)
      {
       /*
        * Read the first 3 bytes of the file...
	*/

        if (fread(header, 1, sizeof(header) - 1, fp) == 0)
	  continue;

	header[sizeof(header) - 1] = '\0';

	fclose(fp);

       /*
        * Check for "#!/" or "#" + whitespace at the beginning of the file...
	*/

        if (!strncmp(header, "#!/", 3) ||
	    (header[0] == '#' && isspace(header[1] & 255)))
	  continue;
      }
      else
      {
       /*
        * File could not be opened; error out...
	*/

        fprintf(stderr, "epm: Unable to open file \"%s\" for destination "
	                "\"%s\" -\n     %s\n",
	        file->src, file->dst, strerror(errno));

        exit(1);
      }

     /*
      * Strip executables...
      */

      run_command(NULL, EPM_STRIP " %s", file->src);
    }
}


/*
 * 'unlink_package()' - Delete a package file.
 */

int					/* O - 0 on success, -1 on failure */
unlink_package(const char *ext,		/* I - Package filename extension */
               const char *prodname,	/* I - Product short name */
	       const char *directory,	/* I - Directory for distribution files */
               const char *platname,	/* I - Platform name */
               dist_t     *dist,	/* I - Distribution information */
	       const char *subpackage)	/* I - Subpackage */
{
  char		prodfull[255],		/* Full name of product */
		name[1024],		/* Full product name */
		filename[1024];		/* File to archive */


 /*
  * Figure out the full name of the distribution...
  */

  if (subpackage)
    snprintf(prodfull, sizeof(prodfull), "%s-%s", prodname, subpackage);
  else
    strlcpy(prodfull, prodname, sizeof(prodfull));

 /*
  * Then the subdirectory name...
  */

  if (dist->release[0])
    snprintf(name, sizeof(name), "%s-%s-%s", prodfull, dist->version,
             dist->release);
  else
    snprintf(name, sizeof(name), "%s-%s", prodfull, dist->version);

  if (platname[0])
  {
    strlcat(name, "-", sizeof(name));
    strlcat(name, platname, sizeof(name));
  }

  strlcat(name, ".", sizeof(name));
  strlcat(name, ext, sizeof(name));

 /*
  * Remove the package file...
  */

  snprintf(filename, sizeof(filename), "%s/%s", directory, name);

  if (unlink(filename))
  {
    fprintf(stderr, "epm: Unable to remove \"%s\" - %s\n", filename,
            strerror(errno));
    return (-1);
  }
  else
    return (0);
}


/*
 * End of "$Id: file.c 703 2006-08-29 16:43:09Z mike $".
 */