libexplain  1.4.D001
libexplain/fileinfo/dir_tree_in_use.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/string.h>
00021 
00022 #include <libexplain/fileinfo.h>
00023 #include <libexplain/lsof.h>
00024 #include <libexplain/string_buffer.h>
00025 
00026 
00027 typedef struct adapter adapter;
00028 struct adapter
00029 {
00030     explain_lsof_t  inherited;
00031     int             found;
00032 };
00033 
00034 
00035 static void
00036 n_callback(explain_lsof_t *context, const char *name)
00037 {
00038     adapter         *a;
00039 
00040     /*
00041      * Sometimes lsof(1) is less than helpful, and says "n (readlink:
00042      * Permission denied)" which is effectively no answer at all.
00043      *
00044      * There is a very small chance of discarding a valid result.
00045      * Get fussier if it proves to be an actual problem.
00046      */
00047     a = (adapter *)context;
00048     if (!strstr(name, " (readlink: "))
00049         return;
00050     a->found++;
00051 }
00052 
00053 
00054 int
00055 explain_fileinfo_dir_tree_in_use(const char *path)
00056 {
00057     adapter         obj;
00058     char            options[1000];
00059     explain_string_buffer_t sb;
00060 
00061     explain_string_buffer_init(&sb, options, sizeof(options));
00062     explain_string_buffer_puts(&sb, "-- ");
00063     explain_string_buffer_puts_shell_quoted(&sb, path);
00064 
00065     obj.inherited.n_callback = n_callback;
00066     obj.found = 0;
00067     explain_lsof(options, &obj.inherited);
00068     return obj.found;
00069 }
00070 
00071 
00072 /* vim: set ts=8 sw=4 et : */