~ubuntu-branches/ubuntu/maverick/aspectc++/maverick

« back to all changes in this revision

Viewing changes to Puma/src/basics/SysCall.cc

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2008-04-10 17:40:52 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080410174052-xdnsm7oi8hauyyf1
Tags: 1.0pre4~svn.20080409+dfsg-3
Fix another missing include, this time in Ag++/StdSystem.cc

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <fcntl.h>
24
24
#include <map>
25
25
using std::map;
 
26
 
26
27
#ifdef _MSC_VER
27
 
#include <direct.h>
28
 
#include <string>
 
28
# include <direct.h>
 
29
# include <string>
29
30
using namespace std;
30
 
#include <io.h>
31
31
#else
 
32
# include <unistd.h>
 
33
# include <dirent.h>
 
34
#endif // _MSC_VER
 
35
 
32
36
#ifdef WIN32
33
 
#include <io.h>
34
 
#include <windows.h>
 
37
# include <io.h>
 
38
# include <windows.h>
35
39
#endif // WIN32
36
 
#include <unistd.h>
37
 
#include <dirent.h>
38
 
#endif // _MSC_VER
 
40
 
39
41
#include <string.h>
40
42
#include <stdlib.h>
41
43
 
 
44
#ifndef MAX_PATH
 
45
#define MAX_PATH PATH_MAX
 
46
#endif // MAX_PATH
 
47
 
42
48
namespace Puma {
43
49
 
44
50
 
140
146
  return fd;
141
147
}
142
148
 
 
149
int SysCall::open_excl (const char *file, int flags, ErrorSink *err) {
 
150
  int fd = open (file, flags, err);
 
151
#if defined(linux)
 
152
  if (!(fd < 0)) {
 
153
    flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
 
154
    if (fcntl (fd, F_SETLKW, &lock) == -1)
 
155
      printerror (err, "fcntl lock", file);
 
156
  }
 
157
#endif 
 
158
  return fd;
 
159
}
 
160
 
 
161
int SysCall::create (const char *file, int mode, ErrorSink *err) {
 
162
  int fd = -1;
 
163
#if defined(WIN32)
 
164
  char *n = StrCol::dup (file);
 
165
  if (n) {
 
166
    MakeDosPath (n);
 
167
    fd = ::open (n, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, mode);
 
168
    delete[] n;
 
169
  }
 
170
#else
 
171
  fd = ::open (file, O_CREAT|O_WRONLY|O_TRUNC, mode);
 
172
#endif
 
173
  if (fd == -1) printerror (err, "create", file);
 
174
  return fd;
 
175
}
 
176
 
 
177
int SysCall::create_excl (const char *file, int mode, ErrorSink *err) {
 
178
  int fd = create (file, mode, err);
 
179
#if defined(linux)
 
180
  if (!(fd < 0)) {
 
181
    flock lock = { F_WRLCK, SEEK_SET, 0, 0, 0 };
 
182
    if (fcntl (fd, F_SETLKW, &lock) == -1)
 
183
      printerror (err, "fcntl lock", file);
 
184
  }
 
185
#endif 
 
186
  return fd;
 
187
}
143
188
 
144
189
bool SysCall::close (int fd, ErrorSink *err) {
145
190
  int ret = ::close (fd);
150
195
  return true;
151
196
}
152
197
 
 
198
bool SysCall::close_excl (int fd, ErrorSink *err) {
 
199
#if defined(linux)
 
200
  flock lock = { F_UNLCK, SEEK_SET, 0, 0, 0 };
 
201
  if (fcntl (fd, F_SETLKW, &lock) == -1) {
 
202
    printerror (err, "fcntl unlock", fd);
 
203
    return false;
 
204
  }
 
205
#endif
 
206
  return close (fd, err);
 
207
}
153
208
 
154
209
long SysCall::read (int fd, void *buffer, size_t n, ErrorSink *err) {
155
210
  long bytes = ::read (fd, buffer, n);
378
433
  return fstr;
379
434
}
380
435
 
 
436
 
381
437
#ifdef WIN32
382
438
bool SysCall::normalize (Filename filename, Filename &norm, ErrorSink *err) {
383
439
  const char *name = filename.name ();
388
444
  if (strlen (name) == 2 && name[1] == ':' && isalpha (name[0])) {
389
445
    result = toupper (name[0]);
390
446
    result += ":";
391
 
  }
392
 
  else {
 
447
  } else {
393
448
    // normalize the directory part recursively ...
394
449
    Filename norm_path;
395
 
    if (!normalize (filename.path (), norm_path, err))
 
450
    if (! canonical (filename.path (), norm_path, err)) {
396
451
      return false;
 
452
    }
397
453
    // .. and find the normlized filename with _findfirst
398
454
    struct _finddata_t find_data;
399
 
    long hdl = _findfirst (filename.name (), &find_data);
400
 
    if (hdl == -1L)
 
455
    long hdl = _findfirst (name, &find_data);
 
456
    if (hdl == -1L) {
401
457
      return false;
 
458
    }
402
459
    result = norm_path.name ();
403
460
    result += "/";
404
461
    result += find_data.name;
409
466
}
410
467
#endif // WIN32
411
468
 
 
469
 
412
470
typedef map<DString, Filename> FilenameMap;
413
471
typedef FilenameMap::value_type FilenameMapPair;
414
472
FilenameMap canonical_cache;
415
473
 
416
474
bool SysCall::canonical (Filename filename, Filename &result, ErrorSink *err) {
417
 
 
418
475
  // first check if the searched canonical name is already cached  
419
476
  FilenameMap::iterator fn = canonical_cache.find (filename.name ());
420
477
  if (fn != canonical_cache.end ()) {
423
480
  }
424
481
  
425
482
  // build a canonical path name
426
 
  char *file_abs = 0;
427
 
  char  file_buf[PATH_MAX];
 
483
  const char *file_abs = filename.name ();
 
484
  char file_buf[MAX_PATH];
428
485
 
429
486
#if defined (WIN32)
430
 
  file_abs = _fullpath(file_buf, filename.name (), PATH_MAX);
431
 
  if (file_abs) {
 
487
  if ((strlen (file_abs) == 2 && file_abs[1] == ':' && isalpha (file_abs[0])) ||
 
488
      (file_abs = _fullpath(file_buf, file_abs, MAX_PATH))) {
432
489
    Filename abs (file_abs);
433
 
    if (!normalize (abs, result, err))
 
490
    if (! normalize (abs, result, err)) {
434
491
      file_abs = 0;
 
492
    }
435
493
  }
436
494
#else
437
 
  file_abs = realpath(filename.name (), file_buf);
438
 
  if (file_abs)
 
495
  file_abs = realpath(file_abs, file_buf);
 
496
  if (file_abs) {
439
497
    result.name (file_abs);
 
498
  }
440
499
#endif
441
500
 
442
 
  if (!file_abs) {
 
501
  if (! file_abs) {
443
502
    printerror (err, "canonical", filename.name ());
444
503
    return false;
445
504
  }
448
507
  return true;
449
508
}
450
509
 
 
510
 
451
511
} // namespace Puma