~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to cpp/src/IceUtil/FileUtil.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// **********************************************************************
2
2
//
3
 
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
 
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
4
4
//
5
5
// This copy of Ice is licensed to you under the terms described in the
6
6
// ICE_LICENSE file included in this distribution.
7
7
//
8
8
// **********************************************************************
9
9
 
 
10
#include <IceUtil/DisableWarnings.h>
10
11
#include <IceUtil/FileUtil.h>
 
12
#include <IceUtil/Unicode.h>
 
13
#include <IceUtil/Exception.h>
 
14
#include <climits>
 
15
#include <string.h>
 
16
 
 
17
#ifdef _WIN32
 
18
#  include <process.h>
 
19
#endif
 
20
 
 
21
#ifdef _WIN32
 
22
#  include <io.h>
 
23
#endif
 
24
 
 
25
#ifdef __BCPLUSPLUS__
 
26
#  include <dir.h>
 
27
#endif
11
28
 
12
29
using namespace std;
13
30
 
14
31
//
15
 
// Detemine if path is an absolute path
 
32
// Determine if path is an absolute path
16
33
//
17
34
bool
18
35
IceUtilInternal::isAbsolutePath(const string& path)
51
68
    return path[i] == '/';
52
69
#endif
53
70
}
 
71
 
 
72
//
 
73
// Determine if a directory exists.
 
74
//
 
75
bool
 
76
IceUtilInternal::directoryExists(const string& path)
 
77
{
 
78
    IceUtilInternal::structstat st;
 
79
    if(IceUtilInternal::stat(path, &st) != 0 || !S_ISDIR(st.st_mode))
 
80
    {
 
81
        return false;
 
82
    }
 
83
    return true;
 
84
}
 
85
 
 
86
//
 
87
// Determine if a regular file exists.
 
88
//
 
89
bool
 
90
IceUtilInternal::fileExists(const string& path)
 
91
{
 
92
    IceUtilInternal::structstat st;
 
93
    if(IceUtilInternal::stat(path, &st) != 0 || !S_ISREG(st.st_mode))
 
94
    {
 
95
        return false;
 
96
    }
 
97
    return true;
 
98
}
 
99
 
 
100
#ifdef _WIN32
 
101
 
 
102
//
 
103
// Stat
 
104
//
 
105
int
 
106
IceUtilInternal::stat(const string& path, structstat* buffer)
 
107
{
 
108
    return _wstat(IceUtil::stringToWstring(path).c_str(), buffer);
 
109
}
 
110
 
 
111
int
 
112
IceUtilInternal::remove(const string& path)
 
113
{
 
114
    return ::_wremove(IceUtil::stringToWstring(path).c_str());
 
115
}
 
116
 
 
117
int
 
118
IceUtilInternal::rename(const string& from, const string& to)
 
119
{
 
120
    return ::_wrename(IceUtil::stringToWstring(from).c_str(), IceUtil::stringToWstring(to).c_str());
 
121
}
 
122
 
 
123
int
 
124
IceUtilInternal::rmdir(const string& path)
 
125
{
 
126
    return ::_wrmdir(IceUtil::stringToWstring(path).c_str());
 
127
}
 
128
 
 
129
int
 
130
IceUtilInternal::mkdir(const string& path, int)
 
131
{
 
132
    return ::_wmkdir(IceUtil::stringToWstring(path).c_str());
 
133
}
 
134
 
 
135
FILE*
 
136
IceUtilInternal::fopen(const string& path, const string& mode)
 
137
{
 
138
    return ::_wfopen(IceUtil::stringToWstring(path).c_str(), IceUtil::stringToWstring(mode).c_str());
 
139
}
 
140
 
 
141
int
 
142
IceUtilInternal::open(const string& path, int flags)
 
143
{
 
144
    if(flags & _O_CREAT)
 
145
    {
 
146
        return ::_wopen(IceUtil::stringToWstring(path).c_str(), flags, _S_IREAD | _S_IWRITE);
 
147
    }
 
148
    else
 
149
    {
 
150
        return ::_wopen(IceUtil::stringToWstring(path).c_str(), flags);
 
151
    }
 
152
}
 
153
 
 
154
int
 
155
IceUtilInternal::getcwd(string& cwd)
 
156
{
 
157
    wchar_t cwdbuf[_MAX_PATH];
 
158
    if(_wgetcwd(cwdbuf, _MAX_PATH) == NULL)
 
159
    {
 
160
        return -1;
 
161
    }
 
162
    cwd = IceUtil::wstringToString(cwdbuf);
 
163
    return 0;
 
164
}
 
165
 
 
166
int
 
167
IceUtilInternal::unlink(const string& path)
 
168
{
 
169
    return _wunlink(IceUtil::stringToWstring(path).c_str());
 
170
}
 
171
 
 
172
int
 
173
IceUtilInternal::close(int fd)
 
174
{
 
175
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
 
176
        return _close(fd);
 
177
#else
 
178
        return ::close(fd);
 
179
#endif
 
180
}
 
181
 
 
182
IceUtilInternal::FileLock::FileLock(const std::string& path) :
 
183
    _fd(INVALID_HANDLE_VALUE),
 
184
    _path(path)
 
185
{
 
186
    _fd = ::CreateFileW(IceUtil::stringToWstring(path).c_str(), GENERIC_WRITE, 0, NULL,
 
187
                        OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 
188
    _path = path;
 
189
 
 
190
    if(_fd == INVALID_HANDLE_VALUE)
 
191
    {
 
192
        throw IceUtil::FileLockException(__FILE__, __LINE__, GetLastError(), _path);
 
193
    }
 
194
 
 
195
    if(::LockFile(_fd, 0, 0, 0, 0) == 0)
 
196
    {
 
197
        ::CloseHandle(_fd);
 
198
        throw IceUtil::FileLockException(__FILE__, __LINE__, GetLastError(), _path);
 
199
    }
 
200
    //
 
201
    // In Windows implementation we don't write the process pid to the file, as is 
 
202
    // not posible to read the file from other process while it is locked here.
 
203
    //
 
204
}
 
205
 
 
206
IceUtilInternal::FileLock::~FileLock()
 
207
{
 
208
    assert(_fd != INVALID_HANDLE_VALUE);
 
209
    CloseHandle(_fd);
 
210
    unlink(_path);
 
211
}
 
212
 
 
213
#ifdef _STLP_BEGIN_NAMESPACE
 
214
namespace
 
215
{
 
216
int
 
217
toFileFlags(ios_base::openmode mode)
 
218
{
 
219
    int flags = 0;
 
220
    if(mode & ios_base::app)
 
221
    {
 
222
        flags |= _O_APPEND;
 
223
    }
 
224
    if(mode & ios_base::trunc)
 
225
    {
 
226
        flags |= _O_TRUNC;
 
227
    }
 
228
    if(mode & ios_base::binary)
 
229
    {
 
230
        flags |= _O_BINARY;
 
231
    }
 
232
    if((mode & ios_base::in) && !(mode & ios_base::out))
 
233
    {
 
234
        flags |= _O_RDONLY;
 
235
    }
 
236
    else if((mode & ios_base::out) && !(mode & ios_base::in))
 
237
    {
 
238
        flags |= _O_WRONLY | _O_CREAT;
 
239
    }
 
240
    else 
 
241
    {
 
242
        flags |= _O_RDWR;
 
243
        if(mode & ios_base::trunc)
 
244
        {
 
245
            flags |= _O_CREAT;
 
246
        }
 
247
    }
 
248
    return flags;
 
249
}
 
250
}
 
251
#endif
 
252
 
 
253
IceUtilInternal::ifstream::ifstream()
 
254
{
 
255
}
 
256
 
 
257
#ifdef _STLP_BEGIN_NAMESPACE
 
258
 
 
259
IceUtilInternal::ifstream::ifstream(const string& path, ios_base::openmode mode) : _fd(-1)
 
260
{
 
261
    open(path, mode);
 
262
}
 
263
 
 
264
IceUtilInternal::ifstream::~ifstream()
 
265
{
 
266
    close();
 
267
}
 
268
 
 
269
void
 
270
IceUtilInternal::ifstream::close()
 
271
{
 
272
    if(!rdbuf()->close())
 
273
    {
 
274
        setstate(ios_base::failbit);
 
275
    }
 
276
    if(_fd >= 0)
 
277
    {
 
278
        _close(_fd);
 
279
    }
 
280
}
 
281
 
 
282
void
 
283
IceUtilInternal::ifstream::open(const string& path, ios_base::openmode mode)
 
284
{
 
285
    mode |= ifstream::in;
 
286
    _fd = IceUtilInternal::open(path, toFileFlags(mode));
 
287
    if(_fd < 0 || !rdbuf()->open(_fd, mode))
 
288
    {
 
289
        setstate(ios_base::failbit);
 
290
    }
 
291
    if(mode & (ios_base::ate || ios_base::app))
 
292
    {
 
293
        seekg(ios_base::end);
 
294
    }
 
295
}
 
296
 
 
297
#else
 
298
 
 
299
IceUtilInternal::ifstream::ifstream(const string& path, ios_base::openmode mode) : std::ifstream(IceUtil::stringToWstring(path).c_str(), mode)
 
300
{
 
301
}
 
302
 
 
303
void
 
304
IceUtilInternal::ifstream::open(const string& path, ios_base::openmode mode)
 
305
{
 
306
    std::ifstream::open(IceUtil::stringToWstring(path).c_str(), mode);
 
307
}
 
308
 
 
309
#endif
 
310
 
 
311
IceUtilInternal::ofstream::ofstream()
 
312
{
 
313
}
 
314
 
 
315
#ifdef _STLP_BEGIN_NAMESPACE
 
316
 
 
317
IceUtilInternal::ofstream::ofstream(const string& path, ios_base::openmode mode) : _fd(-1)
 
318
{
 
319
    open(path, mode);
 
320
}
 
321
 
 
322
IceUtilInternal::ofstream::~ofstream()
 
323
{
 
324
    close();
 
325
}
 
326
 
 
327
void
 
328
IceUtilInternal::ofstream::close()
 
329
{
 
330
    if(!rdbuf()->close())
 
331
    {
 
332
        setstate(ios_base::failbit);
 
333
    }
 
334
    if(_fd >= 0)
 
335
    {
 
336
        _close(_fd);
 
337
    }
 
338
}
 
339
 
 
340
void
 
341
IceUtilInternal::ofstream::open(const string& path, ios_base::openmode mode)
 
342
{
 
343
    mode |= ofstream::out;
 
344
    _fd = IceUtilInternal::open(path, toFileFlags(mode));
 
345
    if(_fd < 0 || !rdbuf()->open(_fd, mode))
 
346
    {
 
347
        setstate(ios_base::failbit);
 
348
    }
 
349
    if(mode & (ios_base::ate || ios_base::app))
 
350
    {
 
351
        seekp(ios_base::end);
 
352
    }
 
353
}
 
354
 
 
355
#else
 
356
 
 
357
IceUtilInternal::ofstream::ofstream(const string& path, ios_base::openmode mode) : std::ofstream(IceUtil::stringToWstring(path).c_str(), mode)
 
358
{
 
359
}
 
360
 
 
361
void
 
362
IceUtilInternal::ofstream::open(const string& path, ios_base::openmode mode)
 
363
{
 
364
    std::ofstream::open(IceUtil::stringToWstring(path).c_str(), mode);
 
365
}
 
366
 
 
367
#endif
 
368
 
 
369
#else
 
370
 
 
371
//
 
372
// Stat
 
373
//
 
374
int
 
375
IceUtilInternal::stat(const string& path, structstat* buffer)
 
376
{
 
377
    return ::stat(path.c_str(), buffer);
 
378
}
 
379
 
 
380
int
 
381
IceUtilInternal::remove(const string& path)
 
382
{
 
383
    return ::remove(path.c_str());
 
384
}
 
385
 
 
386
int
 
387
IceUtilInternal::rename(const string& from, const string& to)
 
388
{
 
389
    return ::rename(from.c_str(), to.c_str());
 
390
}
 
391
 
 
392
int
 
393
IceUtilInternal::rmdir(const string& path)
 
394
{
 
395
    return ::rmdir(path.c_str());
 
396
}
 
397
 
 
398
int
 
399
IceUtilInternal::mkdir(const string& path, int perm)
 
400
{
 
401
    return ::mkdir(path.c_str(), perm);
 
402
}
 
403
 
 
404
FILE*
 
405
IceUtilInternal::fopen(const string& path, const string& mode)
 
406
{
 
407
    return ::fopen(path.c_str(), mode.c_str());
 
408
}
 
409
 
 
410
int
 
411
IceUtilInternal::open(const string& path, int flags)
 
412
{
 
413
    if(flags & O_CREAT)
 
414
    {
 
415
        // By default, create with rw-rw-rw- modified by the user's umask (same as fopen).
 
416
        return ::open(path.c_str(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
 
417
    }
 
418
    else
 
419
    {
 
420
        return ::open(path.c_str(), flags);
 
421
    }
 
422
}
 
423
 
 
424
int
 
425
IceUtilInternal::getcwd(string& cwd)
 
426
{
 
427
    char cwdbuf[PATH_MAX];
 
428
    if(::getcwd(cwdbuf, PATH_MAX) == NULL)
 
429
    {
 
430
        return -1;
 
431
    }
 
432
    cwd = cwdbuf;
 
433
    return 0;
 
434
}
 
435
 
 
436
int
 
437
IceUtilInternal::unlink(const string& path)
 
438
{
 
439
    return ::unlink(path.c_str());
 
440
}
 
441
 
 
442
int
 
443
IceUtilInternal::close(int fd)
 
444
{
 
445
    return ::close(fd);
 
446
}
 
447
 
 
448
IceUtilInternal::FileLock::FileLock(const std::string& path) :
 
449
    _fd(-1),
 
450
    _path(path)
 
451
{
 
452
    _fd = ::open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
 
453
    if(_fd < 0)
 
454
    {
 
455
        throw IceUtil::FileLockException(__FILE__, __LINE__, errno, _path);
 
456
    }
 
457
 
 
458
    struct ::flock lock;
 
459
    lock.l_type = F_WRLCK; // Write lock
 
460
    lock.l_whence = SEEK_SET; // Begining of file
 
461
    lock.l_start = 0;
 
462
    lock.l_len = 0;
 
463
    
 
464
    //
 
465
    // F_SETLK tells fcntl to not block if it cannot 
 
466
    // acquire the lock, if the lock cannot be acquired 
 
467
    // it returns -1 without wait.
 
468
    //
 
469
    if(::fcntl(_fd, F_SETLK, &lock) == -1)
 
470
    {
 
471
        IceUtil::FileLockException ex(__FILE__, __LINE__, errno, _path);
 
472
        close(_fd);
 
473
        throw ex;
 
474
    }
 
475
 
 
476
    //
 
477
    // If there is an error after here, we close the fd,
 
478
    // to release the lock.
 
479
    //
 
480
    
 
481
    //
 
482
    // Now that we have acquire an excluxive write lock,
 
483
    // write the process pid there.
 
484
    //
 
485
    ostringstream os;
 
486
    os << getpid();
 
487
    
 
488
    if(write(_fd, os.str().c_str(), os.str().size()) == -1)
 
489
    {
 
490
        IceUtil::FileLockException ex(__FILE__, __LINE__, errno, _path);
 
491
        close(_fd);
 
492
        throw ex;
 
493
    }
 
494
}
 
495
 
 
496
IceUtilInternal::FileLock::~FileLock()
 
497
{
 
498
    assert(_fd > -1);
 
499
    unlink(_path);
 
500
}
 
501
 
 
502
IceUtilInternal::ifstream::ifstream()
 
503
{
 
504
}
 
505
 
 
506
IceUtilInternal::ifstream::ifstream(const string& path, ios_base::openmode mode) : std::ifstream(path.c_str(), mode)
 
507
{
 
508
}
 
509
 
 
510
void
 
511
IceUtilInternal::ifstream::open(const string& path, ios_base::openmode mode)
 
512
{
 
513
    std::ifstream::open(path.c_str(), mode);
 
514
}
 
515
 
 
516
IceUtilInternal::ofstream::ofstream()
 
517
{
 
518
}
 
519
 
 
520
IceUtilInternal::ofstream::ofstream(const string& path, ios_base::openmode mode) : std::ofstream(path.c_str(), mode)
 
521
{
 
522
}
 
523
 
 
524
void
 
525
IceUtilInternal::ofstream::open(const string& path, ios_base::openmode mode)
 
526
{
 
527
    std::ofstream::open(path.c_str(), mode);
 
528
}
 
529
 
 
530
#endif