File: files.cxx

package info (click to toggle)
bsign 0.4.4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 420 kB
  • ctags: 647
  • sloc: cpp: 3,019; ansic: 344; makefile: 294; sh: 219
file content (186 lines) | stat: -rw-r--r-- 4,491 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
/* files.cxx
     $Id: files.cxx,v 1.11 2002/01/17 06:24:29 elf Exp $

   written by Oscar Levi
   10 December 1998

   This file is part of the project BSIGN.  See the file README for
   more information.

   Copyright (C) 1998 The Buici Company.

   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 of the License, 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.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

   -----------
   DESCRIPTION
   -----------

   Simple routines for manipulating files in the filesystem.

*/

#include "standard.h"
#include "sys/stat.h"
#include "sys/types.h"
#include "limits.h"
#include "utime.h"
#include "errno.h"
#include "files.h"


/* dup_status

   copies the status of one file to another.  This is meant to
   replicate the ownership and permissions of the file.

   When chmod is before chown and we are attempting to set the setuid
   bit, the chown call clears that bit.  Transposing these calls makes
   it work properly.

*/

bool dup_status (const char* szFileNew, const char* szFile)
{
  struct stat st;
  if (::stat (szFile, &st))
    return false;
  struct utimbuf t;
  t.actime = st.st_atime;
  t.modtime = st.st_mtime;
  if (utime (szFileNew, &t))
    return false;
  if (chown (szFileNew, st.st_uid, st.st_gid))
    return false;
  if (chmod (szFileNew, st.st_mode))
    return false;
  return true;
}

#if 0
/* is_file

   returns true if the named file is a regular file or a link to a
   regular file. 

*/

bool is_file (const char* szPath)
{
  struct stat st;
  memset (&st, 0, sizeof (struct stat));
  if (stat (szPath, &st))
    return false;
  return S_ISREG (st.st_mode);
}


/* is_symlink

   returns true of the last component of szPath is a symbolic link.
   Directory components that are symbolic links are not detected. 

*/

bool is_symlink (const char* szPath)
{
  struct stat st;
  memset (&st, 0, sizeof (struct stat));
  if (lstat (szPath, &st))
    return false;
  return S_ISLNK (st.st_mode);
}
#endif

char* path_of (const char* szPathOriginal)
{
  char* sz = resolve_link (szPathOriginal);
  if (!sz) {			// No link to resolve
    sz = new char [strlen (szPathOriginal) + 3];
    strcpy (sz, szPathOriginal);
  }

  char* pch = rindex (sz, '/');
  if (!pch) {
    strcpy (sz, ".");
    return sz;
  }
  *pch = 0;

  return sz;
}


char* resolve_link (const char* szPathOriginal)
{
  if (!szPathOriginal || !*szPathOriginal 
      || strlen (szPathOriginal) >= PATH_MAX)
    return NULL;

  static char szPath[PATH_MAX];
  strcpy (szPath, szPathOriginal);
  
  struct stat st;
  
				// Find where the link really points
  do {
    memset (&st, 0, sizeof (struct stat));
    if (lstat (szPath, &st))
      return NULL;
    if (S_ISLNK (st.st_mode)) {
      static char sz[PATH_MAX + 1];
      int cb = readlink (szPath, sz, sizeof (sz) - 1);
      if (cb == -1)
	return NULL;
      sz[cb] = 0;
      if (*sz == '/')		// Absolute path link
	strcpy (szPath, sz);
      else {			// Relative path link
	if (strlen (szPath) + strlen (sz) >= PATH_MAX)
	  return NULL;
	char* pch = rindex (szPath, '/');
	strcpy (pch ? (pch + 1) : szPath, sz);
      }
    }
  } while (S_ISLNK (st.st_mode));

  char* sz = new char[strlen (szPath) + 1];
  strcpy (sz, szPath);
  return sz;
}


/* replace_file

   function for replacing the old file with the new one.  Because we
   create our temporary output file in the same directory as the
   output target, it is extremely improbably that we will not be able
   to replace the old file with the new.  This function guarantees
   that at no time will the old file be deleted before the new file
   replaces it.

*/

bool replace_file (const char* szFileOutputOriginal, const char* szFile)
{
  char* szFileOutput = resolve_link (szFileOutputOriginal);
  if (!szFileOutput)		// Link resolution error
    return false;

  int result = rename (szFile, szFileOutput); // One step replacement 

  free (szFileOutput);
  return result == 0;
}