~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSPath.cc

  • Committer: Stewart Smith
  • Date: 2010-07-27 00:49:32 UTC
  • mto: (1720.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 1721.
  • Revision ID: stewart@flamingspork.com-20100727004932-basq3vx9szmmbswm
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Original author: Paul McCullagh
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-06-07
 
23
 *
 
24
 * CORE SYSTEM:
 
25
 * Basic file system path.
 
26
 *
 
27
 */
 
28
 
 
29
#include "CSConfig.h"
 
30
 
 
31
#include <unistd.h>
 
32
#include <sys/stat.h>
 
33
#include <sys/param.h>
 
34
#include <string.h>
 
35
#include <stdio.h>
 
36
 
 
37
static int unix_file_rename(const char *from, const char *to)
 
38
{
 
39
        return rename(from, to);
 
40
}
 
41
 
 
42
#include "CSStrUtil.h"
 
43
#include "CSFile.h"
 
44
#include "CSDirectory.h"
 
45
#include "CSPath.h"
 
46
#include "CSGlobal.h"
 
47
 
 
48
/*
 
49
 * ---------------------------------------------------------------
 
50
 * CORE SYSTEM PATH
 
51
 */
 
52
 
 
53
CSPath *CSPath::iCWD = NULL;
 
54
 
 
55
CSPath::~CSPath()
 
56
{
 
57
        if (iPath)
 
58
                iPath->release();
 
59
}
 
60
 
 
61
CSFile *CSPath::createFile(int mode)
 
62
{
 
63
        CSFile  *file;
 
64
        CSPath  *dir;
 
65
        bool    retry = false;
 
66
 
 
67
        CLOBBER_PROTECT(retry);
 
68
 
 
69
        enter_();
 
70
        /* Create and open the file: */
 
71
        do {
 
72
                try_(a) {
 
73
                        file = openFile(mode | CSFile::CREATE);
 
74
                        retry = false;
 
75
                }
 
76
                catch_(a) {
 
77
                        if (retry || !CSFile::isDirNotFound(&self->myException))
 
78
                                throw_();
 
79
 
 
80
                        /* Make sure the parent directory exists: */
 
81
                        dir = CSPath::newPath(RETAIN(this), "..");
 
82
                        try_(b) {
 
83
                                dir->makePath();
 
84
                        }
 
85
                        finally_(b) {
 
86
                                dir->release();
 
87
                        }
 
88
                        finally_end_block(b);
 
89
 
 
90
                        retry = true;
 
91
                }
 
92
                cont_(a);
 
93
        }
 
94
        while (retry);
 
95
        return_(file);
 
96
}
 
97
 
 
98
/* Copy the contents of one file to another.
 
99
 * The destination need not exist.
 
100
 * If the destination exists, it will be
 
101
 * overwritten if 'overwrite' is true.
 
102
 */
 
103
void CSPath::copyFile(CSPath *in_to_file, bool overwrite)
 
104
{
 
105
        CSPath                  *to_file = in_to_file;
 
106
        bool                    is_dir;
 
107
        CSFile                  *infile, *outfile;
 
108
 
 
109
        enter_();
 
110
        push_(in_to_file);
 
111
        
 
112
        if (to_file->exists(&is_dir)) {
 
113
                if (is_dir) {
 
114
                        to_file = CSPath::newPath(RETAIN(in_to_file), getNameCString());
 
115
                        push_(to_file);
 
116
                        
 
117
                        if (to_file->exists(&is_dir)) { 
 
118
                                if (!overwrite)
 
119
                                        CSException::throwFileError(CS_CONTEXT, to_file->getCString(), EEXIST);
 
120
                                to_file->remove();
 
121
                        }
 
122
                }
 
123
                else if (!overwrite)
 
124
                        CSException::throwFileError(CS_CONTEXT, to_file->getCString(), EEXIST);
 
125
        }
 
126
 
 
127
        infile = openFile(CSFile::READONLY);
 
128
        push_(infile);
 
129
 
 
130
        outfile = to_file->createFile(CSFile::TRUNCATE);
 
131
        push_(outfile);
 
132
        
 
133
        CSStream::pipe(outfile->getOutputStream(), infile->getInputStream());
 
134
        release_(outfile);
 
135
        release_(infile);
 
136
        
 
137
        if (to_file != in_to_file)
 
138
                release_(to_file);
 
139
                
 
140
        release_(in_to_file);
 
141
        exit_();
 
142
}
 
143
 
 
144
void CSPath::makePath()
 
145
{
 
146
        CSPath  *path;
 
147
        bool    is_dir;
 
148
 
 
149
        enter_();
 
150
        if (iPath->length() <= 1)
 
151
                exit_();
 
152
 
 
153
        if (exists(&is_dir)) {
 
154
                if (!is_dir)
 
155
                        CSException::throwFileError(CS_CONTEXT, iPath, EEXIST);
 
156
                exit_();
 
157
        }
 
158
 
 
159
        path = CSPath::newPath(RETAIN(this), "..");
 
160
        try_(a) {
 
161
                path->makePath();
 
162
                makeDir();
 
163
        }
 
164
        finally_(a) {
 
165
                path->release();
 
166
        }
 
167
        finally_end_block(a);
 
168
        exit_();
 
169
}
 
170
 
 
171
/* Copy the contents of one directory to another.
 
172
 * The destination must be a directory.
 
173
 * The new source directory will be copied to
 
174
 * a directory of the same name in the destination.
 
175
 */
 
176
void CSPath::copyDir(CSPath *in_to_dir, bool overwrite)
 
177
{
 
178
        CSPath          *to_dir = in_to_dir;
 
179
        bool            is_dir;
 
180
        CSDirectory *dir = NULL;
 
181
        CSPath          *path = NULL;
 
182
 
 
183
        enter_();
 
184
        push_(in_to_dir);
 
185
        
 
186
        if (to_dir->exists(&is_dir)) {
 
187
                if (!is_dir)
 
188
                        CSException::throwFileError(CS_CONTEXT, to_dir->getCString(), ENOTDIR);
 
189
                /* Add the source directory name to the destination: */
 
190
                to_dir = CSPath::newPath(RETAIN(in_to_dir), getNameCString());
 
191
                push_(to_dir);
 
192
                
 
193
                if (to_dir->exists(&is_dir)) {
 
194
                        if (!overwrite)
 
195
                                CSException::throwFileError(CS_CONTEXT, to_dir->getCString(), EEXIST);
 
196
                        to_dir->remove();
 
197
                }
 
198
        }
 
199
 
 
200
        /* Make the directory and copy the contents of the source
 
201
         * into it:
 
202
         */
 
203
        to_dir->makePath();
 
204
 
 
205
        dir = openDirectory();
 
206
        push_(dir);
 
207
        while (dir->next()) {
 
208
                self->interrupted();
 
209
                path = CSPath::newPath(RETAIN(this), dir->name());
 
210
                push_(path);
 
211
                if (dir->isFile())
 
212
                        path->copyFile(RETAIN(to_dir), overwrite);
 
213
                else
 
214
                        path->copyDir(RETAIN(to_dir), overwrite);
 
215
                release_(path);
 
216
        }
 
217
        release_(dir);
 
218
        
 
219
        if (to_dir != in_to_dir)
 
220
                release_(to_dir);
 
221
                
 
222
        release_(in_to_dir);
 
223
        
 
224
        exit_();
 
225
}
 
226
 
 
227
 
 
228
bool CSPath::isLink()
 
229
{
 
230
        struct stat sb;
 
231
        enter_();
 
232
 
 
233
        if (lstat(iPath->getCString(), &sb) == -1)
 
234
                CSException::throwFileError(CS_CONTEXT, iPath, errno);
 
235
                
 
236
        return_(S_ISLNK(sb.st_mode));
 
237
}
 
238
 
 
239
bool CSPath::isEmpty()
 
240
{
 
241
        CSDirectory *dir = NULL;
 
242
        bool            is_dir, result = true;
 
243
 
 
244
        enter_();
 
245
        if (!exists(&is_dir))
 
246
                return_(true);
 
247
 
 
248
        if (!is_dir)
 
249
                return_(false);
 
250
 
 
251
        dir = openDirectory();
 
252
        try_(a) {
 
253
                if (dir->next())
 
254
                        result = false;
 
255
        }
 
256
        finally_(a) {
 
257
                dir->release(); 
 
258
        }
 
259
        finally_end_block(a);
 
260
        return_(result);
 
261
}
 
262
 
 
263
void CSPath::emptyDir()
 
264
{
 
265
        CSDirectory *dir;
 
266
        CSPath          *path = NULL;
 
267
        bool            is_dir;
 
268
 
 
269
        enter_();
 
270
        if (!exists(&is_dir))
 
271
                exit_();
 
272
 
 
273
        if (!is_dir)
 
274
                CSException::throwFileError(CS_CONTEXT, iPath, ENOTDIR);
 
275
 
 
276
        dir = openDirectory();
 
277
        try_(a) {
 
278
                while (dir->next()) {
 
279
                        path = CSPath::newPath(RETAIN(this), dir->name());
 
280
                        if (dir->isFile())
 
281
                                path->remove();
 
282
                        else
 
283
                                path->removeDir();
 
284
                        path->release();
 
285
                        path = NULL;
 
286
                }
 
287
        }
 
288
        finally_(a) {
 
289
                if (path)
 
290
                        path->release();
 
291
                dir->release(); 
 
292
        }
 
293
        finally_end_block(a);
 
294
        exit_();
 
295
}
 
296
 
 
297
void CSPath::copyTo(CSPath *to_path, bool overwrite)
 
298
{
 
299
        bool is_dir;
 
300
 
 
301
        enter_();
 
302
        push_(to_path);
 
303
        if (!exists(&is_dir))
 
304
                CSException::throwFileError(CS_CONTEXT, iPath, ENOENT);
 
305
 
 
306
        pop_(to_path);
 
307
        if (is_dir)
 
308
                /* The source is a directory. */
 
309
                copyDir(to_path, overwrite);
 
310
        else {
 
311
                /* The source is not a directory: */
 
312
                copyFile(to_path, overwrite);
 
313
        }
 
314
 
 
315
        exit_();
 
316
}
 
317
 
 
318
void CSPath::moveTo(CSPath *in_to_path)
 
319
{
 
320
        CSPath  *to_path = in_to_path;
 
321
        bool    is_dir;
 
322
 
 
323
        enter_();
 
324
        if (!exists(NULL))
 
325
                CSException::throwFileError(CS_CONTEXT, iPath, ENOENT);
 
326
 
 
327
        try_(a) {
 
328
                if (to_path->exists(&is_dir)) {
 
329
                        if (is_dir) {
 
330
                                to_path = CSPath::newPath(RETAIN(in_to_path), getNameCString());
 
331
                                if (to_path->exists(NULL))
 
332
                                        CSException::throwFileError(CS_CONTEXT, to_path->getCString(), EEXIST);
 
333
                        }
 
334
                        else
 
335
                                CSException::throwFileError(CS_CONTEXT, to_path->getCString(), ENOTDIR);
 
336
                }
 
337
                move(to_path);
 
338
        }
 
339
        finally_(a) {
 
340
                if (to_path != in_to_path)
 
341
                        to_path->release();
 
342
        }
 
343
        finally_end_block(a);
 
344
 
 
345
        exit_();
 
346
}
 
347
 
 
348
void CSPath::remove()
 
349
{
 
350
        bool is_dir;
 
351
 
 
352
        if (exists(&is_dir)) {
 
353
                if (is_dir) {
 
354
                        emptyDir();
 
355
                        removeDir();
 
356
                }
 
357
                else
 
358
                        removeFile();
 
359
        }
 
360
}
 
361
 
 
362
 
 
363
void CSPath::touch(bool create_path)
 
364
{
 
365
        CSFile *file;
 
366
 
 
367
        enter_();
 
368
        if (create_path)
 
369
                file = createFile(CSFile::READONLY);
 
370
        else
 
371
                file = openFile(CSFile::READONLY | CSFile::CREATE);
 
372
        file->release();
 
373
        exit_();
 
374
}
 
375
 
 
376
CSString *CSPath::getString()
 
377
 
378
        return iPath;
 
379
}
 
380
 
 
381
const char *CSPath::getCString()
 
382
{
 
383
        return iPath->getCString();
 
384
}
 
385
 
 
386
const char *CSPath::getNameCString()
 
387
{
 
388
        const char *str = getCString();
 
389
        
 
390
        return cs_last_name_of_path(str);
 
391
}
 
392
 
 
393
off64_t CSPath::getSize()
 
394
{
 
395
        off64_t size;
 
396
 
 
397
        info((bool *) NULL, &size, (CSTime *) NULL);
 
398
        return size;
 
399
}
 
400
 
 
401
bool CSPath::isDir()
 
402
{
 
403
        bool    is_dir;
 
404
 
 
405
        info(&is_dir, (off64_t *) NULL, (CSTime *) NULL);
 
406
        return is_dir;
 
407
}
 
408
 
 
409
/*
 
410
 * ---------------------------------------------------------------
 
411
 * A UNIX PATH
 
412
 */
 
413
 
 
414
bool CSPath::exists(bool *is_dir)
 
415
{
 
416
        int err;
 
417
 
 
418
        err = access(iPath->getCString(), F_OK);
 
419
        if (err == -1)
 
420
                return false;
 
421
        if (is_dir)
 
422
                *is_dir = isDir();
 
423
        return true;
 
424
}
 
425
 
 
426
void CSPath::info(bool *is_dir, off64_t *size, CSTime *mod_time)
 
427
{
 
428
        struct stat sb;
 
429
 
 
430
        if (stat(iPath->getCString(), &sb) == -1)
 
431
                CSException::throwFileError(CS_CONTEXT, iPath, errno);
 
432
        if (is_dir)
 
433
                *is_dir = sb.st_mode & S_IFDIR;
 
434
        if (size)
 
435
                *size = sb.st_size;
 
436
        if (mod_time)
 
437
#ifdef __USE_MISC
 
438
                /* This is the Linux version: */
 
439
                mod_time->setUTC1970(sb.st_mtim.tv_sec, sb.st_mtim.tv_nsec);
 
440
#else
 
441
                mod_time->setUTC1970(sb.st_mtime, 0);
 
442
#endif
 
443
}
 
444
 
 
445
CSFile *CSPath::openFile(int mode)
 
446
{
 
447
        CSFile *file;
 
448
 
 
449
        enter_();
 
450
        file = CSFile::newFile(RETAIN(this));
 
451
        push_(file);
 
452
        file->open(mode);
 
453
        pop_(file);
 
454
        return_(file);
 
455
}
 
456
 
 
457
void CSPath::removeFile()
 
458
{
 
459
        if (unlink(iPath->getCString()) == -1) {
 
460
                int err = errno;
 
461
 
 
462
                if (err != ENOENT)
 
463
                        CSException::throwFileError(CS_CONTEXT, iPath, err);
 
464
        }
 
465
}
 
466
 
 
467
static void cs_set_stats(char *path)
 
468
{
 
469
        char            super_path[PATH_MAX];
 
470
        struct stat     stats;
 
471
        char            *ptr;
 
472
 
 
473
        ptr = cs_last_name_of_path(path);
 
474
        if (ptr == path) 
 
475
                strcpy(super_path, ".");
 
476
        else {
 
477
                cs_strcpy(PATH_MAX, super_path, path);
 
478
 
 
479
                if ((ptr = cs_last_name_of_path(super_path)))
 
480
                        *ptr = 0;
 
481
        }
 
482
        if (stat(super_path, &stats) == -1)
 
483
                CSException::throwFileError(CS_CONTEXT, path, errno);
 
484
 
 
485
        if (chmod(path, stats.st_mode) == -1)
 
486
                CSException::throwFileError(CS_CONTEXT, path, errno);
 
487
 
 
488
        /*chown(path, stats.st_uid, stats.st_gid);*/
 
489
}
 
490
 
 
491
void CSPath::makeDir()
 
492
{
 
493
        char path[PATH_MAX];
 
494
 
 
495
        cs_strcpy(PATH_MAX, path, iPath->getCString());
 
496
        cs_remove_dir_char(path);
 
497
 
 
498
        if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) == -1)
 
499
                CSException::throwFileError(CS_CONTEXT, iPath, errno);
 
500
 
 
501
        cs_set_stats(path);
 
502
}
 
503
 
 
504
CSDirectory *CSPath::openDirectory()
 
505
{
 
506
        CSDirectory *dir;
 
507
 
 
508
        enter_();
 
509
        dir = CSDirectory::newDirectory(RETAIN(this));
 
510
        push_(dir);
 
511
        dir->open();
 
512
        pop_(dir);
 
513
        return_(dir);
 
514
}
 
515
 
 
516
void CSPath::removeDir()
 
517
{                       
 
518
        emptyDir();
 
519
        if (rmdir(iPath->getCString()) == -1) {
 
520
                int err = errno;
 
521
 
 
522
                if (err != ENOENT)
 
523
                        CSException::throwFileError(CS_CONTEXT, iPath, err);
 
524
        }
 
525
}
 
526
 
 
527
void CSPath::rename(const char *name)
 
528
{
 
529
        char new_path[PATH_MAX];
 
530
        CSString *tmp_path, *old_path = iPath;
 
531
        
 
532
        enter_();
 
533
        
 
534
        cs_strcpy(PATH_MAX, new_path, iPath->getCString());
 
535
        cs_remove_last_name_of_path(new_path);
 
536
        cs_add_dir_char(PATH_MAX, new_path);
 
537
        cs_strcat(PATH_MAX, new_path, name);
 
538
        
 
539
        tmp_path = CSString::newString(new_path);
 
540
        push_(tmp_path);
 
541
        
 
542
        if (unix_file_rename(iPath->getCString(), new_path) == -1)
 
543
                CSException::throwFileError(CS_CONTEXT, iPath, errno);
 
544
                
 
545
        pop_(tmp_path);
 
546
        old_path = iPath;
 
547
        iPath = tmp_path;
 
548
        
 
549
        old_path->release();
 
550
        exit_();
 
551
}
 
552
 
 
553
void CSPath::move(CSPath *to_path)
 
554
{
 
555
        /* Cannot move from TD to non-TD: */
 
556
        if (unix_file_rename(iPath->getCString(), to_path->getCString()) == -1)
 
557
                CSException::throwFileError(CS_CONTEXT, iPath, errno);
 
558
}
 
559
 
 
560
CSPath *CSPath::getCWD()
 
561
{
 
562
        char path[MAXPATHLEN];
 
563
 
 
564
        if (!getcwd(path, MAXPATHLEN))
 
565
                CSException::throwOSError(CS_CONTEXT, errno);
 
566
 
 
567
        return newPath(path);
 
568
}
 
569
 
 
570
CSPath *CSPath::newPath(const char *path)
 
571
{
 
572
        if (!path)
 
573
                CSException::throwAssertion(CS_CONTEXT, "Initial string may not be NULL");
 
574
        return newPath(CSString::newString(path));
 
575
}
 
576
 
 
577
CSPath *CSPath::newPath(CSString *path)
 
578
{
 
579
        CSPath *p;
 
580
 
 
581
        enter_();
 
582
        push_(path);
 
583
        new_(p, CSPath());
 
584
 
 
585
        /* Adjust the path string so that it does not have
 
586
         * a terminating CS_DIR_CHAR:
 
587
         */
 
588
        if (path->endsWith(CS_DIR_DELIM) && path->length() > 1) {
 
589
                p->iPath = path->left(CS_DIR_DELIM, -1);
 
590
                path->release();
 
591
        }
 
592
        else
 
593
                p->iPath = path;
 
594
        pop_(path);
 
595
        return_(p);
 
596
}
 
597
 
 
598
CSPath *CSPath::newPath(CSPath *cwd, const char *path)
 
599
{
 
600
        char abs_path[PATH_MAX];
 
601
 
 
602
        enter_();
 
603
        cs_make_absolute_path(PATH_MAX, abs_path, path, cwd->getCString());
 
604
        cwd->release();
 
605
        CSPath *p = newPath(abs_path);
 
606
        return_(p);
 
607
}
 
608
 
 
609
CSPath *CSPath::newPath(CSString *cwd, const char *path)
 
610
{
 
611
        char abs_path[PATH_MAX];
 
612
 
 
613
        enter_();
 
614
        cs_make_absolute_path(PATH_MAX, abs_path, path, cwd->getCString());
 
615
        cwd->release();
 
616
        CSPath *p = newPath(abs_path);
 
617
        return_(p);
 
618
}
 
619
 
 
620
CSPath *CSPath::newPath(const char *cwd, CSString *path)
 
621
{
 
622
        char abs_path[PATH_MAX];
 
623
 
 
624
        enter_();
 
625
        cs_make_absolute_path(PATH_MAX, abs_path, path->getCString(), cwd);
 
626
        path->release();
 
627
        CSPath *p = newPath(abs_path);
 
628
        return_(p);
 
629
}
 
630
 
 
631
CSPath *CSPath::newPath(const char *cwd, const char *path)
 
632
{
 
633
        char abs_path[PATH_MAX];
 
634
 
 
635
        enter_();
 
636
        cs_make_absolute_path(PATH_MAX, abs_path, path, cwd);
 
637
        CSPath *p = newPath(abs_path);
 
638
        return_(p);
 
639
}
 
640