~ubuntu-branches/ubuntu/trusty/kmod/trusty

« back to all changes in this revision

Viewing changes to libkmod/libkmod-util.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-10-24 06:16:30 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20131024061630-o87ipf2zxqtpigls
Tags: 15-0ubuntu1
* New upstream release. (See Debian #716739)
* Drop patches included upstream: dot_kcmdline, bad_alias_assertion,
  blacklist_aliased.
* Drop check_builtin_kver, this would only apply to lucid and is otherwise
  unnecessary.
* Drop --disable-shared/--enable-static, this got dropped in this version
  and isn't necessary any more. /bin/kmod does not dynamically link to
  libkmod by default now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * libkmod - interface to kernel module operations
3
3
 *
4
 
 * Copyright (C) 2011-2012  ProFUSION embedded systems
 
4
 * Copyright (C) 2011-2013  ProFUSION embedded systems
 
5
 * Copyright (C) 2012  Lucas De Marchi <lucas.de.marchi@gmail.com>
 
6
 * Copyright (C) 2013  Intel Corporation. All rights reserved.
5
7
 *
6
8
 * This library is free software; you can redistribute it and/or
7
9
 * modify it under the terms of the GNU Lesser General Public
29
31
#include <ctype.h>
30
32
 
31
33
#include "libkmod.h"
32
 
#include "libkmod-private.h"
 
34
#include "libkmod-internal.h"
33
35
 
34
36
/*
35
37
 * Read one logical line from a configuration file.
308
310
        return r;
309
311
}
310
312
 
 
313
static inline int is_dir(const char *path)
 
314
{
 
315
        struct stat st;
 
316
 
 
317
        if (stat(path, &st) >= 0)
 
318
                return S_ISDIR(st.st_mode);
 
319
 
 
320
        return -errno;
 
321
}
 
322
 
 
323
int mkdir_p(const char *path, int len, mode_t mode)
 
324
{
 
325
        char *start, *end;
 
326
 
 
327
        start = strndupa(path, len);
 
328
        end = start + len;
 
329
 
 
330
        /*
 
331
         * scan backwards, replacing '/' with '\0' while the component doesn't
 
332
         * exist
 
333
         */
 
334
        for (;;) {
 
335
                int r = is_dir(start);
 
336
                if (r > 0) {
 
337
                        end += strlen(end);
 
338
 
 
339
                        if (end == start + len)
 
340
                                return 0;
 
341
 
 
342
                        /* end != start, since it would be caught on the first
 
343
                         * iteration */
 
344
                        *end = '/';
 
345
                        break;
 
346
                } else if (r == 0)
 
347
                        return -ENOTDIR;
 
348
 
 
349
                if (end == start)
 
350
                        break;
 
351
 
 
352
                *end = '\0';
 
353
 
 
354
                /* Find the next component, backwards, discarding extra '/'*/
 
355
                while (end > start && *end != '/')
 
356
                        end--;
 
357
 
 
358
                while (end > start && *(end - 1) == '/')
 
359
                        end--;
 
360
        }
 
361
 
 
362
        for (; end < start + len;) {
 
363
                if (mkdir(start, mode) < 0 && errno != EEXIST)
 
364
                        return -errno;
 
365
 
 
366
                end += strlen(end);
 
367
                *end = '/';
 
368
        }
 
369
 
 
370
        return 0;
 
371
}
 
372
 
 
373
int mkdir_parents(const char *path, mode_t mode)
 
374
{
 
375
        char *end = strrchr(path, '/');
 
376
 
 
377
        /* no parent directories */
 
378
        if (end == NULL)
 
379
                return 0;
 
380
 
 
381
        return mkdir_p(path, end - path, mode);
 
382
}
 
383
 
 
384
const struct kmod_ext kmod_exts[] = {
 
385
        {".ko", sizeof(".ko") - 1},
 
386
#ifdef ENABLE_ZLIB
 
387
        {".ko.gz", sizeof(".ko.gz") - 1},
 
388
#endif
 
389
#ifdef ENABLE_XZ
 
390
        {".ko.xz", sizeof(".ko.xz") - 1},
 
391
#endif
 
392
        { }
 
393
};
 
394
 
 
395
bool path_ends_with_kmod_ext(const char *path, size_t len)
 
396
{
 
397
        const struct kmod_ext *eitr;
 
398
 
 
399
        for (eitr = kmod_exts; eitr->ext != NULL; eitr++) {
 
400
                if (len <= eitr->len)
 
401
                        continue;
 
402
                if (streq(path + len - eitr->len, eitr->ext))
 
403
                        return true;
 
404
        }
 
405
 
 
406
        return false;
 
407
}
 
408
 
311
409
#define USEC_PER_SEC  1000000ULL
312
410
#define NSEC_PER_USEC 1000ULL
313
411
unsigned long long ts_usec(const struct timespec *ts)